Expressive Regular Expressions
Posted on October 8, 2015
Posted on October 8, 2015
George Frideric Handel (1685-1759) wrote operas. Or in other words, he set poetic texts to music, bringing to life narratives about queens and kings, husbands and jilted wives, and victorious, if lovestruck military leaders. The poetic texts (i.e., librettos) contain a slew of clever devices: alliteration, anaphora, internal rhyme, etc. The librettos also include instances of meaningful word repetition. Most interestingly, the repetition of a word or phrase does not occur immediately, e.g., a character makes a statement and, several scenes later, the character, with added emphasis, repeats it.
...show more...
These poetic moments of text repetition can be hard to identify. Happily, Ruby can help. Ruby has a "regular expression" operator. A regular expression (Regexp) presents a snippet of text, numbers, or other such characters. It compares this snippet against a string, checking to see if the snippet exists.
Ruby contains a variety of notations for Regexp. Let's say, I want to search for the word "figlio" in the first line of an aria. I can write a code like this:
/figlio/ =~ "Vieni, o figlio, e mi consola"
The backslashes enclose the Regexp, and the =~ operator checks the text snippet against the string. If the snippet occurs, then Ruby returns its index number (in this example, the index 9). If the snippet does not occur, then Ruby returns nil. Conveniently, the order of things does not matter. The following works just the same:
"Vieni, o figlio, e mi consola" =~ /figlio/
Ruby offers another operation to search for regular expressions, as follows:
/figlio/.match("Vieni, o figlio, e mi consola")
With the .match method, Ruby literally checks for "matches" of the text snippet within the given string. If it occurs, then Ruby returns a MatchData object, verifying the existence of the Regexp.
Regexp can search for broader swaths of information. For instance, I may want to search for numbers within a text. To do so, I can use a bracket expression: /[[:digit:]]/. Alternatively, I might want to search for letters within a string of numbers: /[[:alpha:]]/. Indeed, Ruby presents several "bracket expressions," as summarized here. Ruby thus helps us appreciate the expressive, sometimes fascinating occurrence of patterns (in opera or elsewhere) that might otherwise pass by unnoticed. And that's something to sing about.
...show less...
Breathe, Code, Repeat
Posted on October 3, 2015
Posted on October 3, 2015
I like free things, especially during this temporary state of non-employment. And since arriving in Chicago, I have attended, or more frankly, taken shameless advantage of free, try-us-out-before-you-buy-a-membership-even-if-you-have-no-intention-of-doing-so yoga at two studios (six classes total, six different instructors). Until last week, I did not fully appreciate the great variety in "yogi" practices. Yoga entails the mindful repetition of postures in sequence, which balance the body, regulate breath, and create mental focus. Though motivated by the same end goal, yoga instructors, as I now know, cultivate their own sequences of repetition – using modified postures and personalized extensions, performed at different speeds to select music (or, sometimes, no music at all).
...show more...
A program running on JavaScript or Ruby can simulate yoga cycles. These languages provide a way to construct loops, though – like two yoga instructors residing on opposite ends of Chicago – the syntax of repetition varies. The WHILE (Ruby) and FOR (JavaScript) loops provide examples. Such loops tell the computer to repeat something, as a certain condition holds true. In Ruby, a WHILE loop looks like this:
cycle_num = 1 while cycle_num <= 6 puts "You are doing #{cycle_num} of 6 horse poses." cycle_num += 1 end
The "cycle_num" variable sets a counter. The program executes the given code (a simple puts statement), while the counter remains less than a given number.
A FOR loop in JavaScript works the same as the WHILE loop in Ruby. A FOR loop looks like this:
for (x = 1; x <= 6; x++) { console.log("You are doing " + x + " of 6 horse poses."); }
The loop sets a variable (x), a condition (while x is less than or equal to), and an additive process (add one to x with every iteration). While the loop condition evaluates as true, the program executes the code between the curly brackets. The Ruby and JavaScript loops produce the same result:
You are doing 1 of 6 horse poses. You are doing 2 of 6 horse poses. You are doing 3 of 6 horse poses. You are doing 4 of 6 horse poses. You are doing 5 of 6 horse poses. You are doing 6 of 6 horse poses.
Regardless of the language, you can attain the same programming goals (at least with fundamental problems), just as you can achieve similar fitness, flexibilty, and mental goals with any yoga teacher, in any city, at any cost.
...show less...