Created
December 16, 2011 15:32
-
-
Save halcat0x15a/1486481 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 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]) | |
} | |
nel(1, 2, 3).traverse(_ +>: 1.some) assert_=== Some(NonEmptyList(2, 3, 4)) | |
ScalaChan(10).traverse(_ +>: 1.wrapNel) assert_=== NonEmptyList(ScalaChan(10), ScalaChan(1)) | |
1.some.traverse(_ +>: nel(1, 2, 3)) assert_=== NonEmptyList(Some(1), 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
sequence :: Monad m => [m a] -> m [a] | |
sequence ms = foldr k (return []) ms | |
where | |
k m m' = do { x <- m; xs <- m'; return (x:xs) } |
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
def sequence[M[_]: Monad, A](xs: List[M[A]]): M[List[A]] = { | |
def k(m: M[A], n: => M[List[A]]): M[List[A]] = for { | |
x <- m | |
xs <- n | |
} yield x :: xs | |
xs.foldr(nil[A].pure[M])(k) | |
} | |
sequence(List(1.some, 2.some, 3.some)) assert_=== Some(List(1, 2, 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
def sequence[M[_]: Applicative, A](xs: List[M[A]]): M[List[A]] = { | |
def k(m: M[A], n: => M[List[A]]): M[List[A]] = (m <**> n)(_ :: _) | |
xs.foldr(nil[A].pure[M])(k) | |
} | |
sequence(List(1.some, 2.some, 3.some)) assert_=== Some(List(1, 2, 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
sequence(List(1.some, 2.some, 3.some)) assert_=== List(1.some, 2.some, 3.some).sequence | |
List(1, 2, 3).some.sequence assert_=== List(1.some, 2.some, 3.some) | |
List(1, 2, 3).some.traverse(identity) assert_=== List(1, 2, 3).some.sequence |
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
nel(1, 2, 3).foldMapDefault(_.shows) assert_=== "123" | |
none[Int].foldMapDefault(_ |+| 100) assert_=== 0 |
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(1, 2, 3).collapse assert_=== 6 | |
none[String].collapse assert_=== "" |
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
nel(1, 2).zipWithA(nel("Hello", "World"))(_.shows |+| _ |> Option.apply) assert_=== Some(NonEmptyList(1Hello, 1World, 2Hello, 2World)) | |
"a".some.zipWithA(1.some)((s, i) => s.parseInt.map(_ |+| i).toOption) assert_=== None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment