Created
October 21, 2012 19:46
-
-
Save seanparsons/3928225 to your computer and use it in GitHub Desktop.
Expanded inventory example using Semigroups.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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