Categories

# 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.

## 2 replies on “F# Friday – The Set.union Function”

Just had to tell you thanks for this series, which I just stumbled upon tonight while trying to figure out reduce; thanks to the series, I think I finally understand fold as well.

I also had to give you props for the Yes album title. :)