Skip to content

Instantly share code, notes, and snippets.

@Elvecent
Last active March 20, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elvecent/8864e747226c09cb47d4dffe687b79ef to your computer and use it in GitHub Desktop.
Save Elvecent/8864e747226c09cb47d4dffe687b79ef to your computer and use it in GitHub Desktop.
Selecting
import Control.Lens
import Data.Monoid
import Data.List.Extra
import Data.Foldable
select as fbs f s =
flip as s $ \a -> a <$ (
flip (fbs a) s $ \b ->
snd <$> f (a,b)
)
selecting as fbs =
(fmap.fmap $ fold) . groupSort . toListOf (select as fbs)
example :: [(Bool, Sum Int)]
example = ([True,False], [1..10])
& selecting (_1 . traversed)
( \b ->
_2 . traversed
. filtered ((==b) . even)
. to Sum
)
{- result:
[
( False
, Sum
{ getSum = 25 }
)
,
( True
, Sum
{ getSum = 30 }
)
]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment