Categories
Tech

F# Friday – The fold Function

Last week we looked at the reduce operation, and we observed three properties:

  • Using reduce on an empty collection yields an exception.
  • You can only reduce a collection to a value of the same type as the elements in the collection.
  • The order of the items in the collection (usually) matters.

We also noticed that there are several common operations—sum, product, and string concatenation—that are just special cases of reduce.

As it happens, reduce is itself a special case of a more fundamental operation: fold. Furthermore, while order still matters, fold can

  • handle empty collections, and
  • reduce a collection of one type to a value of a different type.

Why is that? First, fold takes a binary operation just as reduce does, but it also takes a starting value in addition to the collection. That is how fold can handle empty collections. If the collection is empty, you’re just left with the starting value. Second, because you give fold a starting value, that starting value could be of any type; it doesn’t have to match the type of the items in the collection. The reason reduce can only reduce a collection to a value of the same type is because the only starting value it has is the first value in the collection.

Let’s put fold into action.

Our Product Line

We implemented a product operation last week with reduce. Let’s do it this week with fold. This figure illustrates what’s going on:

The fold operation produces the product of the list of integers [5,3,6] by starting with 1 (because 1 times x is always x), multiplying 1 and 5 to get 5, multiplying 5 and 3 to get fifteen, and finally multiplying 15 and 6 to get a final product of 90.
Producing the Product of a List of Integers Using Fold

In multiplication, 1 is the identity value. That is, 1 times x is always x. So then, if you want to calculate the product of a list of integers, make your starting value 1. Here is the code:

let product = [5;3;6] |> List.fold (*) 1
// val product : int = 90

Notice, by the way, that we are doing something we did not do last week: Using the multiplication operator as the input function rather than defining a lambda. In other words, it is superfluous to define a lambda (fun x y -> x * y) because the multiplication operator (*) is already a function that takes two numbers and multiplies them together. Let’s just use it and cut down on some noise!

Now what if the list is empty? We cannot handle an empty list with reduce, but what does fold do?

let product = [] |> List.fold (*) 1
// val product : int = 1

So then, when fold receives an empty collection, it just returns the starting value—in this case, 1.

Stringing You Along

Last week we also implemented String.concat with reduce. We can do the same thing with fold but there are some gotchas.

Try a straightforward approach:

let illJoined =
  ["do";"mi";"sol"]
  |> List.fold (fun x y -> x + "-" + y) ""
// val illJoined : string = "-do-mi-sol"

Eek! What happened? You don’t want the extra hyphen on the front! You just want hyphens in between the elements!

What if you have a list with just one item?

let illJoined =
  ["do"]
  |> List.fold (fun x y -> x + "-" + y) ""
// val illJoined : string = "-do"

No better. The following figure shows you what has happened:

The fold operation can concatenate a list of strings  together with a separator, but this figure illustrates how the desired result is a little more complicated than it is with reduce.  Taking a list of strings ["do", "mi", "sol"], a starting value of an empty string, and a binary operation that concatenates two strings together with a hyphen in the middle, you end up with an extra hyphen at the front of the resulting string. That is because the first application of the binary operation concatenates the empty string with a hyphen and "do". In a join operation, you usually only want the separator between values, so using fold requires some additional checking.
Using Fold to Join a List of Strings Together with a Separator

While reduce cannot handle an empty collection, it only starts applying the reduction operation on the first two elements. On the other hand, fold applies the binary operation on the starting value and the first item in the list.

To do String.concat right with fold, you have to account for some special cases:

let join xs =
  if xs |> List.isEmpty then "" 
  elif xs |> List.length = 1 then
    xs.Head.ToString()
  else
    xs.Tail
    |> List.fold (fun x y -> x + "-" + y) xs.Head
let emptyJoined = join []
// val emptyJoined : string = ""
let singleJoined = join ["do"]
// val singleJoined : string = "do"
let manyJoined = join ["do";"mi";"sol"]
// val manyJoined : string = "do-mi-sol"

From Type to Type

Finally, consider an example of something fold can do that reduce cannot. If reduce receives a list of integers, it can only produce a single integer. If it receives a list of strings, its result is a single string. In contrast, fold can take a list of integers and produce a string. Or it could take a list of strings and produce a list of integers.

For instance, you can use fold to reverse a list:

let reversed =
  [1..10]
  |> List.fold (fun xs x -> x :: xs) []
// val reversed : int list =
//   [10; 9; 8; 7; 6; 5; 4; 3; 2; 1]

Notice how the starting value is a list, but the type of the elements in the input list int, not int list. Because the starting value can be a type that is different from the type of the input list elements, the binary operation can transform the elements in the list to match the type of the starting value. In fact, that is the constraint with fold: the type of the result must be type of the starting value. If your starting value is a list, fold must return a list. If your starting value is an integer, fold must return an integer.

Another example is determining the length of the longest string in a list of strings:

let longest =
  ["a";"borborygmus";"sesquipedalian";"small"]
  |> List.fold (fun n s -> max n s.Length) 0
// val longest : int = 14

Here is the documentation on fold in each collection module that defines it:

As an exercise, you could try implementing map with fold.

Categories
Tech

Scala Saturday – The reduce Method

If you have a collection of numbers that you want to sum or multiply together, Scala’s collection classes all define both a sum operation and a product operation:

ScalaDocs for Scala Collection sum and product Methods
Array sum product
List sum product
Seq sum product
Set sum product

But what if, for some reason, you want to implement sum or product yourself? Well, it turns out that sum and product are both just special cases of a more general operation that the collection modules also define: reduce. The reduce operation takes all the elements in a collection and combines them in some way to produce a single value. It uses a binary operation to combine the first two values. Then it takes the result of that first combination and combines it with the next element in the collection, and then the next, and so on through the end of the collection.

Here’s an illustration of how to use reduce to implement a product operation:

Taking a list of [2,5,3,6], reduce multiplies 2 and 5 to produce 10, then 10 and 3 to produce 30, and finally 30 and 6 to get the final result of 180.
Using Reduce to Calculate the Product of All Numbers in a List

The code looks like this:

val product = List(2,5,3,6) reduce {
  (x,y) => x * y
}
// product: Int = 180

You can be even more succinct by employing the underscore, Scala’s shorthand for the lambda’s input arguments:

val product = List(2,5,3,6).reduce(_*_)
// product: Int = 180

Another special case of reduce is mkString, e.g., List.mkString—also known as a join operation—which allows you to concatenate a sequence of values together into a string with a separator between each value. Here’s how it works:

Taking a list of strings ["do","mi","sol","do"], reduce combines "do" and "mi" to produce "do-mi", and then combines "do-mi" and "sol" to produce "do-mi-sol", and finally "do-mi-sol" and "do" to produce the final result of "do-mi-sol-do"
Using Reduce to Join a List of Strings Together with a Separator

See how similar it is to the product operation above? If you should want to implement mkString yourself, you could use reduce like this:

val joined =
  List("do","mi","sol","do") reduce {
    (x,y) => x + "-" + y
  }
// joined: String = do-mi-sol-do

Or again, with the shorthand:

val joined =
  List("do","mi","sol","do").
    reduce(_ + "-" + _)
// joined: String = do-mi-sol-do

Here is the documentation on reduce in each of the collection classes:

Now a few of caveats about reduce:

  • You cannot use reduce on an empty collection. You will get an exception if you do, so make sure you check to make sure your collection is not empty before you pass it to reduce. (Scala provides an alternative, reduceOption, that does not throw an exception, but represents the result as an Option.)

  • The result of a reduce operation is always the same type as the elements in the collection. In other words, you can only reduce a collection of type A to a value of type A. You cannot reduce a collection of type A to a value of type B.

  • Finally, order matters if the order of your binary operation matters. In other words, with addition and multiplication, order does not matter. That is, a + b is the same as b + a. (Mathematicians say that such a function is commutative.) But with something like subtraction, order does matter. In other words, a − b does not necessarily produce the same result as b − a. So make sure that your binary operation applies the reduction to its operands in the correct order.

Having noted these caveats, next week, we will cover the fold operation. It operates almost the same way as reduce in that it walks a collection, applying a binary operation to each element. The fold operation cannot help us with that last point—order still matters—but it can handle an empty collection and, if necessary, produce a result that is of a type different from the type of the source collection’s elements.

Categories
Tech

F# Friday – The reduce Function

If you have a sequence of numbers that you want to sum, F# defines the sum operation in all of the sequential collection modules:

But what if you want to multiply all those numbers together? Well, it turns out that sum is just a special case of a more general function that the sequential collection modules also define: reduce. The reduce operation takes all the elements in a collection and combines them in some way to produce a single value. It uses a binary operation to combine the first two values. Then it takes the result of that first combination and combines it with the next element in the collection, and then the next, and so on through the end of the collection.

Here’s an illustration of how to use reduce to implement a product operation:

Using Reduce to Calculate the Product of All Numbers in a List
Using Reduce to Calculate the Product of All Numbers in a List

The code looks like this:

let product =
  [2;5;3;6]
  |> Seq.reduce (fun x y -> x * y)
// val product : int = 180

Another special case of reduce is String.concat—also known as a join operation—which allows you to concatenate a sequence of strings together with a separator in between each. Here’s how it works:

Using Reduce to Join a List of Strings Together with a Separator
Using Reduce to Join a List of Strings Together with a Separator

See how similar it is to the product operation above? If you should want to implement String.concat yourself, you could use reduce like this:

let joined =
  ["do";"mi";"sol";"do"]
  |> Seq.reduce (fun x y -> x + "-" + y)
// val joined : string = "do-mi-sol-do"

Here is the documentation on reduce in each of the sequential collection modules:

Now a few of caveats about reduce:

  • You cannot use reduce on an empty collection. You will get an exception if you do, so make sure you check to make sure your collection is not empty before you pass it to reduce.

  • The result of a reduce operation is always the same type as the elements in the collection. In other words, you can only reduce a collection of type A to a value of type A. You cannot reduce a collection of type A to a value of type B.

  • Finally, order matters if the order of your binary operation matters. In other words, with addition and multiplication, order does not matter. That is, a + b is the same as b + a. (Mathematicians say that such a function is commutative.) But with something like subtraction, order does matter. In other words, a − b does not necessarily produce the same result as b − a. So make sure that your binary operation applies the reduction to its operands in the correct order.

Having noted these caveats, next week, we will cover the fold operation. It operates almost the same way as reduce in that it walks a collection, applying a binary operation to each element. The fold operation cannot help us with that last point—order still matters—but it can handle an empty collection and, if necessary, produce a result that is of a type different from the type of the source collection’s elements.

Categories
Tech

Scala Saturday – The Set.intersect Method

Last week we looked at Set.union. This week we look at Set.intersect. When dealing with sets (which, remember, by definition, cannot contain duplicate elements), sometimes you have two sets, and you want to know what elements they have in common. That is called the intersection of two sets.

Maybe you have some crazy password policy:

  • Passwords must be at least 12 characters;
  • Each character must be unique; and
  • New passwords may have no more than four characters in common.

You can use a set intersection to determine whether an old password and a new password have too many characters in common:

val oldPwd = "aU*E3)vn'2-="

val newPwd1 = "Uv0&*n2'EI~5"
val common1 = oldPwd.toSet intersect newPwd1.toSet
// common1: scala.collection.immutable.Set[Char] =
//   Set(E, *, n, U, v, ', 2)
// Too many characters in common!

val newPwd2 = "Uv0&6;2'ZI~/"
val common2 = oldPwd.toSet intersect newPwd2.toSet
// common2: scala.collection.immutable.Set[Char] =
//   Set(U, v, ', 2)
// Just right!
Categories
Tech

F# Friday – The Set.intersect Function

Last week we looked at Set.union. This week we look at Set.intersect. When dealing with sets (which, remember, by definition, cannot contain duplicate elements), sometimes you have two sets, and you want to know what elements they have in common. That is called the intersection of two sets.

From Genesis to Revelation

The band Genesis, like many bands that have been around as long as they have, has undergone several lineup changes. When they recorded their first album, From Genesis to Revelation, Genesis consisted of Peter Gabriel, Tony Banks, Anthony Phillips, Mike Rutherford, and John Silver. You can put their names in a set:

let on1stAlbum =
  set ["Gabriel"; "Banks"; "Phillips";
       "Rutherford"; "Silver"]

Calling All Stations

Almost thirty years later, Genesis recorded their last studio album (to date), Calling All Stations. They had had several lineup changes through the years, and by this time, the members of Genesis were Ray Wilson, Tony Banks, and Mike Rutherford. Put their names into a set:

let onLastAlbum =
  set ["Wilson"; "Banks"; "Rutherford"]

Were there any members—keepers of the flame, as it were—who stayed with the band the entire time? Perform a set intersection to find out:

let keepersOfTheFlame =
  Set.intersect on1stAlbum onLastAlbum
// val keepersOfTheFlame : Set<string> =
//   set ["Banks"; "Rutherford"]
Categories
Tech

Scala Saturday – The Set.union Method

A defining property of mathematical set is that it contain no duplicates. Sometimes you need to combine two sets. But what if those sets contain some of the same elements, so that the combination would contain duplicates? No worries! That’s where Set.union comes in.

Journey from Mariabronn

The classic lineup of progressive rock band Kansas in the 1970s consisted of Steve Walsh, Kerry Livgren, Rich Williams, Robby Steinhardt, Dave Hope, and Phil Ehart. However, over the years, the lineup changed several times. Currently Kansas are Ronnie Platt, David Manion, Rich Williams, David Ragsdale, Billy Greer, and Phil Ehart.

You can represent these two lineups as sets:

val classicKansas = Set(
  "Walsh", "Livgren", "Williams",
  "Steinhardt", "Hope", "Ehart")
val currentKansas = Set(
  "Platt", "Manion", "Williams",
  "Ragsdale", "Greer", "Ehart")

Magnum Opus

What if the classic Kansas lineup got together with the current Kansas lineup for a reunion tour? How would the omnibus lineup look? Call the first set’s union method, and pass it the second set:

val totalKansas = classicKansas union currentKansas
// totalKansas: scala.collection.immutable.Set[String] =
//   Set(Ehart, Walsh, Steinhardt, Platt, Ragsdale,
//     Manion, Greer, Livgren, Hope, Williams)

Notice how the union of the two sets does not contain duplicates even though Ehart and Williams were in both lineups.

Categories
Tech

F# Friday – The Set.union Function

A defining property of mathematical set is that it contain no duplicates. Sometimes you need to combine two sets. But what if those sets contain some of the same elements, so that the combination would contain duplicates? No worries! That’s where Set.union comes in.

Shock to the System

The classic lineup of progressive rock band Yes in the 1970s consisted of Jon Anderson, Steve Howe, Chris Squire, Rick Wakeman, and Bill Bruford. However, over the years, the lineup changed so that the most stable incarnation of Yes in the 1980s was Jon Anderson, Chris Squire, Trevor Rabin, Tony Kaye, and Alan White.

You can represent these two lineups as sets:

let yes70s = 
  ["Anderson"; "Howe"; "Squire"; "Wakeman"; "Bruford"]
  |> Set.ofList
let yes80s =
  ["Anderson"; "Rabin"; "Squire"; "Kaye"; "White"]
  |> Set.ofList

Union

In 1990, the record company was ready for a new Yes album. Someone had the bright idea of recording an album called Union: It would unite the classic 70s Yes lineup with the 80s lineup all on one album.

How would the lineup look on the Union album? Just pass both sets to Set.union:

let yesUnion = Set.union yes70s yes80s
// val yesUnion : Set<string> =
//   set
//     ["Anderson"; "Bruford"; "Howe"; "Kaye";
//      "Rabin"; "Squire"; "Wakeman"; "White"]

Notice how the union of the two sets does not contain duplicates even though Anderson and Squire were in both lineups.

Categories
Tech

Scala Saturday – The flatMap Method

In a previous Scala Saturday post, we looked at the map method. As a quick review, map lets you take a collection of elements, calculate a new value from each source element, and create a new collection containing those new values. The mapping function has the following signature:

A => B

In other words, the function takes a single value of type A and returns a single value of type B.

But what if you have a mapping function with this signature?

A => List[B]

In other words, this function does not take one value and produce one value. Rather it takes one value and produces multiple values. What if you’re OK with that, but you just need to take those many values returned from each mapping and smash them all together into one list?

From Side to Side

Most of the guys in legendary progressive rock band Yes didn’t have just one job on their Close to the Edge album. They performed multiple duties. So let’s create a Musician case class to represent a musician and the list of instruments he plays on the album:

case class Musician(
  name: String,
  instruments: List[String])

Now you can create a list of musicians:

val yes = List(
  Musician("Jon Anderson", List("vocals")),
  Musician(
    "Steve Howe",
    List("electric guitars", "acoustic guitars",
      "vocals")
  ),
  Musician(
    "Chris Squire",
    List("bass guitar", "acoustic guitars",
      "vocals")
  ),
  Musician(
    "Rick Wakeman",
    List("Hammond organ", "Minimoog",
      "Mellotron", "grand piano",
      "RMI 368 Electra-Piano and Harpsichord",
      "pipe organ at St. Giles, Cripplegate church")
  ),
  Musician(
    "Bill Bruford",
    List("drums", "percussion")
  )
)

Maybe you want one long list of every instrument the fellows in Yes used on the album. You just have to map each musician to his list of instruments, and then concatenate those lists together:

Illustrates mapping a list of musicians (jon, steve, chris, rick, nill) to a list of lists of the instruments that each musician plays ((vocals), (electric guitars, acoustic guitars), (bass guitar, vocals), (Hammond organ, Minimoog, ...), (drums, percussion)) and then a flattening of those nested lists into one long list
Collecting Musicians’ Instruments into One Long List

You could do this with two functions you’ve read about already on Scala Saturdays: map and concat:

val instruments = yes.map(_.instruments).flatten
// instruments: List[String] =
//   List(vocals, electric guitars, acoustic guitars,
//     vocals, bass guitar, ...)

The Total Mass

What if there were a function that could do both of those things—perform the transformation and the concatenation all in one? Turns out that there is: flatMap!

val instruments = yes flatMap (_.instruments)
// instruments: List[String] =
//   List(vocals, electric guitars, acoustic guitars,
//     vocals, bass guitar, ...)

How ’bout that? The very same results all in one function!

So then, flatMap performs a map and a flatten all in one function call.

You’ve seen flatMap examples here only in terms of lists, but the Scala’s other collections define the flatMap method, too:

Categories
Language

The Oxford Comma

Examples of why the Oxford comma is absolutely necessary:

Among those interviewed were Merle Haggard’s two ex-wives, Kris Kristofferson and Rober Duvall.

This book is dedicated to my parents, Ayn Rand and God.

Alexander MacDonald via Twitter

Categories
Tech

F# Friday – The collect Function

In a previous F# Friday post, we looked at the map function. As a quick review, map lets you take a collection of elements, calculate a new value from each source element, and create a new collection containing those new values. The mapping function has the following signature:

'T -> 'U

In other words, the function takes a single value of type 'T and returns a single value of type 'U.

But what if you have a mapping function with this signature?

'T -> 'U list

In other words, this function does not take one value and produce one value. Rather it takes one value and produces multiple values. What if you’re OK with that, but you just need to take those many values returned from each mapping and smash them all together into one list?

The Universe Divided

The guys in Canadian power trio Rush didn’t just stick to one instrument on their Hemispheres album. Each of them played a collection of instruments. So let’s create a Musician record type to represent a musician and the list of instruments he plays on the album:

type Musician = {
  Name : string
  Instruments : string list
}

Now you can create a list of musicians:

let rush = [
  {
    Name = "Geddy Lee";
    Instruments = 
    [
      "vocals"; "bass guitar"; 
      "Oberheim polyphonic"; 
      "Minimoog"; "Moog Taurus pedals"
    ]
  };
  {
    Name = "Alex Lifeson";
    Instruments = 
    [
      "electric and acoustic guitars";
      "classical guitar"; "guitar synthesizer"; 
      "Moog Taurus pedals"
    ]
  };
  {
    Name = "Neil Peart";
    Instruments =
    [
      "drums"; "orchestra bells"; "bell tree";
      "timpani"; "gong"; "cowbells";
      "temple blocks"; "wind chimes"; "crotales"
    ]
  }
]

Maybe you want one long list of every instrument the fellows in Rush used on the album. You just have to map each musician to his list of instruments, and then concatenate those lists together:

Illustrates mapping a list of musicians (geddy, alex, neil) to a list of lists of the instruments that each musician plays ((vocals, bass guitar, ...), (electric guitars, acoustic guitars, ...), (drums, orchestra bells, ...)) and then a flattening of those nested lists into one long list
Collecting Musicians’ Instruments into One Long List

You could do this with two functions you’ve read about already on F# Fridays: map and concat:

let instruments =
  rush
  |> List.map (fun m -> m.Instruments)
  |> List.concat
// val instruments : string list =
//   ["vocals"; "bass guitar"; "Oberheim polyphonic";
//    "Minimoog"; "Moog Taurus pedals"; ...]

Heart and Mind United

What if there were a function that could do both of those things—perform the transformation and the concatenation all in one? Turns out that there is: collect!

let instruments =
  rush |> List.collect (fun m -> m.Instruments)
// val instruments : string list =
//   ["vocals"; "bass guitar"; "Oberheim polyphonic";
//    "Minimoog"; "Moog Taurus pedals"; ...]

How ’bout that? The very same results all in one function!

So then, collect performs a map and a concatenation, or a flatten, all in one function call. In fact, some languages name this operation flatMap or flat_map.

You’ve seen collect examples here only in terms of lists, but the F#’s Array and Seq modules define the collect operation, too: