1

Following up from C++ Functors - and their uses, I have come across 'right biased functor' and 'left biased functor'. I have tried to do some research on my own, but I am still failing to understand the difference between both, and what functor bias even is.

Can someone please give an overview of what functor bias is, the difference between right bias and left bias, along with any examples of how this would be useful? It would be great if Scala could be used for the examples.

abhi
  • 1,760
  • 1
  • 24
  • 40
  • It's unfortunate that the word "functor" ended up used in such radically different ways in different communities. C++ "functors" are not Standard ML "functors" are not Scala/Haskell/category-theory "functors". – Seth Tisue Feb 13 '18 at 03:23

1 Answers1

4

Functor in scala isn't quite the same thing as what they call that in C++. It is a category with an operation like

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

that turns F[A] into F[B] given a transformer from A to B.

For example, an Option is a functor, that applies the transformation to the inner value when it is defined. A List is a functor, that applies the transformer to each element, etc.

Now, consider, something like Either[A, B]. Suppose, we wanted to define a functor for that. It takes two type parameters, but Functor only has one, so we have to choose on which the Functor will operate. It is common to use Either in a way, where Right is the default case, and Left is an exception (an error condition). So, it is natural, to consider it right-biased:

  def eitherFunctor[T] = new Functor[Either[T, ?]] {
     def map(fa: Either[T, A])(f: A => B): Either[T, B] = fa match {   
        case Right(a) => Right(f(a))
        case Left(t) => Left(t)
     }
  }

This works kinda like Option.map: when Either is a Right, it applies the transformer, otherwise, just returns the Left untouched. Thus, it can be said that Either is a right-biased functor, because it only operates on its right side.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • Thanks, makes much more sense. I was trying to play with the code you provided and was having a lot of trouble with compiling using an IDE or even with SBT via the command line. I figure it may be because we did not override `map` as the compiler would have expected? Also, wouldn't we need to specify our other type parameters like `?`, `A`, and `B` in `eitherFunctor` where `T` is specified? – abhi Feb 17 '18 at 06:09