Last active
August 18, 2016 13:47
-
-
Save PiDelport/f24f1a92e58f21f73c38a85d15781302 to your computer and use it in GitHub Desktop.
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
-- Sequence a list of Either-yielding monadic actions. | |
-- Sequencing stops on the first Left value, or yields a list successful Right values. | |
sequenceRights :: Monad m => [m (Either e t)] -> m (Either e [t]) | |
sequenceRights [] = pure (pure []) | |
sequenceRights (x:xs) = handle =<< x | |
where | |
handle (Left e) = pure (Left e) | |
handle (Right t) = (t:) <<$>> sequenceRights xs | |
-- This should be in a standard library somewhere. | |
(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b) | |
(<<$>>) = fmap . fmap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tangentially-related Gist: https://gist.github.com/jml/c308c351d2ccf39d03a421370d7e48df