scala 中的惰性函数定义
我一直在学习 Scala,我不得不说这是一门非常酷的语言。我特别喜欢它的模式匹配功能和函数文字,但我有 javascript、ruby 背景,这些语言中我最喜欢的模式之一是惰性函数和方法定义模式。 javascript 中的一个示例是
var foo = function() {
var t = new Date();
foo = function() {
return t;
};
return foo();
};
在 ruby 中使用相同的代码,进行一些细微的调整,您只需在执行计算后使用单例对象重新定义方法。当涉及昂贵的计算并且您不提前知道是否需要结果时,这种事情非常方便。我知道在 scala 中我可以使用缓存来模拟相同类型的结果,但我试图避免条件检查,到目前为止我的实验返回了负面结果。有谁知道scala中是否有惰性函数或方法定义模式?
注意:JavaScript 代码来自 Peter Michaux 的网站。
I've been learning scala and I gotta say that it's a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascript, ruby background and one of my favorite patterns in those languages is the lazy function and method definition pattern. An example in javascript is
var foo = function() {
var t = new Date();
foo = function() {
return t;
};
return foo();
};
The same code with minor tweaks works in ruby where you just use the singleton object to redefine the method after the computation is performed. This kind of thing comes in really handy when expensive computation are involved and you don't know ahead of time if you are going to need the result. I know that in scala I can use a cache to simulate the same kind of result but I'm trying to avoid conditional checks and so far my experiments have returned negative results. Does anyone know if there is a lazy function or method definition pattern in scala?
Note: The javascript code is from Peter Michaux's site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
JavaScript 中所有复杂的代码似乎只是试图缓存日期的值。在 Scala 中,您可以简单地实现同样的事情:
并且,如果甚至不想创建 val,而是想调用一个仅在需要时执行昂贵代码的函数,您可以
在模式
处贵:=> String
被称为按名称参数,您可以将其视为“给我一些可以根据请求生成字符串的东西”。请注意,如果您使用它两次,它每次都会重新生成它,这就是 Randall Schultz 的便捷模式的用武之地:现在,仅在需要时才生成(通过名称参数)和存储它并在您再次需要时重新使用它(通过惰性 val)。
因此,请以这种方式而不是 JavaScript 方式进行操作,即使您可以使 Scala 看起来很像 JavaScript。
All that complicated code in JavaScript appears to just try to cache the value of the date. In Scala, you can achieve the same thing trivially:
And, if don't even want to make a val, but want to call a function that will only execute the expensive code if it needs it, you can
where the pattern
expensive: => String
is called a by-name parameter, which you can think of as, "Give me something that will generate a string on request." Note that if you use it twice, it will regenerate it each time, which is where Randall Schultz' handy pattern comes in:Now you generate only if you need it (via the by-name parameter) and store it and re-use it if you need it again (via the lazy val).
So do it this way, not the JavaScript way, even though you could make Scala look a lot like the JavaScript.
Scala 有惰性 val,除非使用 val,否则不会评估其初始值设定项。惰性 val 可以用作方法局部变量。
Scala 还具有按名称命名的方法参数,其实际参数表达式被包装在 thunk 中,并且每次在方法体中引用形式参数时都会对 thunk 进行求值。
这些可以一起用来实现惰性求值语义,例如 Haskell 中的默认语义(至少在我对 Haskell 的非常有限的理解中)。
在此方法中,用作实际参数的表达式将被计算零次(如果方法体的动态执行路径从不使用
ii
)或一次(如果它使用ii
)代码> 一次或多次)。Scala has
lazy val
s, whose initializers are not evaluated unless and until the val is used. Lazy vals may be used as method local variables.Scala also has by-name method parameters, whose actual parameter expressions are wrapped in a thunk and that thunk is evaluated every time the formal parameter is referenced in the method body.
Together these can be used to achieve lazy evaluation semantics such as are the default in Haskell (at least in my very limited understanding of Haskell).
In this method, the expression used as the actual parameter will be evaluated either zero times (if the dynamic execution path of the method body never uses
ii
) or once (if it usesii
one or more times).您可以定义一个惰性 val,它是一个函数:
foo()
现在每次都会返回相同的 Date 对象,该对象将在第一次调用 foo 时初始化。为了稍微解释一下代码,第一次调用 foo()
{ val d = new Date; ()=> { d } }
被执行,d 被分配给一个新的日期值,然后它计算最后一个表达式() =>; { d }
并将其分配给 foo 值。那么 foo 是一个没有参数的函数,返回 d。You can define a lazy val which is a function :
foo()
will now return the same Date object each time, object which will be initialized the first time foo is called.To explain the code a little, the first time foo() is called
{ val d = new Date; () => { d } }
is executed, d is assigned to a new date value then it evaluate the last expression() => { d }
and assign it to the foo value. Then foo is a function with no parameters which return d.我认为一些答复者对您提出问题的方式感到有点困惑。这里您想要的 Scala 构造是一个简单的惰性定义:
Date 对象的构造最多发生一次,并推迟到第一次引用 foo 为止。
I think some of the responders were a little confused by the way you phrased the question. The Scala construct you want here is a simple lazy definition:
The construction of the Date object will occur at most once and be deferred until the first reference to foo.
我对 Ruby 一无所知,但 scala 也有单例对象模式:
如果你想获取函数,你可以将其设为函数类型的子类型:
I known nothing about Ruby, but scala has singleton object pattern also:
If you want to get function ,you can make it subtype of a function type:
我认为你的意思是“惰性函数”是函数文字或匿名函数。
在 Scala 中,您可以执行类似的操作,与您发布的 javascript 代码非常相似。
主要区别在于:
I think what you mean "lazy function" is function literal or anonymous function.
In Scala you could do things like this, very similar to the javascript code you posted.
The main difference is that: