Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cheecheeo/7e54e193419158e4b7fe to your computer and use it in GitHub Desktop.
Save cheecheeo/7e54e193419158e4b7fe to your computer and use it in GitHub Desktop.
module ATComposeProductSum where
import Control.Applicative
import Data.Traversable
import Data.Functor.Compose
import Data.Functor.Product
import Data.Functor.Sum
import Data.Functor.Identity
instance (Applicative f, Applicative g) => Applicative (Sum f g) where
pure x = InR (pure x)
InL f <*> InL x = InL (f <*> x)
InR f <*> InR x = InR (f <*> x)
-- | Applicative Composes
-- >>> pure (+) <*> (Compose (replicate 10 (Just 41))) <*> pure 1
-- Compose [Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42]
--
-- Traversable Composes
-- >>> traverse (Identity . length) (Compose (replicate 10 (Just "foo")))
-- Identity (Compose [Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3])
--
-- Applicative Sums
-- >>> pure (+) <*> Pair (Just 41) (replicate 10 41) <*> pure 1
-- Pair (Just 42) [42,42,42,42,42,42,42,42,42,42]
--
-- Traversable Sums
-- >>> traverse (Identity . length) (Pair (Just "foo") (replicate 10 "foo"))
-- Identity (Pair (Just 3) [3,3,3,3,3,3,3,3,3,3])
--
-- Applicative Products
-- >>> (InL (Just (+1))) <*> InL (Just 41) :: Sum Maybe [] Integer
-- InL (Just 42)
-- >>> pure (+1) <*> InR (replicate 10 41) :: Sum Maybe [] Integer
-- InR [42,42,42,42,42,42,42,42,42,42]
--
-- Traversable Products
-- >>> traverse (Identity . (+1)) (InL (Just 41)) :: Identity (Sum Maybe [] Integer)
-- Identity (InL (Just 42))
-- >>> traverse (Identity . (+1)) (InR (replicate 10 41)) :: Identity (Sum Maybe [] Integer)
-- Identity (InR [42,42,42,42,42,42,42,42,42,42])
true = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment