Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with 'lifting' functions in Scala

In the book Functional Programming In Scala, there's an example of 'Lift' where a function with type A => B is promoted to Option[A] => Option[B].

This is how lift is implemented:

def lift[A,B](f: A => B):Option[A] => Option[B] = _ map f

I have a couple of confusions regarding this:

The first one is, what is the '_' here? And secondly, when I remove the return type from the def, expecting the type-inference to do its magic I get the following exception:

scala> def lift[A,B](f: A => B) =  _ map f
<console>:7: error: missing parameter type for expanded function ((x$1) => x$1.map(f))
       def lift[A,B](f: A => B) =  _ map f

Can somebody explain what's going on here?

Thanks

like image 568
sc_ray Avatar asked Jun 22 '13 23:06

sc_ray


1 Answers

  1. lift is a function that returns a function. The function returned lifts a value (unnamed) by applying the function f to that value. The unnamed value to be lifted is referred to as _. You can certainly give it a more explicit name:

    def lift[A,B](f: A => B): Option[A] => Option[B] = { value => value map f }
    
  2. The return type of that function (the one being returned) needs to be either stated explicitly or determined implicitly. As written the compile can infer that what is to be returned is an Option[B] (more specifically, lift is returning a function Option[A] => Option[B] (explicitly stated), while that function has return type Option[B] (determined implicitly)). Without that type information, the compiler needs some other indication of what the return type(s) are.

    Alternately, define lift thus:

    def lift[A,B](f: A => B) = { value: Option[A] => value map f }
    

    Here you're explicitly stating the type for value, and the compiler can infer

    • the return type of the function returned to be Option[B], because f: A => B will map type A to B,
    • the return type for lift to be Option[A] => Option[B].
like image 138
Richard Sitze Avatar answered Nov 11 '22 00:11

Richard Sitze