简体   繁体   English

在 map 方法中调用匿名 function

[英]Calling an anonymous function inside a map method

I was making a constructor with multiple possible arguments, when I realized my IDE was pointing out a type discrepancy:当我意识到我的 IDE 指出了一个类型差异时,我正在使用多个可能的 arguments 创建一个构造函数:

  case class PathAndColumns(path: String, column: Array[Column]) {

  def this(path: String, column: Column) {
    this(path, Array(column))
  }

  def this(path: String, column: String) {
    this(path, column.split(",").map( _ => col(_))) // : Array[String=>Column]
  }

So I tried just passing the col function.所以我试着通过col function。

  def this(path: String, column: String) {
    this(path, column.split(",").map(col)) // I work!
  }

It works, and then, trying to understand why, I ended up with:它有效,然后,试图理解为什么,我最终得到:

  def this(path: String, column: String) {
    this(path, column.split(",").map(letsSee)) // I work as well
  }

  def letsSee(myStringCol: String): Column = {
    (() => col(myStringCol))() // A pair of extra parenthesis to simulate a call
  }

So I found out that passing an anonymous function to methods like map doesn't return the result, but rather the whole function (because it's treated as an object I suppose). So I found out that passing an anonymous function to methods like map doesn't return the result, but rather the whole function (because it's treated as an object I suppose).

So how do I make this work in order to get a Column rather than the function type, without declaring it separatedly?那么,如何在不单独声明的情况下获得一个 Column 而不是 function 类型来完成这项工作?

  def this(path: String, column: String) {
    this(path, column.split(",").map(_ => {
      col(_)
    }))
  }

The simple answer is that you replace简单的答案是你替换

_ => col(_)

with

x => col(x)

The problem is that you are treating _ is a normal variable identifier, but it isn't.问题是您将_普通变量标识符,但事实并非如此。 The two uses of _ in the first expression are completely independent.第一个表达式中_的两种用法是完全独立的。

The first _ is a placeholder for the argument to an anonymous function.第一个_是匿名 function 的参数的占位符。 The syntax _ =>... means that you are defining a function but will not use the argument to that function when calculating the result.语法_ =>...表示您正在定义 function 但在计算结果时不会使用该 function 的参数。

The second _ is a placeholder for the argument to col .第二个_col参数的占位符。 The syntax col(_) turns the method col into a free function.语法col(_)将方法col转换为免费的 function。

The two underscores in中的两个下划线

.map(_ => col(_))

desugar to脱糖

.map(x1 => x2 => col(x2))

instead of the desired而不是想要的

.map(x1 => col(x1))

On the other hand, the following works另一方面,以下工作

.map(col)

due to eta-expansion .由于eta 扩展

What you are doing is passing to map an anonymous function returning another function with type String => Column.您正在做的是向 map 传递一个匿名 function 返回另一个 function 类型为 String => Column。 You have to remove placeholder _ from the left part of your function literal, or use explicit argument name.您必须从 function 文字的左侧删除占位符 _ ,或使用显式参数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM