0

For the following map signature, am I reading it correctly?

   object OptionImpl extends Option {
        def map[B](f: A => B): Option[B]
    }

source - FP in Scala

[B] means only objects of type B can call this function

f: A => B means that it accepts 1 argument, a function, that returns the same type B

I'm fuzzy on a concrete example of this function.

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • 1
    I don't see any `OptionImpl` in the whole book, and it makes absolutely no sense to subclass `Option` that way. There are only two subclasses of `Option`: `Some` and `None`. – Daniel C. Sobral Aug 28 '13 at 02:21
  • @Daniel, I'm referring to Chapter 4, Exercise 1 on page 58. `Implement the above functions on the trait, 'Option'` `trait Option[+A] { def map[B](f: A => B): Option[B] ... }` OptionImpl is the name I'm giving to my implementation of `Option` – Kevin Meredith Aug 28 '13 at 02:35
  • For the purposes of an exercise, "implement" doesn't mean you need to subclass anything. Just copy the `Option[+A]` trait and write the code there. – Ben James Aug 28 '13 at 07:08
  • @Kevin You should probably implement them on the trait itself, but, if you decide to implement it on subclasses, `Option` should be extended by the singleton `None` and the class `Some`, and only by them. Your `OptionImpl` would, then, be equivalent to `None`, and a `Some` class would still be required, and a corresponding implementation on it. – Daniel C. Sobral Aug 28 '13 at 14:02

2 Answers2

4

B is just a wildcard (i.e. generic). It just says that these two types are the same:

def map[B](f: A => B): Option[B]
                   ^          ^

That is, it says: if you pass me a function that converts As to Bs, I will give you back an Option that may contain a B (where B can be any type).

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • Thanks, Rex. Could you please give me an example of how an `Option` object would use the `map` function? – Kevin Meredith Aug 28 '13 at 01:08
  • 2
    @Kevin - No, because I don't have the book. But if `OptionImpl` were an `Option[String]`, you might use it as `OptionImpl.map(s => s.length)` to get an `Option[Int]` back (which would contain an `Int` if `OptionImpl` actually contained a string). – Rex Kerr Aug 28 '13 at 01:10
  • Would you expect this method to consist of `if (A is empty) None else f`? Your `Option[Int]` example helped me, but I'm trying to write an implementation of the trait in a generic way: `trait Option[+A] { def map[B](f: A => B): Option[B] }` – Kevin Meredith Aug 28 '13 at 01:41
  • @Kevin you can find solutions and hints for the exercises in the [book github repo](https://github.com/pchiusano/fpinscala). Enjoy your reading! – pagoda_5b Aug 28 '13 at 10:44
2

This is a very useful link http://blog.tmorris.net/posts/scalaoption-cheat-sheet/ on usage of Option.

If you have scenario like this

 option match {
   case None => None
   case Some(x) => Some(foo(x))
 }

use

option.map(foo(_))

Another example

def processBody(contentType: String): String = {
 .....
}

val body: Option[String] = 
   headers.get("Content-Type").map(processBody(_))

I assumed here that headers.get returns an Option.

Vishal John
  • 4,231
  • 25
  • 41