foldleft

Scala immutable Map class: methods, examples, and syntax alvin July 22, 2019 - 10:38am

This page contains a large collection of examples of how to use the Scala Map class. There are currently well over 100 examples.

A Scala Map is a collection of unique keys and their associated values (i.e., a collection of key/value pairs), similar to a Java Map, Ruby Hash, or Python dictionary.

This is a page from my book, Functional Programming, Simplified

Appendix: Recursion is Great, But ... (Scala’s fold and reduce)

“Folds can be used to implement any function where you traverse a list once, element by element, and then return something based on that. Whenever you want to traverse a list to return something, chances are you want a fold. That’s why folds are, along with maps and filters, one of the most useful types of functions in functional programming.”

From the book, Learn You a Haskell for Great Good!

Methods on the Scala collections classes, organized by category alvin November 18, 2017 - 4:10pm

When I wrote the Scala Cookbook, I gave each recipe and then each chapter my full attention. I thought that if I wrote each recipe as well as possible, and included important recipes in each chapter, well, I wanted each chapter to be worth the price of the entire book. That was my goal.

As a result of this effort -- and perhaps to the chagrin of my editor -- the Scala collections chapters ended up being 130 pages in length.

How to walk through a Scala collection with ‘reduce’ and ‘fold’

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.20, “How to Walk Through a Scala Collection with the reduce and fold Methods”

Problem

You want to walk through all of the elements in a Scala sequence, comparing two neighboring elements as you walk through the collection.

Scala: A “sum of the squares” algorithm using map/sum and foldLeft

As a quick Scala example, I just needed to write a “sum of the squares” algorithm for a “Pearson Correlation” function I’m working on, and initially wrote it like this using map and sum:

val sumOfTheSquares = movieRatingsMap.values.map(rating => Math.pow(rating, 2)).sum

If you know that movieRatingsMap is a Map of movies and my ratings of those movies, I think that line of code is fairly easy to read. That line can also be written like this

How to use variable names with the Scala ‘foldLeft’ method

Here’s a quick example of how to use variable names with the Scala foldLeft method:

val pSum = movies.foldLeft(0.0)((accum, element) => accum + p1Movies(element) * p2Movies(element))

In this example the variable movies is a Seq, and the variables p1Movies and p2Movies are Map objects.

A Reverse Polish Notation calculator written with foldLeft in Scala

Chapter 10 of the book, Learn You a Haskell for Great Good!, is titled, Functionally Solving Problems. In that chapter, the author describes a “Reverse Polish Notation” (RPN) calculator. If you ever used an old Hewlett-Packard (HP) calculator, you might know what that is. (At least that’s where I first learned about RPN.)

You don’t have to read it all yet, but some of that discussion is shown in this image: