Posts Tagged ‘fold’

Scala Collections Tips and Tricks

Posted in Programming, Scala on March 14th, 2015 by Pavel – 32 Comments

Library bookshelfThis article presents a list of simplifications and optimizations of typical Scala Collections API usages.

Some of the tips rest upon subtle implementation details, though most of the recipes are just common sense transformations that, in practice, are often overlooked.

The list is inspired by my efforts to devise practical Scala Collections inspections for the IntelliJ Scala plugin. We’re now in process of implementing those inspections, so if you use the plugin in IDEA, you’ll automatically benefit from static code analysis.

Nevertheless, the recipes are valuable by themselves and can help you to deepen your understanding of Scala Collections and to make your code faster and cleaner.

Update:
If you feel adventurous, you can learn how to contribute to IntelliJ Scala plugin and try your hand at “up for grabs” collection inspections that are to your liking.

The article is also (independently) translated into Russian and Chinese (one more translation).

Contents:

  1. Legend
  2. Composition
  3. Side effects
  4. Sequences
    4.1. Creation
    4.2. Length
    4.3. Equality
    4.4. Indexing
    4.5. Existence
    4.6. Filtering
    4.7. Sorting
    4.8. Reduction
    4.9. Matching
    4.10. Rewriting
  5. Sets
  6. Options
    6.1. Value
    6.2. Null
    6.3. Processing
    6.4. Rewriting
  7. Maps
  8. Supplement

All the code examples are available as a GitHub repository.
Continue reading →