Skip to content

Instantly share code, notes, and snippets.

@sshastry
Created July 25, 2015 23:57
Show Gist options
  • Save sshastry/9fc90361204564e1f684 to your computer and use it in GitHub Desktop.
Save sshastry/9fc90361204564e1f684 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Applicative
import Data.Maybe
import Data.Monoid
newtype X a = X { getX :: Maybe a } deriving (Eq, Ord, Read, Show, Functor, Applicative, Monad)
instance Monoid a => Monoid (X a) where
mempty = X (Just mempty)
X (Just x) `mappend` X (Just y) = X (Just (x `mappend` y))
X (Nothing) `mappend` _ = X Nothing
_ `mappend` X (Nothing) = X Nothing
g = X . Just . Sum
xs = map g [1..10]
ys = (map g [1..5]) ++ [X Nothing] ++ (map g [6..10])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment