What does this mean: map (1 +)

I mean, I know what that means: take a list and add 1 to each element in it; those. equivalently map (1 + _ ). I don't understand how Scala knows this is equivalent. What's going on here?

Edit

Daniel points out a more general question. for instance

def g(f : Int => Int, x : Int) = f(f(x))
g( (1 + ), 2)
res12: Int = 4

Which is cool. Every day, I find a new useful thing Scala can do. I guess I'm looking for a full description (and, ideally, a name) of this particular thing.

+3
source share
1 answer

This is something like this:

  • mapexpects a function Int => B(in this case).
  • 1 +doesn't allow the function Int => B, so try other things.
  • 1 +can be removed from a method that expects a parameter Intto a function Int => Int.

Presto.

One uses 1 + _to resolve ambiguity.

+8
source

Source: https://habr.com/ru/post/1789774/


All Articles