Last active
July 23, 2018 00:24
-
-
Save chrisdone/6b99d27f666aeea71cc532d2543a6901 to your computer and use it in GitHub Desktop.
Origamic fold
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
-- | Fold over the input, folding left or right depending on the element. | |
origami :: (s -> l -> s) -> (r -> s -> s) -> s -> [Either l r] -> s | |
origami _ _ nil [] = nil | |
origami fl fr nil (x:xs) = | |
case x of | |
Left l -> origami fl fr (fl nil l) xs | |
Right r -> fr r (origami fl fr nil xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about this:
Then your original function is equivalent to:
But we can just as easily tie the knot the other way
Whereas
origami
is semantically equivalent tofoldr fr (foldl fl s as) bs
,origami'
has the semantics offoldl fl (foldr fr s bs) as
where(as, bs) = partitionEithers xs
.