> -----Original Message----- > From: Rafael 'Dido' Sevilla [mailto:dido / imperium.ph] > Sent: Wednesday, August 21, 2002 11:37 PM > To: ruby-talk ML > Subject: blocks and lambdas, or blocks as first-class entities > > As I'm a fan of functional languages as well, feeling comfortable with > languages such as OCaml (which seems to be a favorite other language for > many people on this list), I've noticed that Ruby's blocks look > suspiciously like lambda abstractions in functional languages, just with > a very quirky syntax. They're pretty much used in the same way, as > parameters to functions, although they're far more limited in scope than > a lambda would be in a Scheme or OCaml, and require somewhat special > treatment. What if we made a block a first class entity, allowing one > to assign a block to a variable, and pass a block as a formal parameter > to a method? This of course would also mean making a block an object, > which in my opinion is a very powerful idea. So you might have some > code that looks like this: > > def foo(b1, b2) > b1.execute(a, b, c) > b2.execute(d, e) > end > > tmp_blk = { |a, b| ... } > > foo({ |a, b, c| ... }, tmp_blk) > > {|a| ... }.execute(b) Procs are not a clean looking as plain blocks...but... def foo(p1, p2) p1.call(1,2,3) p2.call(4,5) end tmp_proc = proc { |a, b| puts a,b} foo(proc { |a,b,c| puts a,b,c }, tmp_proc) #=> 1,2,3,4,5 proc {|a| puts a }.call(1) #=> 1 -Rich > > The yield builtin keyword would do the same thing as invoking the > execute method on the first (possibly implicitly specified) parameter. > > What do you guys think? > > -- > Rafael R. Sevilla <dido at imperium dot ph> +63(2)8123151 > Software Developer, Imperium Technology Inc. +63(917)4458925