scala中的惰性函数定义 [英] lazy function definitions in scala

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

问题描述

我一直在学习scala,我不得不说这是一个很酷的语言。我特别喜欢它的模式匹配功能和功能文字,但我来自一个javascript,红宝石背景,我最喜欢的模式之一就是这些语言的懒惰功能和方法定义模式。 javascript中的一个例子是

  var foo = function(){
var t = new Date();
foo = function(){
return t;
};
return foo();
};

与小调整相同的代码在Ruby中工作,您只需使用单例对象重新定义方法执行计算。当涉及昂贵的计算时,这种事情真的很方便,如果你需要结果,你不会提前知道。我知道在scala中,我可以使用缓存来模拟相同的结果,但是我试图避免条件检查,到目前为止,我的实验已经返回了负面的结果。有没有人知道Scala中是否有懒惰的函数或方法定义模式?



注意:JavaScript代码来自Peter Michaux的 site

解决方案

p>所有复杂的JavaScript代码似乎只是尝试缓存日期的值。在Scala中,您可以平凡地实现同样的事情:

  lazy val foo = new Date 

而且,如果甚至不想做一个val,但是想要调用一个函数,只有在需要时才执行昂贵的代码,可以

  def maybeExpensive(doIt:Boolean,expensive:=> String){
if(doIt) println(昂贵)
}
maybeExpensive(false,(0到1000000).toString)//(0到1000000).toString永远不会被调用!
maybeExpensive(true,(0 to 10).toString)//这个时候被调用并使用

其中模式 expensive:=> String 被称为by-name参数,您可以将其视为给我一些可以根据请求生成字符串的东西。请注意,如果您使用它两次,它将每次重新生成,这是Randall Schultz的方便模式:

  def maybeExpensiveTwice(doIt:Boolean,expensive:=> String){
lazy val e = expensive
if(doIt){
println(e)
println(哇,那是+ e.length +字符长!)
}
}

现在,只有当您需要它(通过by-name参数)存储并生成(如果您需要再次使用它(通过延迟的值)),那么它就会生成。



所以这样做,而不是JavaScript的方式,即使你可以使Scala看起来很像JavaScript。


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.

解决方案

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:

lazy val foo = new Date

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

def maybeExpensive(doIt: Boolean, expensive: => String) {
  if (doIt) println(expensive)
}
maybeExpensive(false, (0 to 1000000).toString)  // (0 to 1000000).toString is never called!
maybeExpensive(true, (0 to 10).toString)        // It is called and used this time

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:

def maybeExpensiveTwice(doIt: Boolean, expensive: => String) {
  lazy val e = expensive
  if (doIt) {
    println(e)
    println("Wow, that was " + e.length + " characters long!")
  }
}

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中的惰性函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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