#Monoid in Scala by programmers from different backgrounds#
In spirit of The Evolution of a Haskell Programmer. Originally stolen from isoresursive.
| import scalaz._, Scalaz._ | |
| case class Writer[W: Monoid, +A](run: (A, W)) { | |
| def map[B](f: A ⇒ B): Writer[W, B] = | |
| Writer { | |
| val (a, w) = run | |
| (f(a), w) | |
| } | |
| def flatMap[B](f: A ⇒ Writer[W, B]): Writer[W, B] = |
| case class Reader[T, +A](run: T ⇒ A) { | |
| def map[B](f: A ⇒ B): Reader[T, B] = | |
| Reader((r: T) ⇒ f(run(r))) | |
| def flatMap[B](f: A ⇒ Reader[T, B]): Reader[T, B] = | |
| Reader((r: T) ⇒ f(run(r)).run(r)) | |
| def &&&[B](x: Reader[T, B]): Reader[T, (A, B)] = | |
| for { | |
| a ← this |
| import Control.Applicative | |
| data NewYear = NewYear Int | |
| deriving (Eq, Show) | |
| data Happy a = Happy a | |
| deriving (Eq, Show) | |
| instance Functor Happy where | |
| fmap f (Happy a) = Happy $ f a |
| import scalaz._, Scalaz._ | |
| case class Happy[A](a: A) | |
| case class NewYear(year: Int) | |
| implicit val happyInstance = new Applicative[Happy] { | |
| def point[A](a: ⇒ A) = Happy(a) | |
| def ap[A, B](fa: ⇒ Happy[A])(f: ⇒ Happy[A => B]) = | |
| f match { | |
| case Happy(f) ⇒ fa match { |
| % ghci | |
| GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help | |
| Loading package ghc-prim ... linking ... done. | |
| Loading package integer-gmp ... linking ... done. | |
| Loading package base ... linking ... done. | |
| Prelude> let n = map show [1..10] | |
| Prelude> let c = \x y -> concat ["(", x, "+", y, ")"] | |
| Prelude> foldl c "0" n | |
| "((((((((((0+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)" | |
| Prelude> foldr c "0" n |
| shapeless [0849b6f...] % sbt | |
| [info] Loading project definition from /home/folone/workspace/shapeless/project | |
| [info] Set current project to shapeless (in build file:/home/folone/workspace/shapeless/) | |
| > shapeless-core/console | |
| [warn] Credentials file /home/folone/.ivy2/.credentials does not exist | |
| [info] Compiling 2 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes... | |
| [info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes... | |
| [info] Starting scala interpreter... | |
| [info] | |
| Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09). |
| > shapeless-core/console | |
| [warn] Credentials file /home/folone/.ivy2/.credentials does not exist | |
| [info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes... | |
| [info] Starting scala interpreter... | |
| [info] | |
| Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09). | |
| Type in expressions to have them evaluated. | |
| Type :help for more information. | |
| scala> import shapeless._ |
| > shapeless-core/console | |
| [warn] Credentials file /home/folone/.ivy2/.credentials does not exist | |
| [info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes... | |
| [info] Starting scala interpreter... | |
| [info] | |
| Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09). | |
| Type in expressions to have them evaluated. | |
| Type :help for more information. | |
| scala> import shapeless.SingletonTypes._ |
#Monoid in Scala by programmers from different backgrounds#
In spirit of The Evolution of a Haskell Programmer. Originally stolen from isoresursive.
| type State[E] = Adjunction[({type λ[α] = Writer[E, α]})#λ, ({type λ[α] = Reader[E, α]})#λ] | |
| type Store[E] = Adjunction[({type λ[α] = Reader[E, α]})#λ, ({type λ[α] = Writer[E, α]})#λ] | |
| type Cont = Adjunction[Function0, Function0] // Function0 -| Function0 |