Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more implementations to Function0 and Function1 Monads #3515

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/instances/function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ sealed private[instances] trait Function0Instances extends Function0Instances0 {

def pure[A](x: A): () => A = () => x

override def map[A, B](fa: () => A)(fn: A => B): () => B =
() => fn(fa())

override def map2[A, B, C](fa: () => A, fb: () => B)(fn: (A, B) => C): () => C =
() => fn(fa(), fb())

override def product[A, B](fa: () => A, fb: () => B): () => (A, B) =
() => (fa(), fb())

override def ap[A, B](f: () => A => B)(fa: () => A): () => B =
() => f()(fa())

def flatMap[A, B](fa: () => A)(f: A => () => B): () => B =
() => f(fa())()

Expand Down Expand Up @@ -122,6 +134,15 @@ sealed private[instances] trait Function1Instances extends Function1Instances0 {
override def map[R1, R2](fa: T1 => R1)(f: R1 => R2): T1 => R2 =
f.compose(fa)

override def map2[A, B, C](fa: T1 => A, fb: T1 => B)(fn: (A, B) => C): T1 => C =
t => fn(fa(t), fb(t))

override def product[A, B](fa: T1 => A, fb: T1 => B): T1 => (A, B) =
t => (fa(t), fb(t))

override def ap[A, B](f: T1 => A => B)(fa: T1 => A): T1 => B =
t => f(t).apply(fa(t))

def tailRecM[A, B](a: A)(fn: A => T1 => Either[A, B]): T1 => B =
(t: T1) => {
@tailrec
Expand Down