Tuesday, July 19, 2016

Calculating Chris Davis' Golden Sombrero

As a major league pitcher, if you can't crack 90 mph and you make faces like this, you need to get creative to win.


Hall of Fame pitcher Greg Maddux preferred to pitch to his team's backup catcher. From what I recall he even took this so far as to call his own pitches until the backup got a feel for how Maddux wanted to approach hitters. To take it a step further, Mad Dog called his own pitches in a special way.

As soon as Maddux threw a pitch he called his next pitch based on where he, the pitcher, put his glove to receive the ball back from the catcher or how he touched his cap among other means of deception. Depending on his glove's location, be it up or down, by him or his arm out, determined what he wanted to throw next and where.

While the other team tried to steal signs by reading whichever one the catcher put down, it was too late. Maddux had already called the next pitch.

Call it baseball shorthand; a neat little trick to abbreviate the game - and throw off the opposition.

Swift also has some shorthand tricks. Today we'll cover the operator shorthand. What's an operator? Well, if we say, "1 + 2" then the "+" is the operator. As you can imagine, there are also operators for subtraction (-), multiplication (*), and division (/). Today, we'll focus on the addition and subtraction operators.

Let's say Chris Davis has 3 strikeouts in a game and then he gets rung up for the golden sombrero. How does that look in code?



OK. And now how does that look when we use the operator shorthand?



What happened? We replaced the "+ 1" with "++" and got the same result. And what happens if we change the location of the ++? Let's put it after the DavisK this time and see what happens.



What happened? We got the same result, but only when we used - or called - the print command to print out the result. What else happened? In the first two examples we calculated Davis' strikeout totals immediately, but in the last example the new total did not appear until we ran the print command. We'll get to whether or not to calculate something immediately or later in a future post.

The same applies when subtracting.

Let's say a giddy score keeper credits Crush with a home run only for Mike Trout to rob him (rough day for Davis all around). How does that look in code?



And how does it look if we use the subtraction operator?



And if we want to calculate the subtraction later?



Oh, and what's that "\n"? That refers to "insert newline" which sounds like something the computer wants you to do, but in fact, it is something it does before printing out that statement (on a new line).

When do we use operator shorthands?
Operator shorthands are most often used in loop functions. What are loops function? They are the subject of the next post. So stick around.

Challenge: Write an addition operator for a player's particular statistical category and tweet it to me (@randallmardus).

On Deck: Loops!

No comments:

Post a Comment