Last active
December 26, 2015 12:29
-
-
Save klapaucius/7152074 to your computer and use it in GitHub Desktop.
J Applicative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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