Thursday, July 14, 2016

You Make The Call: Bat or Pull Kyle Seager

Baseball has no shortage of statistics, stats that allow us to draw comparisons between players, their performances, and the likelihood of this outcome or that outcome. These stats also help managers make decisions in the course of a game.


Take Seattle Mariners' third baseman Kyle Seager for instance. This year Seager has a 1.053 OPS versus right-handed pitching. Impressive. And against lefties? His OPS is only .613. Less impressive. Chances are if Seager comes up in the late innings, that the opposing skipper is going to bring in a lefty to face him at which point Seager's manager has a decision to make: Stay with Seager or go to the bench.

To help us make decisions based on numbers (whether they are integers, floats, or doubles), Swift offers a series of comparison operators for us to use. What are comparison operators? Good question. Here they are from Apple's own documentation:




The first two aside, the rest look a lot like what we learned in school, right? Here are some baseball examples:



Now back to those first two. What is the difference between the "=" assignment operator and the "==" comparison operator?

There's a big difference. Let's take a look.

As we discussed in an earlier post about assigning values to constants, when using the assignment operator (=) what we are saying is that we are attaching a certain value to a variable or constant. In the case of Miggy, we are assigning 426 to the variable miggyHRs. We are not saying that miggyHRs are equal to 426.

It's normal to think of the assignment operator (=) in mathematical terms because that's how many of us first came to know it. But here, it's more like saying, Miggy is with the Tigers. He's not equal to the Tigers. He's with them because we've assigned him to them. But that could change, especially if Miggy is a variable.




On the other hand, when we use "==" comparison operator we are saying that whatever is on the left-hand side of the "==" is equal to whatever is on the right-hand side. This is more like the equal sign we used in math class.

More so, when you use the "==" comparison operator Xcode will either return true or false. True, those items are equal or false, those items are not equal. Xcode does not return true or false when you use the assignment operator.

Similarly, we can say that these two statements are not true as we use the "does not equal" comparison operator: (!=). Here's an example comparing the career ERAs of Ed Walsh and Addie Joss:



Now let's put these comparison operators to work with conditionals. Back to Kyle Seager. If you're Seager's manager you probably have a certain cutoff at which point you prefer to pinch hit for a player. Let's say that cutoff for Seager against lefties is an OPS of 0.725 and that Seager's cutoff against righties is an OPS of 0.800.



Challenge 1: Re-create the Kyle Seager code above using the player and cutoff figures of your choice and then take a screenshot and tweet it to me (@randallmardus).

Challenge 2: Flip the script on the Seager code and write code for a pitcher and how that pitcher deals with left or right-handed batters. Then take a screenshot and tweet it to me (@randallmardus).

On Deck: Logical operators!

No comments:

Post a Comment