totalCopies :: Card -> PositiveNumber
totalCopies card =
PositiveNumber . unsafeRefine . getSum $
mconcat . map (Sum . unrefine . runNumber . view copies) $
card ^.. categories . traverse
diffCategory ::
(DeckSYM repr, HunkSYM repr) =>
Card
-> [Category]
-> [Category]
-> [DiffSig repr]
diffCategory card left right = go mempty (sorter left) (sorter right)
where
sorter = sortBy (compare `on` view Category.name)
go ::
(HunkSYM repr, DeckSYM repr) =>
[DiffSig repr]
-> [Category]
-> [Category]
-> [DiffSig repr]
go acc [] [] = acc
go acc xs [] = acc ++ map (Diff.remove cardname . view Category.name) xs
go acc [] ys = acc ++ map (uncurry (Diff.add cardname) . mkTuple) ys
go acc lx@(x : xs) ly@(y : ys) =
case (compare `on` view Category.name) x y of
LT -> go (Diff.remove cardname (x ^. Category.name) : acc) xs ly
EQ ->
go
( Diff.chunk cardname [Diff.deltaCopies (diffCopies x y) (x ^. Category.name)]
: acc
)
xs
ys
GT -> go (Diff.add cardname (y ^. Category.name) (y ^. copies) : acc) lx ys
mkTuple x = (x ^. Category.name, x ^. copies)
diffCopies x y = uncurry (-) $ (y ^. copies, x ^. copies) & both %~ toNumber
cardname = card ^. Deck.name
diff ::
(HunkSYM repr, DeckSYM repr) => Map Text Card -> Map Text Card -> [DiffSig repr]