Last active
December 22, 2018 07:25
-
-
Save Lifelovinglight/bb6ba765d78b8f5022738e2784b76565 to your computer and use it in GitHub Desktop.
stupid juggling
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
import Control.Applicative | |
import Data.Monoid | |
import Control.Monad | |
import Control.Monad.Loops | |
main :: IO () | |
main = putStrLn =<< intersperse ':' <$> getLines | |
-- | The class of types that support short-circuiting on e.g. empty, false, error - almost the same as but not quite Alternative | |
class Circuit a where | |
(<||>) :: a -> a -> a | |
instance Circuit [a] where | |
[] <||> b = b | |
a <||> _ = a | |
instance Circuit (Maybe a) where | |
Nothing <||> b = b | |
a <||> _ = a | |
instance Circuit (Either a b) where | |
(Left _) <||> b = b | |
ea@(Right a) <||> _ = ea | |
getLines :: IO [String] | |
getLines = unfoldWhileM (/= mempty) getLine | |
separate :: (Monoid (f a), Applicative f) => a -> f a -> f a -> f a | |
separate a fax fay = fax <> pure a <> fay | |
intersperse :: (Monoid (f a), Foldable t, Applicative f, Applicative t, Alternative t, Circuit (t (f a))) => a -> t (f a) -> f a | |
intersperse a as = foldl1 (separate a) (as <||> pure mempty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment