Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Last active July 8, 2025 23:41
Show Gist options
  • Save tonymorris/dfc75f703ae2efbaa6645e696e5d7665 to your computer and use it in GitHub Desktop.
Save tonymorris/dfc75f703ae2efbaa6645e696e5d7665 to your computer and use it in GitHub Desktop.
import Data.Bool
filter' ::
(a -> Either b c)
-> [a]
-> ([b], [c])
filter' _ [] =
([], [])
filter' f (a:as) =
let (bs, cs) = filter' f as
in case f a of
Left b ->
(b:bs, cs)
Right c ->
(bs, c:cs)
filter'' ::
(a -> Maybe b)
-> [a]
-> [b]
filter'' f as =
snd (filter' (maybe (Left ()) Right . f) as)
-- You think you want this.
-- But you don't. You want something like the above.
filter''' ::
(a -> Bool)
-> [a]
-> [a]
filter''' f =
filter'' (\a -> bool Nothing (Just a) (f a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment