谁能解释一下符号“="在 Scala 中使用 [英] Can anyone explain how the symbol "=>" is used in Scala

查看:32
本文介绍了谁能解释一下符号“="在 Scala 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Scala 中阅读了很多使用符号 => 的代码片段,但我从未真正理解它.我试图在互联网上搜索,但找不到任何全面的内容.任何有关如何使用/可以使用符号的指针/解释都会非常有帮助.

I've read a lot of code snippets in scala that make use of the symbol =>, but I've never really been able to comprehend it. I've tried to search in the internet, but couldn't find anything comprehensive. Any pointers/explanation about how the symbol is/can be used will be really helpful.

(更具体地说,我还想知道操作符是如何在函数字面量中出现的)

(More specifially, I also want to know how the operator comes into picture in function literals)

推荐答案

不仅仅是传递值/名称,=> 用于定义函数字面量,这是用于定义的替代语法一个函数.

More than passing values/names, => is used to define a function literal, which is an alternate syntax used to define a function.

示例时间.假设您有一个接收另一个函数的函数.集合中充满了它们,但我们将选择 filter.filter,当用于集合(如 List)时,将删除任何导致您提供的函数返回 false 的元素.

Example time. Let's say you have a function that takes in another function. The collections are full of them, but we'll pick filter. filter, when used on a collection (like a List), will take out any element that causes the function you provide to return false.

val people = List("Bill Nye", "Mister Rogers", "Mohandas Karamchand Gandhi", "Jesus", "Superman", "The newspaper guy")
// Let's only grab people who have short names (less than 10 characters)
val shortNamedPeople = people.filter(<a function>)

我们可以从其他地方传入一个实际的函数(def isShortName(name: String): Boolean,也许),但最好把它放在在那里.唉,我们可以,使用函数文字.

We could pass in an actual function from somewhere else (def isShortName(name: String): Boolean, perhaps), but it would be nicer to just place it right there. Alas, we can, with function literals.

val shortNamedPeople = people.filter( name => name.length < 10 )

我们在这里所做的是创建一个函数,它接受一个字符串(因为 peopleList[String] 类型),并返回一个布尔值.很酷吧?

What we did here is create a function that takes in a String (since people is of type List[String]), and returns a Boolean. Pretty cool, right?

此语法在许多上下文中使用.假设想要编写一个接收另一个函数的函数.另一个函数应该接受一个字符串,并返回一个 Int.

This syntax is used in many contexts. Let's say you want to write a function that takes in another function. This other function should take in a String, and return an Int.

def myFunction(f: String => Int): Int = {
  val myString = "Hello!"
  f(myString)
}
// And let's use it. First way:
def anotherFunction(a: String): Int = {
  a.length
}
myFunction(anotherFunction)
// Second way:
myFunction((a: String) => a.length)

这就是函数字面量.回到by-nameby-value,有一个技巧,你可以强制一个参数在你想要的时候不被评估.经典例子:

That's what function literals are. Going back to by-name and by-value, there's a trick where you can force a parameter to not be evaluated until you want to. The classic example:

def logger(message: String) = {
  if(loggingActivated) println(message)
}

这看起来没问题,但 message 实际上是在调用 logger 时计算的.如果 message 需要一段时间来评估怎么办?例如,logger(veryLongProcess()),其中 veryLongProcess() 返回一个字符串.哎呀?并不真地.我们可以利用我们对函数字面量的了解来强制 veryLongProcess() 在实际需要之前不被调用.

This looks alright, but message is actually evaluated when logger is called. What if message takes a while to evaluate? For example, logger(veryLongProcess()), where veryLongProcess() returns a String. Whoops? Not really. We can use our knowledge about function literals to force veryLongProcess() not to be called until it is actually needed.

def logger(message: => String) = {
  if(loggingActivated) println(message)
}
logger(veryLongProcess()) // Fixed!

logger 现在接受一个 函数,它不接受任何参数(因此左侧是裸露的 =>).您仍然可以像以前一样使用它,但现在,message 仅在使用时进行评估(在 println 中).

logger is now taking in a function that takes no parameters (hence the naked => on the left side). You can still use it as before, but now, message is only evaluated when it's used (in the println).

这篇关于谁能解释一下符号“="在 Scala 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆