Using zip() For Something Useful

There are a few posts describing how the zip(_:_:) function works but not a whole lot of practical illustrations. While doing some research on Bayes' Rule, I put together a simple Swift function that implements the Odds Form of Bayes' Rule.

The initial form used a for loop to go through the two input arrays and multiply the members together:

Consider the following scenario (based on this illustration):

We are trying to solve a mysterious murder, and start out thinking the odds of Prof Plum vs Miss Scarlet vs Col Mustard committing the murder are 1:2:3 (i.e., Scarlet is twice as likely as Plum, Mustard three times as likely as Plum) a priori.

Now we learn that the victim was killed with a lead pipe. We think that Plum, *if* he was the murderer, is 60% likely to use a pipe. Scarlet, *if* she did the deed is 6% likely to use a pipe. Mustard is about 50% likely to use a pipe. The relative likelihood of Plum vs Scarlet vs Mustard using the Pipe is 10:1.

The posterior odds for Plum vs Scarlet vs Mustard are (1 : 2 : 3) * (60 : 6 : 50) = (60 : 12 : 150) = (10 : 2 : 25)

It works, but it's not pretty. Let's see how much nicer zip and map can make the same function: