Skip to content

Instantly share code, notes, and snippets.

@klapaucius
Last active December 26, 2015 12:29
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 klapaucius/7152074 to your computer and use it in GitHub Desktop.
Save klapaucius/7152074 to your computer and use it in GitHub Desktop.
J Applicative
{-# LANGUAGE FlexibleInstances #-}
import Control.Applicative
newtype J a = J { runJ :: a }
instance (Applicative f, Num n) => Num (J(f n)) where
(+) = (J .) . liftA2 (+) `on` runJ
(-) = (J .) . liftA2 (-) `on` runJ
(*) = (J .) . liftA2 (*) `on` runJ
abs = J . fmap abs . runJ
signum = J . fmap signum . runJ
fromInteger = J . pure . fromIntegral
runJ $ J[1..5] + J[2]
[3,4,5,6,7]
getZipList.runJ $ J(ZipList[1..5]) + J(ZipList[2..6])
[3,5,7,9,11]
runJ (J length + J sum) $ [1..10]
65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment