Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created December 23, 2011 07:34
Show Gist options
  • Save halcat0x15a/1513492 to your computer and use it in GitHub Desktop.
Save halcat0x15a/1513492 to your computer and use it in GitHub Desktop.
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
nel("Hello", "Scalaz").foldr(none[String])(_.some |+| _) assert_=== Some("HelloScalaz")
List('Scalaz, 'Scala).foldl('Scalaちゃん.wrapNel)(_ |+| _.wrapNel) assert_=== NonEmptyList('Scalaちゃん, 'Scalaz, 'Scala)
nil[Int].foldl1(_ * _) assert_=== None
nel(1, 2, 3).foldr1(_ * _) assert_=== Some(6)
nel(3, 4, 6).foldRightM(6)((a, b) => b +>: a.wrapNel) assert_=== NonEmptyList(6, 3, 4, 3, 6, 3, 4, 3)
nel("10", "Scala").foldLeftM(10)((i, s) => s.parseInt.map(_ |+| i).toOption) assert_=== None
nel(1, 3, 5).all(_ % 2 /== 0) assert_=== true
nel(1, 3, 6) ∀ (_ % 2 /== 0) assert_=== false
none[Int].any(_ gt 0) assert_=== false
1.some ∃ (_ gt 0) assert_=== true
List(10, 20, 30).element(30) assert_=== true
List(10, 11, 12) ∋ 13 assert_=== false
11 ∈: List(10, 11, 12) assert_=== true
nil[String].empty assert_=== true
nel(1, 2, 4).count assert_=== 3
100.some.foldIndex(0) assert_=== 100
nel(1, 2, 3).listr assert_=== List(1, 2, 3)
'Scalaz.some.listr assert_=== List('Scalaz)
"Hello".show.stream.take(3) assert_=== Stream('H', 'H', 'H')
"Hello".some.stream.take(3) assert_=== Stream("Hello")
"Scalaz".show.maximum assert_=== Some('z')
none[Int].maximum assert_=== None
nel(3, 2, 1, 2, 3).minimum assert_=== Some(1)
nil[String].minimum assert_=== None
nel(0, 1, 1, 2, 3, 5).selectSplit(_ % 2 /== 0) assert_=== List(List(1, 1), List(3, 5))
346.some.selectSplit(_ === 345) assert_=== Nil
import Digit._
11235L.digits.splitWith(_.toInt % 2 === 0) assert_=== List(List(_5, _3), List(_2), List(_1, _1))
"Scalaちゃん".show.splitWith(_ === 'a') assert_=== List(List('S', 'c'), List('a'), List('l'), List('a'), List('ち', 'ゃ', 'ん'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment