“With” statement equivalent for Scala?

前端 未结 6 879
野的像风
野的像风 2021-02-13 10:31

Idle pondering from a Scala learner perhaps, but ... in my tinkerings I\'ve written the following:

( n.child.size > 0 ) && ( n.child.filter( ! _.isIns         


        
6条回答
  •  清酒与你
    2021-02-13 10:39

    "with" is, of course, a reserved word in Scala, so let's call it "let", from the similar binding form in Lisp and Haskell. Turns out "let" is just a backwards way of writing function application.

    def let[A,B](param:A)(body: A=>B):B = body(param)
    
    let(n.child){list=> ...}
    

    If the bound variable is used only once, you could of course use the anonymous function form, but that defeats the purpose.

提交回复
热议问题