-- | Insert a `Card`. If the `Card` already exists then replace it with the new value.
insert :: Card -> DeckList -> DeckList
insert a b =
let t = unrefine . runText . name $ a
in DeckList . Set.insert (Arg t a) . runDeckList $ b
-- | Delete a card from a decklist if it exists
delete :: CardName -> DeckList -> DeckList
delete x =
let cardName = unrefine . runText $ x
in DeckList . Set.delete (Arg cardName (error "unreachable")) . runDeckList
{- | Update a `Card` in a `DeckList`. @`update` f a b@ applies the function @f@ on the card
| if it exists, otherwise it leaves the `DeckList` unchanged.
-}
update ::
((Category, PositiveNumber) -> (Category, PositiveNumber))
-> Card
-> DeckList
-> DeckList
update f a b =
let cname = unrefine . runText . name $ a
argname (Arg x _) = x
card = find ((== cname) . argname) . runDeckList $ b
tuple x = (category x, quantity x)
toCard (category, quantity) x = x {category, quantity}
in case card of
Nothing -> b
Just ((Arg _ y)) -> insert (flip toCard a . f $ tuple y) b