Functors in Scala

Table of contents
Reading Time: 2 minutes

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

Written by 

Pallavi is a Software Consultant, with more than 3 years of experience. She is very dedicated, hardworking and adaptive. She is Technology agnostic and knows languages like Scala and Java. Her areas of interests include microservices, Akka, Kafka, Play, Lagom, Graphql, Couchbase etc. Her hobbies include art & craft and photography.

4 thoughts on “Functors in Scala2 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading