Scala underscore minimal function

前端 未结 3 632

Let\'s create a value for the sake of this question:

val a = 1 :: Nil

now, I can demonstrate that the anonymous functions can be written in

3条回答
  •  臣服心动
    2020-11-27 22:39

    For the record, a.map(_) does not work because it stands for x => a.map(x), and not a.map(x => x). This happens because a single _ in place of a parameter stands for a partially applied function. In the case of 2*_, that stands for an anonymous function. These two uses are so close that is very common to get confused by them.

提交回复
热议问题