Skip to content

Instantly share code, notes, and snippets.

@folone
Last active December 26, 2017 01:34
Show Gist options
  • Save folone/4421623 to your computer and use it in GitHub Desktop.
Save folone/4421623 to your computer and use it in GitHub Desktop.
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :l HappyNewYear.scala
Loading HappyNewYear.scala...
import scalaz._
import Scalaz._
defined class Happy
defined class NewYear
happyInstance: java.lang.Object with scalaz.Applicative[Happy] = $anon$1@4b6747e3
from: (year: Int)Happy[Int]
congratulation: Happy[NewYear]
bonusCongratulation: (java.lang.String, Int)
scala> congratulation
res0: Happy[NewYear] = Happy(NewYear(2013))
scala> bonusCongratulation
res1: (java.lang.String, Int) = (Happy New Year,2013)
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 {
case Happy(x) ⇒ Happy(f(x))
}
}
}
def from(year: Int): Happy[Int] = (year + 1).point[Happy]
def congratulation: Happy[NewYear] =
from(2012) <*> Happy(NewYear)
def bonusCongratulation = ("Year", 3) <*> (("New ", 671) <*> ("Happy ", ((_: Int) * (_: Int)) curried))
@svanlaug
Copy link

this is gr8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment