Last active
July 8, 2025 23:41
-
-
Save tonymorris/dfc75f703ae2efbaa6645e696e5d7665 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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