I want to define lifting with implicits. Suppose we have a function A => B, I want to define how to lift it to Maybe, i.e. Maybe[A] => Maybe[B].
This can be simply done with implicit conversions. However, if I want to do the same with functions with two or more parameters, I have a problem. The only solutionI know is to duplicate the code.
I want to implement such lifting for arbitrary functions with any number of parameters without duplication. Is this possible in Scala?
If
Fhas a functor instance available, it’s possible to lift any functionA => BtoF[A] => F[B].If
Fhas an applicative functor instance available, it’s possible to lift any functionA => B => C => .. => ZtoF[A] => F[B] => F[C] => .. => F[Z]. Essentially, applicative functor is a generalization of functor for arbitrary arity.You can learn about functor and applicative functors here and here. There is also this excellent talk which covers these ideas.
Scalaz library provides these abstractions (and more!).
Scalaz pimps
liftmethod onFunction2,Function3etc because curried functions being syntactially heavier are used less often. Behind the scenes, the lifting happens withFunction1s (i.e. curried functions).You might also want to take a look at Scalaz source code.