简体   繁体   English

scala中的延迟函数定义

[英]lazy function definitions in scala

I've been learning scala and I gotta say that it's a really cool language. 我一直在学习scala,我得说它是一种非常酷的语言。 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. 我特别喜欢它的模式匹配功能和函数文字,但我来自javascript,ruby背景,这些语言中我最喜欢的模式之一是惰性函数和方法定义模式。 An example in javascript is javascript中的一个例子是

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. 带有小调整的相同代码在ruby中工作,您只需使用单例对象在执行计算后重新定义该方法。 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. 我知道在scala中我可以使用缓存来模拟相同类型的结果,但我正在尝试避免条件检查,到目前为止,我的实验已经返回了负面结果。 Does anyone know if there is a lazy function or method definition pattern in scala? 有没有人知道scala中是否存在惰性函数或方法定义模式?

Note: The javascript code is from Peter Michaux's site . 注意:javascript代码来自Peter Michaux的网站

All that complicated code in JavaScript appears to just try to cache the value of the date. JavaScript中所有复杂的代码似乎只是尝试缓存日期的值。 In Scala, you can achieve the same thing trivially: 在Scala中,您可以轻松地实现同样的目标:

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 并且,如果甚至不想制作val,但想要调用只在需要时才执行昂贵代码的函数,则可以

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." 模式expensive: => String被称为名字参数,您可以将其视为“给我一些将根据请求生成字符串的东西”。 Note that if you use it twice, it will regenerate it each time, which is where Randall Schultz' handy pattern comes in: 请注意,如果您使用它两次,它将每次重新生成它,这是Randall Schultz的便利模式所在:

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). 现在只有在需要时才会生成(通过by-name参数) 存储它并在需要时重新使用它(通过lazy val)。

So do it this way, not the JavaScript way, even though you could make Scala look a lot like the JavaScript. 所以这样做,而不是JavaScript方式,即使你可以让Scala看起来很像JavaScript。

Scala has lazy val s, whose initializers are not evaluated unless and until the val is used. Scala具有lazy val值,除非使用val,否则不会对其初始化程序进行求值。 Lazy vals may be used as method local variables. 延迟val可以用作方法局部变量。

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. Scala还具有按名称方法参数,其实际参数表达式包含在thunk中,并且每次在方法体中引用形式参数时都会评估thunk。

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). 这些可以用来实现惰性评估语义,例如Haskell中的默认语义(至少在我对Haskell的非常有限的理解中)。

def meth(i: => Int): Something = {
  //        ^^^^^^ by-name parameter syntax
  lazy val ii = i
  // Rest of method uses ii, not i
}

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 uses ii one or more times). 在此方法中,用作实际参数的表达式将被评估为零次(如果方法体的动态执行路径从不使用ii )或一次(如果它使用ii一次或多次)。

You can define a lazy val which is a function : 你可以定义一个lazy val这是一个函数:

lazy val foo = {
  val d = new Date
  () => { d }
}

println(foo())

foo() will now return the same Date object each time, object which will be initialized the first time foo is called. foo()现在每次都返回相同的Date对象,在第一次调用foo时将初始化的对象。

To explain the code a little, the first time foo() is called { val d = new Date; () => { d } } 为了解释一下代码,第一次调用foo() { val d = new Date; () => { d } } { 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. { val d = new Date; () => { d } } ,将d分配给新的日期值,然后计算最后一个表达式() => { d }并将其分配给foo值。 Then foo is a function with no parameters which return d. 然后foo是一个没有参数返回d的函数。

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: 你想要的Scala构造是一个简单的惰性定义:

lazy val foo = new java.util.Date

The construction of the Date object will occur at most once and be deferred until the first reference to foo. Date对象的构造最多只发生一次,并推迟到第一次引用foo。

I known nothing about Ruby, but scala has singleton object pattern also: 我对Ruby一无所知,但scala也有单例对象模式:

Welcome to Scala version 2.8.0.r22634-b20100728020027 (Java HotSpot(TM) Client VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object LazyInit {                                       
     |     val msec = { println("Hi,I'm here!");   System.currentTimeMillis }
     | }
defined module LazyInit

scala> System.currentTimeMillis                                              
res0: Long = 1282728315918

scala> println(System.currentTimeMillis +" : " + LazyInit.msec)              
Hi,I'm here!
1282728319929 : 1282728319930

scala> println(System.currentTimeMillis +" : " + LazyInit.msec)
1282728322936 : 1282728319930

scala> println(System.currentTimeMillis +" : " + LazyInit.msec)
1282728324490 : 1282728319930

scala> 

If you want to get function ,you can make it subtype of a function type: 如果要获取函数,可以将其设置为函数类型的子类型:

scala> object LazyFun extends (() => Long) {            
     |     val msec = System.currentTimeMillis          
     |     def apply() = msec                           
     | }
defined module LazyFun

scala> System.currentTimeMillis                         
res2: Long = 1282729169918

scala> println(System.currentTimeMillis + " : " + LazyFun())
1282729190384 : 1282729190384

scala> println(System.currentTimeMillis + " : " + LazyFun())
1282729192972 : 1282729190384

scala> println(System.currentTimeMillis + " : " + LazyFun())
1282729195346 : 1282729190384

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. 在Scala中你可以做这样的事情,非常类似于你发布的javascript代码。

val foo = () => {
    val t = new Date()
    val foo = () => {t}

    foo()
}

println ("Hello World:" + foo())

The main difference is that: 主要区别在于:

  • You could not re-assignment the outer foo 你无法重新分配外部foo
  • There is no "function" keyword, instead you use something like (s:String) => {code} 没有“function”关键字,而是使用类似(s:String)=> {code}的内容
  • The last statement is the return value of a block, so you don't need add "return". 最后一个语句是块的返回值,因此您不需要添加“return”。

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

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