Adrian Adrian -3 years ago 111
Scala Question

What methods are needed to create custom Applicative Functor to use with scalaz |@|

I would like to be able to do use scalaz's |@| on my own applicative functor.

Example:

val spread: Source[Yield] = (y2 |@| y1)(_ - _)


This is my class

sealed abstract class Source[+A] {
def map[B](f: A => B): Source[B]
def unit[A](a: A): Source[A]
def pureList[A](la: List[A]): Source[A]
def zero[A]: Source[A]
def map2[A, B, C](as: Source[A], bs: Source[B], f: (A, B) => C): Source[C]
}


I'm certain I have to implement
map
because it's a functor.

An applicative can be implemented in various ways: for example using
apply()
and
unit()
or
map2()
and
unit()
.

Do I need
ap
and
pure
as well?

As you can see I'm not sure what is needed.

Answer Source
implicit val mya = new Applicative[Source] {}

Let the compiler answer that question for you:

object creation impossible, since:
it has 2 unimplemented members.
/** As seen from <$anon: scalaz.Applicative[Source]>, the missing signatures are as follows.
 *  For convenience, these are usable as stub implementations.
 */
  // Members declared in scalaz.Applicative
  def point[A](a: => A): Source[A] = ???

  // Members declared in scalaz.Apply
  def ap[A, B](fa: => Source[A])(f: => Source[A => B]): Source[B] = ???
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download