Getting to Done
Tonight’s presentation on higher order procedures is now listed on the Presentations page to your right. I’ll be adding some practice exercises later, but feel free to check out what I have already.
Tonight’s presentation on higher order procedures is now listed on the Presentations page to your right. I’ll be adding some practice exercises later, but feel free to check out what I have already.
Look ma, I made my own object! It doesn’t handle state that well, but at least it can be passed messages and told to do things.
def make_number(x)
lambda do |op, *y|
if op == '+'
make_number(x + y[0])
elsif op == '-'
make_number(x - y[0])
elsif op == 'val'
x
end
end
end
my_num = make_number(5) => <#Proc>
my_num.call('+', 7).call('val') => 12
In case you can’t tell, this object creation makes use of higher order procedures. The make number procedure returns the anonymous procedure created by lambda. This form of object creation is not terribly practical, but it sure is cool.
While working on the upcoming presentation, I came to the realization that I have never used Microsoft Powerpoint. Being a good dork, I decided that now was not the time to learn and instead set out to find presentation software that would allow me to post my presentation to the web. S5 to the rescue. S5 is a creation of Eric Meyer that runs on html, css, and javascript. It looks great, is easy to use, and your presentation can be viewed by anyone anywhere. Great work Eric!
So after reading this blog for a little while, you’re probably wondering “How can I be as smart as this Trotter fella?” Well, let me clue you in on the secret of my skillz. I’m reading Structure and Interpretation of Computer Programs. It may be old, but wisdom never spoils. Read it and get smart.
Time to play with synonyms. Let me know which versions you like better.
result = 0 0.upto(arr.length) do |i| result += arr[i] end
or
arr.inject{|x,y| x + y}
How about this one?
0.upto(arr.length) do |i| arr[i] = 10+arr[i] end
or
arr.map! {|x| 10 + x}
Higher-order procedures are h0t!
So, I’m sure you’re sitting there wondering what kind of bum doesn’t update his blog with more super cool posts about higher order procedures like he said he would. Well, it’s not my fault, honest. I wanted to post cool little blurbs about how fun it can be to map array elements, but big bad Websense said no. Read a little bit more, and I’ll tell you my proposed solution.
So, I’ve finally started doing actual research for this presentation beyond asking, “What do I already know?” First stop on the research train? Blocks and Closures in Ruby over at Artima. It’s an interview with Matz (the creator of Ruby for those of you not in the know) discussing how cool it is that Ruby supports blocks and closures. Read on to get an abrievated version of what goes on in the article.
Remember how I said that we would be seeing a lot more about higher-order procedures on this site? Well, I’m giving a talk on using them in Ruby this coming Tuesday. All of my research will be posted here along with the final presentation. So get ready to read, because there’s going to be a lot of information coming at you.