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 flap function to Apply #2588

Merged
merged 5 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ trait AllSyntaxBinCompat2
with ListSyntaxBinCompat0
with ValidatedSyntaxBincompat0

trait AllSyntaxBinCompat3 extends UnorderedFoldableSyntax
trait AllSyntaxBinCompat3 extends UnorderedFoldableSyntax with Function1Syntax
33 changes: 33 additions & 0 deletions core/src/main/scala/cats/syntax/function1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cats
package syntax

trait Function1Syntax {

implicit def catsSyntaxFunction1[F[_]: Functor, A, B](fab: F[Function1[A, B]]): Function1Ops[F, A, B] =
new Function1Ops[F, A, B](fab)

final class Function1Ops[F[_]: Functor, A, B](fab: F[Function1[A, B]]) {

/**
* Given a function in the Functor context and a plain value, supplies the
* value to the function.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val someF: Option[Int => Long] = Some(_.toLong + 1L)
* scala> val noneF: Option[Int => Long] = None
* scala> val anInt: Int = 3
*
* scala> someF.mapApply(anInt)
* res0: Option[Long] = Some(4)
*
* scala> noneF.mapApply(anInt)
* res1: Option[Long] = None
*
* }}}
*/
def mapApply(a: A): F[B] = Functor[F].map(fab)(_(a))
}
}