By Alvin Alexander. Last updated: November 10, 2018
Wow, I began by writing a few Scala programming tutorials just because I like the language, and as I look here a couple of months later I now have more than sixty tutorials. As a result, I thought I'd start organizing them here in the form a Scala Programming Cookbook.
Here's my current collection of Scala FAQs and recipes, in a "cookbook" format.
General Scala programming recipes
- A Scala cheat sheet (reference page)
- A Scala current date and time example
- Scala if then else syntax
- Scala multiline Strings and heredoc syntax
- Scala ternary operator syntax
- Scala for loop and yield examples (a 'yield' tutorial)
- The C# style (curly brace) package syntax in Scala
- How to add new methods to existing classes in Scala
Scala classes and methods
- Creating JavaBeans classes in Scala
- Importing Java classes and packages into a Scala program
- How to create multiple class constructors in Scala
- Scala functions - named arguments and default arguments
- Scala methods - dots, spaces, and parentheses
- A Scala class casting example
- Scala equivalent of Java .class (classOf)
- How to rename classes when you import them in Scala
- How to create a private default/primary constructor
Scala try/catch/finally block examples
Scala collection recipes
Scala Array examples:
- Mutable Scala arrays
- Scala String array examples
- Convert a Scala array to a String with mkString
- Scala data types
- Converting Java collections to Scala collections
- How to create and use a multidimensional array (2D array) in Scala
Scala List examples:
- Iterating over Scala Lists with foreach and for
- How to merge Scala Lists
- Five ways to create a Scala List
- Scala List class examples
- How to add elements to a List in Scala
- How to use the Scala List class filter method
Scala Map examples:
Iterating and looping over Scala collections:
- Iterating over Scala Lists with foreach and for
- Iterating over Scala Maps
- Convert a Scala array to a String with mkString
Scala tuples:
Using Scala parallel programming methods and classes:
Scala String recipes
- Scala String array examples
- Scala String formatting (like Java String.format)
- Convert a Scala array to a String with mkString
- A Scala split string example
- Scala multiline Strings and heredoc syntax
- How to convert a String to Int in Scala
- In Scala you compare two Strings with '==', not equals()
- A Scala String chomp method
Functions and functional programming recipes
- Scala functions - named arguments and default arguments
- Passing one function to another function as a function argument
- Passing a function to a function (Swing example)
Scala web development recipes
- Scala Lift Framework form tutorials, examples
- A Scala Spring Framework dependency injection example
- A Scala JSON example, using Lift-JSON
Scala email recipes
- A simple Scala IMAP client (using SSL and IMAPS)
- A Scala IMAP client with search capability (SearchTerm, AndTerm)
Scala database recipes
Scala file read, write, input/output recipes
Scala command line and script recipes
- Reading Scala command line arguments
- How to execute (exec) system commands in Scala
- Scala shell scripts - Prompting a user and reading their input
- How to make your Scala scripts run faster by compiling them to jar files
- How to show String and StringOps methods in the Scala REPL
- How to show more methods on classes/objects in the Scala REPL
- How to enter or paste multiline commands in the Scala REPL
Web service recipes
- A Scala REST client using Apache HttpClient library
- A Scala Twitter client example
- A Scala JSON parsing example, using Lift-JSON
- A Scala JSON array parsing example, using Lift-JSON
Scala XML recipes
- Scala XML - searching XMLNS namespaces, XPath, and more
- How to load an XML file in Scala (open and read the XML file)
- How to load an XML URL in Scala (RSS feed, RESTful web service, or other)
- A collection of Scala XML examples (creating XML literals, multiline literals, XPath searching)
Scala build, testing, and debugging recipes
- How to run ScalaTest unit tests in Eclipse
- The Scala SBT documentation in PDF format
- How to show your SBT history (history of SBT commands)
Scala Actors and concurrency
- How to stop/quit a Scala Actor
- Simple Scala Akka Actor 'Hello, World' examples
- A Simple Akka actors 'ping pong' example
- How to stop an Akka actor and shut down the Akka actor system
- A simple Akka Futures example (calculating 1+1 concurrently)
- An Akka actors "ask" example (ask, future, await, timeout)
Scala programming idioms
- Using Scala's Option, Some, and None idiom
- Scala idiom - Methods should have no side effects
- Scala idiom - Prefer immutable code
- More coming soon ...
Example algorithms
Scala cookbook recipes - summary
As mentioned, I'm just putting this collection of Scala programming recipes out here today, and I'll try to clean up and organize them in the coming days.