lazy function definitions in scala

后端 未结 6 1516
滥情空心
滥情空心 2021-02-02 16:49

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 javascri

相关标签:
6条回答
  • 2021-02-02 17:18

    You can define a lazy val which is a function :

    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.

    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.

    0 讨论(0)
  • 2021-02-02 17:23

    Scala has lazy vals, 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).

    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).

    0 讨论(0)
  • 2021-02-02 17:31

    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.

    0 讨论(0)
  • 2021-02-02 17:31

    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:

    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.

    0 讨论(0)
  • 2021-02-02 17:31

    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.

    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
    • There is no "function" keyword, instead you use something like (s:String) => {code}
    • The last statement is the return value of a block, so you don't need add "return".
    0 讨论(0)
  • 2021-02-02 17:38

    I known nothing about Ruby, but scala has singleton object pattern also:

    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
    
    0 讨论(0)
提交回复
热议问题