Functors in Scala


While programming in Scala we  often come across a term called Functor. A functor is an extremely simple but powerful concept. In this blog, let us discuss that in more detail.

Theoretically functor is a type of mapping between categories. Given two categories A and B, a functor F maps the objects or entities of A to objects or entities of B. We can simply call it a function of objects or entities.

In programming the functors come into play when we have types or values wrapped inside context or containers. This wrapping up inside context, blocks application of normal functions on the values. This happens because the result on application of function is dependent on the context.

The solution to above scenario is a function that knows how to apply functions to the values wrapped in a context. Internally speaking this function should have a potential to

  • unwrap(fetch) the value from the context
  • apply the function onto the value
  • re-wrap the resultant into context

A Functor is defined by a type Constructor F[_] together with a function

                                  map :(A=>B) => F[A]=>F[B]

where the following holds true :

  1. map identity means the same thing as identity
  2. (map f) compose (map g) is equivalent of map ( f compose g)

Functor(s) are not in-built in Scala i.e as abstractions they do not exist in Scala but functor like behavior is build into Scala. The invocations of map on various types in scala is consistent with the definition of Functors. The example below illustrates the point :

Example :

one

But certain libraries like Scalaz provide us with concrete concept of Functors. Scalaz provides us with a type class Functor[F[_]] which defines a map as

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

along with a trait Functor which formulates the above stated laws.

Let us look at an implementation with an example:

First of all we need to import the Scalaz

import scalaz.Functor

case class Container[A](first:A,second:A)

implicit val demoFunctor= new Functor[Container]{

   def map[A,B](fa:Container[A])(f:A=>B):Container[B]=Container(f(fa.first),f(fa.second))

}

There are many more example of Functors like Options, Streams ,List etc

implicit val OptionFunctor =new Functor[Option]{

    def fmap[A,B](f:A=>B):Option[A]=>Option[B]=option =>option map f

}

implicit val ListFunctor =new Functor[List]{

    def fmap[A,B](f:A=>B):List[A]=>List[B]=list =>list map f

}

In my next blog I shall be discussing more about Functors and Applicatives.

Happy Reading !

References

KNOLDUS-advt-sticker

This entry was posted in Scala and tagged , , , , , . Bookmark the permalink.

4 Responses to Functors in Scala

  1. Pingback: Applicatives in Scala | Knoldus

  2. Pingback: Applicatives in Scala – pallavisingh1992blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s