Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Last active October 8, 2018 14:49
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 sjoerdvisscher/f36676a8ba88fcfa86fce389255b74dd to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/f36676a8ba88fcfa86fce389255b74dd to your computer and use it in GitHub Desktop.
Monoid Comprehensions
{-# LANGUAGE MonadComprehensions, RebindableSyntax #-}
import Prelude hiding (return, (>>=), (>>), guard)
return :: a -> a
return = id
(>>=) :: (Foldable f, Monoid m) => f a -> (a -> m) -> m
(>>=) = flip foldMap
guard :: Bool -> Bool
guard = id
(>>) :: Monoid m => Bool -> m -> m
(>>) True m = m
(>>) False _ = mempty
query 
:: (Monoid m, Foldable f1, Foldable f2)
=> (a -> b -> Bool) -> (a -> b -> m) -> f1 a -> f2 b -> m
query f g xs ys = [ g x y | x <- xs, y <- ys, f x y ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment