Archive for Higher Order Procedures

nyc.rb In The House!

Last night’s meeting was a smashing success. We had our largest turnout ever with what I think was 28 people. My presentation went very well, with only a few minor corrections coming from the illustrious David Black. For those of you following along via the web, I’ve updated the copy of the presentation available here to reflect those corrections. Moving past my accomplishments, Zed Shaw gave a highly informative presentation on the use of statistics to gauge performance. Also, Greg Brown talked about his reporting library Ruport, which looks very interesting. Needless to say, the meeting was amazing, and I hope to see you at the next one.

Comments

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.

Comments

Objects from Scratch

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.

Comments

Synonyms

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!

Comments (3)

Blocks and Closures in Ruby

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.

Read the rest of this entry »

Comments