scala cookbook

How to use partially applied functions in Scala

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:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to define Scala methods that take complex functions as parameters (syntax)

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.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

A recommendation for would-be book writers

If you’ve read any of my books (like the Scala Cookbook or Functional Programming, Simplified), and thought, “Hey, I can write a book,” I encourage you to do so. One book that has been helpful in my writing career is William Zinsser’s On Writing Well: The Classic Guide to Writing Nonfiction. If you’re seriously thinking about writing a book about programming or any other technical topic, it’s a good read.

On deleting a chapter from the Scala Cookbook

In an effort to “Get rid of the crap,” I asked my editor if we could delete an entire chapter from the Scala Cookbook, and she said yes. I didn’t know if they’d allow that since the advance they paid me is based on a proposal of X chapters, but they’re all-in on just trying to create a good book, which is nice.

This is a page from my book, “A Survival Guide for New Consultants”

More information

As I finish editing this book in January, 2014, it’s a busy time. In addition to releasing this book, I just finished writing a 700+ page computer programming book for O’Reilly named Scala Cookbook.

A funny thing about the Scala Cookbook: I wrote it on a whim, as a dare, wondering, “Am I good enough to write this book, on my own?” I doubted myself for a little while, but then overcame my fears and sent an email to the people at O’Reilly. As I’ve said throughout this book, all you are is attitude.

How to split strings in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.3, “How to Split Strings in Scala.”

Problem

You want to split a string into parts based on a field separator, such as a string you get from a CSV or pipe-delimited file.

Solution

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

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to create multiline strings in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.2, “How to Create Multiline Strings in Scala.”

Problem

You want to create multiline strings within your Scala source code, like you can with the “heredoc” syntax of other languages.

Solution

In Scala you create multiline strings by surrounding your text with three double quotes:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to test String equality in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”

Problem

When using Scala, you want to compare two strings to see if they’re equal, i.e., whether they contain the exact same sequence of characters.

Solution

In Scala you compare two String instances with the == operator. Given these strings:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.