Created
December 7, 2011 05:29
-
-
Save halcat0x15a/1441584 to your computer and use it in GitHub Desktop.
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 ScalaChanFunctor: Functor[ScalaChan] = new Functor[ScalaChan] { | |
def fmap[A, B](a: ScalaChan[A], f: A => B): ScalaChan[B] = ScalaChan(f(a.value)) | |
} | |
implicit lazy val ScalaChanBind: Bind[ScalaChan] = new Bind[ScalaChan] { | |
def bind[A, B](a: ScalaChan[A], f: A => ScalaChan[B]): ScalaChan[B] = f(a.value) | |
} | |
lazy val scalaChan = for { | |
i <- ScalaChan(7) | |
s <- ScalaChan("Scalaz") | |
} yield s |+| i.shows | |
scalaChan assert_=== ScalaChan("Scalaz7") |
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
List.fill(2, 2)(100).flatMap(1 +>: _) assert_=== List(1, 100, 100, 1, 100, 100) | |
(1.some.pure[Option] >>= (1 +>: _)) assert_=== 2.some | |
1.some.fpure[List] >>=| 2.some assert_=== 2.some | |
1.pure[Tuple1].fpure[List].fpure[Option] >|> none[Int].pure[Tuple1] assert_=== Tuple1(None) |
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> try { ((_: Int) + 1).forever[Int].apply(1) } catch { case e => e } | |
res75: Any = java.lang.StackOverflowError | |
scala> try { 1.pure[Stream].forever[Stream[Int]] } catch { case e => e } | |
res76: java.lang.Object = java.lang.StackOverflowError |
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
List(3, 4, 6).replicate[List](2).join assert_=== List(3, 4, 6, 3, 4, 6) | |
lazy val f: Int => Int => Int = i => j => i |+| j | |
(f μ).apply(5) assert_=== 10 |
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 Ikamusume[A](value: A) | |
implicit def IkamusumeEqual[A]: Equal[Ikamusume[A]] = equalA | |
implicit def IkamusumeShow[A]: Show[Ikamusume[A]] = showA | |
implicit def IkamusumeFunctor: Functor[Ikamusume] = new Functor[Ikamusume] { | |
def fmap[A, B](r: Ikamusume[A], f: A => B): Ikamusume[B] = Ikamusume(f(r.value)) | |
} | |
def mapAppend[M[_]: Functor, A: Semigroup](m: M[A], a: A) = m.map(_ |+| a) | |
mapAppend(List(1, 2, 4), 2) assert_=== List(3, 4, 6) | |
mapAppend(Ikamusume(100), 1) assert_=== Ikamusume(101) |
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
(1, 2).map(1 +) assert_=== (1, 3) | |
((_: Int) + 1).map(2 *).apply(5) assert_=== 12 | |
List(1.some, 2.some, 4.some).map2[Option, Int, Int](2 +) assert_=== List(3.some, 4.some, 6.some) | |
List(List(1, 2, 3), List(2, 2, 2)).map2[List, Int, Int](2 *) assert_=== List(2.replicate[List](3, _ |+| 2), 4.replicate[List](3)) | |
1.replicate[Stream](5) >| 3 assert_=== 3.replicate[Stream](5) |
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
1.mapply((1 |+| (_: Int)).some) assert_=== 2.some | |
List(1).mapply(List(1 +>: (_: List[Int]), (_: List[Int]) |+| List(1, 1))) assert_=== List(List(1, 1), List(1, 1, 1)) |
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
1.some.fpair assert_=== Some((1, 1)) | |
List(1, 2, 3).fpair assert_=== List((1,1), (2,2), (3,3)) | |
1.some.fpure[List] assert_=== Some(List(1)) | |
List(1, 2, 3).fpure[Option] assert_=== List(Some(1), Some(2), 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
import Comp._ | |
List.fill(2, 2)(1).comp.map(10 *) assert_=== List.fill(2, 2)(10) | |
List(1).some.comp.fpure[Option] assert_=== List(1.some).some | |
Ikamusume(100).some.comp.map(2 *) assert_=== Ikamusume(200).some |
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
("Hello" |+| "World")(Semigroup.IntSemigroup.xmap(_.shows)(_.size)) assert_=== "10" | |
(1 |+| 1)(Semigroup.StringSemigroup.xmap(_.toInt)(_.shows)) assert_=== 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment