Design Patterns
- What is a functor?
A functor defines data, accepts a function, and returns a transformation of the data.
- What is a contravariant functor?
A contravariant functor is a functor where the accepted function may produce 0, 1, or many return values. By comparison, functor's accepted functions must return exactly 1 value.
- What is a monad?
A monad, parameterized by a single type A
, is a value that has a trait exposing two operations, usually named return
and bind
. return
is a function that constructs a new monad<A>
from a provided A
value. bind
should incorporate new information to produce a related but separate monad<B>
.
- What are the monad laws?
These equivalencies must hold for strict monads. The three horizontal bars means equivalence:
_return(v).bind(f) ≡ f(v) m.bind(_return) ≡ m m.bind(f).bind(g) ≡ (|x| f(x).bind(g))
- What is a combinator?
A functional combinator combines functions. A combinator more generator combines things.
- Why is the impl keyword necessary for closure return values?
Closures are traits, not types. Therefore they do not have a size known at compile time. impl
for a return type tells the compiler to parameterize the return type.
- What is lazy evaluation?
Lazy evaluation is when computation is delayed until some point in the future. This is compared to eager evaluation, where computation occurs immediately.