Skip to content

Comment on Making Map Operations Implicit in Programming

Comments

The example in the article was taken directly from the Scala documentation for [Option](http://www.scala-lang.org/api/current/index.html#scala.Optio...). The map is implicit if you use Scala's for notation. From the linked docs:

    val upper = for {
      name <- request getParameter "name"
      trimmed <- Some(name.trim)
      upper <- Some(trimmed.toUpperCase) if trimmed.length != 0
    } yield upper
    println(upper getOrElse "")
In Scala, an implementation of flatMap (bind), map, and filter allows you to use for expressions like the above. Objects that support these operations are not strictly monads, but they're close enough for most practical uses. Haskell is great, but give Scala a little credit.

for expressions (and Haskell's do notation, etc.) are alternative syntax for explicit mapping: implicit mapping would be syntax that would make applying an operator to all members of a collection (including one with limited cardinality like Option) indistinguishable from applying the same operator to a simple value.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.