Skip to content

Instantly share code, notes, and snippets.

@PiDelport
Last active August 18, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiDelport/f24f1a92e58f21f73c38a85d15781302 to your computer and use it in GitHub Desktop.
Save PiDelport/f24f1a92e58f21f73c38a85d15781302 to your computer and use it in GitHub Desktop.
-- 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
@PiDelport
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment