After long time Yankee PA announcer Bob Sheppard passed away, Derek Jeter continued to use The Voice of God to announce his at-bats.
Often, PA announcers use a set formula. For example, "Now batting, #2, Derek Jeter, #2." Then, for the next hitter, the same formula with different inputs: "Now batting, #21, Paul O'Neill, #21."
But what if you had to announce a million different names and numbers in a matter of seconds? Kinda hard, right? Not so hard for a computer, especially if they use string interpolation.
What is string interpolation?
According to The Big Nerd Ranch Guide to Swift Programming, string interpolation "lets you combine constant and variable values into a new string. You can then assign th[at] string to a new variable or constant, or just print it to the console." OK, well, what does that look like? Here's an example from the first home game in Yankee Stadium in 1923:

The nice thing about string interpolation is that you know it when you see it. How? See that \(...)? If you want to use string interpolation in Swift you need to enclose it in those three symbols: \(). Can't do it without them.
Why is string interpolation important?
String interpolation automates something that would otherwise require us to fill in manually over and over again which is a pain in the ass.
When do you use string interpolation?
Use string interpolation whenever you have a set string that just needs a little tweaking. In the case above, the set string is: "Now batting, your (insert position), (insert name)!" You can also use it to announce pitching changes or roster moves ("Today the (insert team) optioned (insert player) to (insert minor league location) and recalled (insert other player) from (insert other minor league location)"), for example.
Challenge 1: Create a batting order for your favorite team and use string interpolation to have your team's announcer call the order and tweet it to me (@randallmardus).
Challenge 2: Have an announcer announce a pitching change. ("Now pitching for (team), (what handed pitcher or number), (name of pitcher)!" and tweet it to me (@randallmardus).
On Deck: Conditionals!
No comments:
Post a Comment