Created
April 20, 2015 09:14
-
-
Save philipnilsson/fa7dc0dba708ef3f218b 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
| combine :: [Either a b] -> ((a,b) -> c) -> [c] | |
| combine xs f = combine' (Nothing, Nothing) xs | |
| where | |
| combine' (_, Just y) (Left l:xs) = | |
| f (l,y) : combine' (Just l, Just y) xs | |
| combine' (Just x, _) (Right l:xs) = | |
| f (x,l) : combine' (Just x, Just l) xs | |
| combine' (_, y) (Left l : xs) = | |
| combine' (Just l, y) xs | |
| combine' (x, _) (Right l : xs) = | |
| combine' (x, Just l) xs | |
| combine' _ [] = [] | |
| eithers = [Left 3, Right 10, Right 20, Right 30, Left 1] | |
| -- *Main> combine eithers (uncurry (+)) | |
| -- [13,23,33,31] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want to check out the Arrow Transformers library, particularly
Control.Arrow.Transformer.ArrowState:https://hackage.haskell.org/package/arrows-0.4.1.2/docs/Control-Arrow-Transformer-State.html
However it is possible to do implement your
combinefunction using ordinary State monads, with(Maybe Int, Maybe Int)as the state value, and usingControl.Monad.mapMto update the state with each item in the list. In the following example, the Arrow operators|||and***are used to unwrap theLeftorRightinput and apply it to the first or second element of the stateful tuple.