- Functors can only be defined over types that are
[*] => *
- Laws:
- Identity law (identity function):
map(fa, identity) = fa
- Composition:
map(map(fa,g), f) = map(fa, g andThen f)
orfa.map(g andThen f) = fa.map(g).map(f)
- Identity law (identity function):
- Functors compose very well
- product of two functors is a functor
- Sum of two functors is a functor
- Nested functors is a functor
- Functor preserves the structure (container type). For an example a list order can’t change.
- Functor doesn’t need to be a data structure, it could be a function. Lots of data structures are functors but not all functors are data structures.
- Functors doesn’t allow you to combine programs but only to change the return type.
1
2
3
trait functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
We can think of functors as a type of a programming language. For a functor F[_]
F
: A sum type of different termsF[A]
: is a program in F. That is, its instructions in theF
sum type. This program may halt of produce one or moreA
value(s).- Map: is the ability to change the output of a parser by replacing the value captured by the program
a
byf(a)
, wheref: A => B
Examples
- Option
None
corresponds to a haltSome
Emits a value
- List
Nil
corresponds to a halt.Cons(head, tail)
recursive: Emits and then continue to the next element.
Notes
Nothing
has a poly-kinded… that is it satisfies[*] => *
and*
and[*, *] => *
, etc.
Comments powered by Disqus.