Scala List class: methods, examples, and syntax

This page contains a large collection of examples of how to use the methods on the Scala List class.

The Scala List class as an immutable, linear, linked-list class. It’s very efficient when it makes sense for your algorithms to (a) prepend all new elements, (b) work with it in terms of its head and tail elements, and (c) use functional methods that traverse the list from beginning to end, such as filter, map, foldLeft, reduceLeft.

Scala Seq class: methods, examples, and syntax alvin May 21, 2018 - 5:47pm

This page contains a large collection of examples of how to use the methods on the Scala Seq class.

Important note about Seq, IndexedSeq, and LinearSeq

As an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. As the Seq class Scaladoc states:

Scala: How to convert multiple spaces to a single space (regex) alvin November 30, 2017 - 3:04pm

Here’s a quick Scala example that shows how to convert multiple spaces in a string to a single space:

Scala “split string” examples

Scala String FAQ: How do I split a String in Scala based on a field separator, such as a string I get from a comma-separated value (CSV) or pipe-delimited file.


Use one of the split methods that are available on String objects:

A Perl trim function

Perl string trim FAQ: Is there something like a "trim" function in Perl, similar to the Java trim function, which trims leading and trailing whitespace characters from a string?

A Perl trim function

My Perl skills aren't exactly up to date these days, but in days gone past there was no Perl trim function, and somewhere along the way I wrote one. Here's the source code for my Perl trim function: