Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"With" statement equivalent for Scala?

Tags:

scala

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

( n.child.size > 0 ) && ( n.child.filter( ! _.isInstanceOf[Text] ).size == 0 )

('n' is a scala.xml.Node, but that's not important. Nor is the particular logic.)

Calling child() twice isn't so good, so I was about to change it:

val list = n.child
( list.size > 0 ) && ( list.filter( ! _.isInstanceOf[Text] ).size == 0 )

But given how much I've come to much appreciate being able to filter() and map() and such without needing to declare intermediate variables, I found this immediately smelly. It's so... so... so Java-ish! :p

Alas, digging through SO and Google and the ScalaDocs (especially Any and AnyRef) and The Book has turned up nothing appropriate. I was hoping perhaps for something like:

n.child{ list => ( list.size > 0 ) && ( list.filter( ! _.isInstanceOf[Text] ).size == 0 ) }

or even

n.child.with{ list => ... }

Does something like this exist? Or am I just getting caught up in a variable-less-ness fervour?

like image 627
Rodney Gitzel Avatar asked Jul 13 '10 20:07

Rodney Gitzel


People also ask

What is with in Scala?

Use the with Keyword in ScalaThis keyword is usually used when dealing with class compositions with mixins. Mixins are traits that are used to compose a class. This is somewhat similar to a Java class that can implement an interface.

What is the difference between extends and with in Scala?

The first thing you inherit from can either be a trait or a class, using the extends keyword. You can define further inherited traits (and only traits) using the with keyword. If a trait inherits from a class you cannot use it like in the above example. That's it regarding the extends and with keywords.

What is expression in Scala?

2.2. In Scala, all statements are expressions that return a value. The value of the last statement determines the value of an expression. The if expression is no different — it always returns a value. So, we can assign the result of an if expression to a value.

What is Instanceof in Scala?

You can use the isInstanceOf method to test the type of an object: if (x. isInstanceOf[Foo]) { do something ... However, some programmers discourage this approach, and in other cases, it may not be convenient. In these instances, you can handle the different expected types in a match expression.


2 Answers

{
    import n.child._
    ( size > 0 ) && ( filter( ! _.isInstanceOf[Text] ).size == 0 )
}
like image 157
Ken Bloom Avatar answered Oct 09 '22 00:10

Ken Bloom


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

like image 40
Dave Griffith Avatar answered Oct 09 '22 00:10

Dave Griffith