Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created October 21, 2012 19:46
Show Gist options
  • Save seanparsons/3928225 to your computer and use it in GitHub Desktop.
Save seanparsons/3928225 to your computer and use it in GitHub Desktop.
Expanded inventory example using Semigroups.
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed abstract class Inventory
case object UserInventory extends Inventory
case object BankInventory extends Inventory
// Exiting paste mode, now interpreting.
defined class Inventory
defined module UserInventory
defined module BankInventory
scala> val currentInventories: Map[Inventory, Map[String, Int]] = Map(BankInventory -> Map("monkeywrench" -> 3, "toothbrush" -> 1), UserInventory -> Map("map" -> 1))
currentInventories: Map[Inventory,Map[String,Int]] = Map(BankInventory -> Map(monkeywrench -> 3, toothbrush -> 1), UserInventory -> Map(map -> 1))
scala> val inventoryChange: Map[Inventory, Map[String, Int]] = Map(BankInventory -> Map("monkeywrench" -> -1), UserInventory -> Map("monkeywrench" -> 1))
inventoryChange: Map[Inventory,Map[String,Int]] = Map(BankInventory -> Map(monkeywrench -> -1), UserInventory -> Map(monkeywrench -> 1))
scala> val updatedInventories = currentInventories |+| inventoryChange
updatedInventories: Map[Inventory,Map[String,Int]] = Map(BankInventory -> Map(monkeywrench -> 2, toothbrush -> 1), UserInventory -> Map(monkeywrench -> 1, map -> 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment