This file contains 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
implicit lazy val IkamusumeEach: Each[Ikamusume] = new Each[Ikamusume] { | |
def each[A](e: Ikamusume[A], f: A => Unit): Unit = e.value.foreach(f) | |
} | |
Ikamusume(2, 3, 5, 7, 11).foreach(print) | |
Ikamusume(1, 1, 2, 3, 5) |>| print | |
scala> implicit lazy val IkamusumeEach: Each[Ikamusume] = new Each[Ikamusume] { | |
| def each[A](e: Ikamusume[A], f: A => Unit): Unit = e.value.foreach(f) | |
| } | |
IkamusumeEach: scalaz.Each[Ikamusume] = <lazy> |
This file contains 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
case class ScalaChan[A](value: A) | |
implicit def ScalaChanEqual[A]: Equal[ScalaChan[A]] = equalA | |
implicit def ScalaChanShow[A]: Show[ScalaChan[A]] = showA | |
implicit lazy val ScalaChanTraverse: Traverse[ScalaChan] = new Traverse[ScalaChan] { | |
def traverse[F[_] : Applicative, A, B](f: A => F[B], t: ScalaChan[A]): F[ScalaChan[B]] = f(t.value).map(ScalaChan[B]) | |
} |
This file contains 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
from functools import partial | |
from maybe import * | |
class Input(object): | |
def input(self, el, empty, eof): | |
if self == Empty: | |
return empty | |
elif self == EOF: |
This file contains 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
scala> def time[A](f: => A): A = { | |
| val s = System.nanoTime | |
| val x = f | |
| val e = System.nanoTime | |
| (((e - s) / 1000).toDouble / 1000000).println | |
| x | |
| } | |
time: [A](f: => A)A | |
scala> object Fib { |
This file contains 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
import scalaz._ | |
import Scalaz._ | |
import scala.util.parsing.combinator._ | |
object KanjiNumberParser extends RegexParsers { | |
def one = "一" ^^^ digits(1) | |
def two = "二" ^^^ digits(2) | |
def three = "三" ^^^ digits(3) | |
def four = "四" ^^^ digits(4) |
This file contains 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
import Digit._ | |
_1.toInt assert_=== 1 | |
_9.toChar assert_=== '9' | |
_0.toLong assert_=== 0L |
This file contains 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
import IterV._ | |
implicit lazy val StreamEnumerator: Enumerator[Stream] = new Enumerator[Stream] { | |
def apply[E, A](e: Stream[E], i: IterV[E, A]): IterV[E, A] = e match { | |
case Stream() => i | |
case x #:: xs => i.fold(done = (_, _) => i, cont = k => apply(xs, k(El(x)))) | |
} | |
} | |
head(Stream(3, 4, 6)).run assert_=== Some(3) |
This file contains 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
lazy val f: Int => Int = _ + 2 | |
lazy val g: Int => Int = _ * 2 | |
(f >>> g).apply(5) assert_=== 14 | |
(f <<< g).apply(5) assert_=== 12 | |
(f.promise >>> g.promise).apply(3).get assert_=== (f.promise >=> g.promise).apply(3).get |
This file contains 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
Generator.FoldrGenerator[List].from(StreamReducer[String], List("Scala", "Scalaz"), Stream.empty) assert_=== Stream("Scala", "Scalaz") | |
Generator.FoldlGenerator[List].to(FirstReducer[String], mzero[FirstOption[String]], List("Scala", "Scalaz")) assert_=== "Scala".pure[FirstOption] | |
Generator.FoldMapGenerator[List].reduce(LastReducer[String], List("Scala", "Scalaz")) assert_=== "Scalaz".pure[LastOption] |
This file contains 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
case class ScalaChan[A](values: A*) | |
implicit def ScalaChanFoldable = new Foldable[ScalaChan] { | |
override def foldMap[A, M: Monoid](t: ScalaChan[A], f: (A) => M): M = t.values.map(f).fold(mzero[M])(_ |+| _) | |
} | |
ScalaChan(1, 2, 3, 4).foldMap(identity) assert_=== 10 |