Last active
March 20, 2021 10:02
-
-
Save Elvecent/8864e747226c09cb47d4dffe687b79ef to your computer and use it in GitHub Desktop.
Selecting
This file contains 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
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