expression

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

How to use function literals (anonymous functions) in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.1, “How to use function literals (anonymous functions) in Scala.”

Problem

You want to use an anonymous function — also known as a function literal — so you can pass it into a method that takes a function, or to assign it to a variable.

Solution

Given this List:

Returning a value from a Scala try/finally expression alvin June 8, 2015 - 8:09am

I like looking at source code from other developers, I almost always learn something. In this case I knew that a try expression in Scala returned a value, but I have never used it like this. This example is from the book, Learning Concurrent Programming in Scala.

How to access the value of the default case in a Scala match expression

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shorter recipes, Recipe 3.10, “How to access the value of the default case in a Scala match expression.”

Problem

You want to access the value of the default, “catch all” case when using a match expression, but you can’t access the value when you match it with the _ wildcard syntax.

Solution

Instead of using the _ wildcard character, assign a variable name to the default case:

Scala: How to assign the result of a match expression to a variable

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 3.9, “How to assign the result of a match expression to a variable.”

Problem

You want to return a value from a match expression and assign it to a variable, or use a match expression as the body of a method.

Scala: How to match multiple conditions (patterns) with one case statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.8, “How to match multiple conditions with one case statement.”

Problem

You have a situation where several match conditions require that the same business logic be executed, and rather than repeating your business logic for each case, you’d like to use one copy of the business logic for the matching conditions.

How to use a Scala match expression like a switch statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.7, “How to use a Scala match expression like a switch statement.”

Back to top

Problem

You have a situation where you want to create something like a simple Java integer-based switch statement, such as matching the days in a week, months in a year, and other situations where an integer maps to a result.

Back to top

Solution

To use a Scala match expression like a Java switch statement, use this approach:

Table of Contents

  1. Problem
  2. Solution
Back to top

Using Scala match expressions with XML

Problem: You want to use match expressions as another way to access the information contained in XML data when writing your Scala applications.

Solution

Given this XML literal:

How to enter multiline commands (statements) into the Scala REPL

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:

How to search a Perl list for a pattern using grep

A lot of times when you're using Perl you have a list (or array), and you need to search that list for all strings that match a certain regular expression pattern (or regex). Fortunately Perl is built for this type of work, and it's straightforward to (a) perform this search, and (b) put your search results in another list.