3 ms·
In Scala '||' does mean logical OR. The example given is using the logical OR to fold across booleans. Imagine this expression: (a || b || c || d || e ||
by tommorris 15y ago
In Scala '||' does mean logical OR.
The example given is using the logical OR to fold across booleans.
Imagine this expression:
(a || b || c || d || e || f || g || h)
If any of those values are true, the whole expression evaluates as true. It only evaluates as false if all the values are false.
Now consider a simple example in Scala:
val res1 = List(false, false, false, false, true)
res1.foldLeft(false) { _ || _ }
Here the foldLeft function is 'accumulating' the value of the boolean by doing an OR comparison with the starting value (false). For all but the last one, it is doing (false || false -> false), then it hits the last one and does (false || true -> true).
It's a neat trick, and useful if you were using a programming language that had foldLeft but not exists.
In Scala, you have exists. So you can do this instead:
res1.exists(_ == true)
Or for the example given, something like this:
words.exists(tweet.contains(_))
- jamesaguilar 15y agoOh, fail me for that particular point. I should have looked at what function was being called rather than just the syntax.