From: Gregory Seidman Date: 2007-05-26T02:59:33+09:00 Subject: Re: Introducing the "it" keyword On Fri, May 25, 2007 at 04:58:08PM +0900, Ronald Fischer wrote: > > This is obviously suboptimal code, as is, because it results in the > > evaluation of m(x) twice. I propose a new keyword is added, "it", > > which may appear within the statement to the left of the decorator. > > So, the previous statements become: > > > > puts "User: #{it}" if opts[:user] > > > > and > > > > return it if |v + 1| < 10 > > IMO, this is a special case of the more general idea that within a > certain piece of code, the value of an expression needs to be refered to > more offen. > > Looking at how others do this, the functional programming / Scheme people > usually handle it with a 'let' construct: > > let v = opts[:user] in > puts "User: #{v}" if v > > Of course this has the disadvantage that we *have* to invent a new name > (v). But assuming that such a substition is only done locally, we could > as well use placeholders. 'it' would be such a placeholder, kind of: > > let opts[:user] in puts "User: #{it}" if it > > but this would restrict us to have only one binding at any time. A more > general solution could be like this: > > let a+b+c in > let foo=d*e in > let x/y in > puts ($1+foo)*($1-$2)-$2/($1-foo) > > Here, $1 would be bound to x/y (innermost unnamed let binding), $2 would > be bound to a+b+c (second innermost unnamed let binding). This is similar > to the binding of parameters in the DeBrujin Lambda calculus, if my rusty > memory is right (any FP people here who can comment on this?). [...] You're working too hard: def let(*args) yield *args end def use_let(a,b,c) let a+b+c do |it| return it if it < 10 end end use_let(1,2,3) # 6 use_let(4,5,6) # nil No need for additional keywords or even global variables, just a little method named let. > Ronald --Greg