Applicative functions like map2, product, etc are defined in terms of flatMap
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 5.5k
- Forks
- 1.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 5
Description
in FlatMap, you'll find the following implementation for various functions:
@typeclass trait FlatMap[F[_]] extends Apply[F] {
...
override def ap[A, B](ff: F[A => B])(fa: F[A]): F[B] =
flatMap(ff)(f => map(fa)(f))
override def product[A, B](fa: F[A], fb: F[B]): F[(A, B)] =
flatMap(fa)(a => map(fb)(b => (a, b)))
override def ap2[A, B, Z](ff: F[(A, B) => Z])(fa: F[A], fb: F[B]): F[Z] =
flatMap(fa)(a => flatMap(fb)(b => map(ff)(_(a, b))))
override def map2[A, B, Z](fa: F[A], fb: F[B])(f: (A, B) => Z): F[Z] =
flatMap(fa)(a => map(fb)(b => f(a, b)))
override def productR[A, B](fa: F[A])(fb: F[B]): F[B] =
flatMap(fa)(_ => fb)
override def productL[A, B](fa: F[A])(fb: F[B]): F[A] =
map2(fa, fb)((a, _) => a)
This makes it such that whenever I define a Monad that has a parallel implementation of ap, none of the other functions will have the parallel behaviour!
Is there a reason these methods are defined in terms of flatMap and not in terms of ap?
For example, I would expect map2 to be implemented as:
override def map2[A, B, Z](fa: F[A], fb: F[B])(f: (A, B) => Z): F[Z] =
fa.map(a => f.curried.apply(a)).ap(fb)
so that it automatically inherits parallel behaviour of ap
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the FlatMap typeclass and the implementations shown in the issue, then compare their behavior with the parallel ap implementation described there. Done means the intended behavior is decided and either the relevant methods are updated consistently or the rationale for keeping them is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100