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"]