Scala:返回对函数的引用 [英] Scala: return reference to a function

查看:53
本文介绍了Scala:返回对函数的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 Scala 函数来返回对另一个函数的引用,这可能吗?

I'd like to have a Scala functions that returns the reference to another function, is that possible?

推荐答案

您可以返回一个函数类型(这是由 A => B 定义的).在这种情况下,从整数到整数:

You can return a function type (this is defined by A => B). In this case Int to Int:

scala> def f(x:Int): Int => Int =  { n:Int => x + n }
f: (x: Int)(Int) => Int

当您调用该函数时,您将获得一个新函数.

When you call the function you get a new function.

scala> f(2)
res1: (Int) => Int = <function1>

可以作为普通函数调用的:

Which can be called as a normal function:

scala> res1(3)
res2: Int = 5

这篇关于Scala:返回对函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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