Functors compose! Given any functor F[_]
and any functor G[_]
we can
create a new functor F[G[_]]
by composing them:
Functors compose! Given any functor F[_]
and any functor G[_]
we can
create a new functor F[G[_]]
by composing them:
val listOpt = Functor[List] compose Functor[Option]
In the previous example the resulting functor will apply the map
operation through the two
type constructors: List
and Option
.
We can use Functor
to "lift" a function from A => B
to F[A] => F[B]
:
We can use Functor
to "lift" a function from A => B
to F[A] => F[B]
:
val lenOption: Option[String] => Option[Int] = Functor[Option].lift(_.length) lenOption(Some("abcd"))
We can now apply the lenOption
function to Option
instances.
Functor
provides an fproduct
function which pairs a value with the
result of applying a function to that value.
Functor
provides an fproduct
function which pairs a value with the
result of applying a function to that value.
List
is a functor which applies the function to each element of the list:
List
is a functor which applies the function to each element of the list:
Functor[List].map(List("qwer", "adsfg"))(_.length)
Option
is a functor which only applies the function when the Option
value
is a Some
:
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
A
Functor
is a ubiquitous type class involving types that have one "hole", i.e. types which have the shapeF[?]
, such asOption
,List
andFuture
. (This is in contrast to a type likeInt
which has no hole, orTuple2
which has two holes (Tuple2[?,?]
)).The
Functor
category involves a single operation, namedmap
:This method takes a function
A => B
and turns anF[A]
into anF[B]
. The name of the methodmap
should remind you of themap
method that exists on many classes in the Scala standard library, for example:Creating Functor instances
We can trivially create a
Functor
instance for a type which has a well behavedmap
method:However, functors can also be created for types which don't have a
map
method. For example, if we create aFunctor
forFunction1[In, ?]
we can useandThen
to implementmap
:This example demonstrates the use of the kind-projector compiler plugin This compiler plugin can help us when we need to change the number of type holes. In the example above, we took a type which normally has two type holes,
Function1[?,?]
and constrained one of the holes to be theIn
type, leaving just one hole for the return type, resulting inFunction1[In,?]
. Without kind-projector, we'd have to write this as something like({type F[A] = Function1[In,A]})#F
, which is much harder to read and understand.