Scala Collections for the Easily Bored Part 1: A Tale of Two Flavors. 21 Jul 2008 One of the most obvious things to a Java developer first coming into Scala-land is the radically different Collections API included as part of the standard library.
For the most part, we use the same frameworks and APIs in Scala as are available in Java. This is natural, thanks to the extremely tight integration between the two languages. So why is this one area such a startling departure from Scala’s heritage? The answer has to do with what the Scala language is syntactically capable of handling. I had actually planned on addressing this topic in a single article. A Tale of Two Flavors The very first thing developers notice when looking at Scala’s collections is a (seemingly) odd redundancy in the specification. I don’t know about you, but this confused the heck out of me the first time I really dug into Scala’s standard API.
As it turns out, there’s a very logical reason for having these separated and seemingly-redundant collections packages. Conclusion. Scala Collections for the Easily Bored Part 2: One at a Time. 28 Jul 2008 As I hinted previously, this series is intended to delve into Scala’s extensive collections API and the many ways in which it can make your life easier.
Probably the most important operations you could ever perform on collections are those which examine each element, one at a time. After all, what’s a more common array idiom than looping over all values? In that vein, this article starts by looking at foreach, the imperative programmer’s bread-and-butter when it comes to types like Array and List. But rather than just stopping there, we also will look at more powerful, higher-order operations like fold, map and the ever-mysterious: flatMap. Iterating As I said above, looping over every item in a collection is probably the most heavily used operation in a programmer’s repertoire.
This code should be old hat to anyone coming from a Java background. The same approach is taken in Scala. Here we define an anonymous method (Scala’s name for a closure) which takes a single parameter. Scala Collections for the Easily Bored Part 3: All at Once. 4 Aug 2008 In the previous installment of this series, we looked at how Scala’s collections provide common mechanisms for iteration, as well as many higher-order operations in the same conceptual vein.
In this, the third and final part of the series, we will examine some mechanisms for conceptually operating on the collection as a whole. Thus, rather than transforming individual collection elements, we will be looking at ways to inspect and modify the data structure itself. Filtration Stepping out of Scala for a moment, let’s consider the common design paradigm of the relational database.
This query would of course return a data set which could be iterated over, performing the necessary operations (in this case, incarceration) for each record. If we call the onlyEven method, passing List(1, 2, 3, 4, 5), the result will be List(2, 4), since 2 and 4 are the only numbers into which 2 divides evenly. Partition Searching Conclusion. Scalaj's scalaj-collection at master - GitHub.