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 Applicative for Arrow #2078

Merged
merged 4 commits into from
Dec 10, 2017
Merged
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/arrow/Arrow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,25 @@ import simulacrum.typeclass
def split[A, B, C, D](f: F[A, B], g: F[C, D]): F[(A, C), (B, D)] =
andThen(first(f), second(g))
}
object Arrow {
/**
* Creates a semigroupal functor for `F`, holding domain fixed and combining
* over the codomain.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.arrow.Arrow.catsSemigroupalForArrow
* scala> val toLong: Int => Long = _.toLong
* scala> val double: Int => Int = 2*_
* scala> val f: Int => (Long, Int) = catsSemigroupalForArrow.product(toLong, double)
* scala> f(3)
* res0: (Long, Int) = (3,6)
* }}}
*/
implicit def catsSemigroupalForArrow[F[_, _], A](implicit F: Arrow[F]): Semigroupal[F[A, ?]] = new Apply[F[A, ?]] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably worthwhile to override product.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will do.

def map[B, C](fb: F[A, B])(f: B => C): F[A, C] = F.rmap(fb)(f)
def ap[B, C](ff: F[A, B => C])(fb: F[A, B]): F[A, C] =
F.rmap(F.andThen(F.lift((x: A) => (x, x)), F.split(ff, fb)))(tup => tup._1(tup._2))
}
}