How map work on Options in Scala?

烂漫一生 提交于 2021-01-28 11:44:59

问题


I have this two functions

  def pattern(s: String): Option[Pattern] =
try {
  Some(Pattern.compile(s))
} catch {
  case e: PatternSyntaxException => None
}

and

  def mkMatcher(pat: String): Option[String => Boolean] =
     pattern(pat) map (p => (s: String) => p.matcher(s).matches)

Map is the higher-order function that applies a given function to each element of a list.

Now I am not getting that how map is working here as per above statement.


回答1:


Map is the higher-order function that applies a given function to each element of a list.

This is an uncommonly restrictive definition of map.

At any rate, it works because it was defined by someone who did not hold to that.

For example, that someone wrote something akin to

sealed trait Option[+A] {
  def map[B](f: A => B): Option[B] = this match {
    case Some(value) => Some(f(value))
    case None => None
  }
}

as part of the standard library. This makes map applicable to Option[A]

It was defined because it makes sense to map many kinds of data structures not just lists. Mapping is a transformation applied to the elements held by the data structure.

It applies a function to each element.

Option[A] can be thought of as a trivial sequence. It either has zero or one elements. To map it means to apply the function on its element if it has one.

Now it may not make much sense to use this facility all of the time, but there are cases where it is useful.

For example, it is one of a few distinct methods that, when present enable enable For Expressions to operate on a type. Option[A] can be used in for expressions which can be convenient.

For example

val option: Option[Int] = Some(2)

val squared: Option[Int] = for {
  n <- option
  if n % 2 == 0
} yield n * n

Interestingly, this implies that filter is also defined on Option[A].

If you just have a simple value it may well be clearer to use a less general construct.




回答2:


Map is working the same way that it does with other collections types like List and Vector. It applies your function to the contents of the collection, potentially changing the type but keeping the collection type the same.

In many cases you can treat an Option just like a collection with either 0 or 1 elements. You can do a lot of the same operations on Option that you can on other collections.

You can modify the value

var opt = Option(1)

opt.map(_ + 3)
opt.map(_ * math.Pi)
opt.filter(_ == 1)
opt.collect({case i if i > 0 => i.toString })

opt.foreach(println)

and you can test the value

opt.contains(3)
opt.forall(_ > 0)
opt.exists(_ > 0)
opt.isEmpty

Using these methods you rarely need to use a match statement to unpick an Option.



来源:https://stackoverflow.com/questions/48920941/how-map-work-on-options-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!