Skip to content

Instantly share code, notes, and snippets.

@Lifelovinglight
Last active December 22, 2018 07:25
Show Gist options
  • Save Lifelovinglight/bb6ba765d78b8f5022738e2784b76565 to your computer and use it in GitHub Desktop.
Save Lifelovinglight/bb6ba765d78b8f5022738e2784b76565 to your computer and use it in GitHub Desktop.
stupid juggling
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