function literal

How to use partially applied functions in Scala alvin March 7, 2018 - 8:16pm

Problem: You want to eliminate repetitively passing variables into a Scala function by (a) passing common variables into the function to (b) create a new function that’s pre-loaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution: The classic example of a partially applied function begins with a simple sum function:

How to define Scala methods that take complex functions as parameters (syntax) alvin March 7, 2018 - 8:14pm

Problem: You want to define a Scala method that takes a function as a parameter, and that function may have one or more input parameters, and may also return a value.

Solution: Following the approach described in the previous recipe, define a method that takes a function as a parameter. Specify the function signature you expect to receive, and then execute that function inside the body of the method.

A real-world functional programming example in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.9, “A real-world functional programming example in Scala.”

Problem

Understanding functional programming concepts is one thing; putting them into practice in a real project is another. You’d like to see a real example of functional programming in Scala.

How to create and use partial functions in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.8, “How to create and use partial functions in Scala.”

Problem

You want to define a function that will only work for a subset of possible input values, or you want to define a series of functions that only work for a subset of input values, and combine those functions to completely solve a problem.

How to create a method that returns a function in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.7, “How to create a method that returns a function in Scala.”

Problem

You want to return a function (algorithm) from a function or method.

Solution

Define a function that returns an algorithm (an anonymous function), assign that to a new function, and then call that new function.

The following code declares an anonymous function that takes a String argument and returns a String:

How to use closures in Scala (closure examples, syntax)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.5, “How to use closures in Scala (closure examples, syntax).”

Back to top

Problem

You want to pass a function around like a variable, and while doing so, you want that function to be able to refer to one or more fields that were in the same scope as the function when it was declared.

Back to top

Solution

You can demonstrate a closure in Scala with the following simple (but complete) example:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to define a Scala method that accepts a function parameter

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.3, “How to define a method that accepts a simple function parameter.”

Problem

You want to create a method that takes a simple function as a method parameter.

Solution

This solution follows a three-step process:

How to use functions as variables (values) in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.2, “How to use functions as variables (values) in Scala.”

Back to top

Problem

You want to pass a Scala function around like a variable, just like you pass String, Int, and other variables around in an object-oriented programming language.

Back to top

Solution

Use the syntax shown in Recipe 9.1 to define a function literal, and then assign that literal to a variable.

Table of Contents

  1. Problem
  2. Solution
Back to top

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: