From: Robert Feldt Date: 2001-11-30T14:10:33+09:00 Subject: [ruby-talk:27024] Re: first class functions in Ruby On Fri, 30 Nov 2001, MikkelFJ wrote: > > "Ralph Mason" wrote in message > news:135701c1792a$679ffc90$0101a8c0@p3... > > irb(main):010:0> def sq(x) > > irb(main):011:1> x+x > > irb(main):012:1> end > > nil > > irb(main):013:0> def aMes(f) > > irb(main):014:1> send(f,42) + send(f,27) > > irb(main):015:1> end > > nil > > irb(main):016:0> aMes :sq > > 138 > > irb(main):017:0> > > > > That is nice. It has the same level of compactness that I asked for. > The :sq handles the problem of delayed evalution. > I'd still like to be able to simply apply the parameters: > f(42) > but it isn't too bad. > What remains then is partial evaluation: > Something along the lines: class Fun def initialize(&b) @b, @as = b, [] end def [](*args) if @as.length + args.length < @b.arity @as = @as.concat args res = self else res = @b.call(*(@as + args)) end res end end def aMes(f) f[3] + f[4] end sq = Fun.new {|x,y| x*y} p aMes(sq[2]) #=> 14 ? It's not perfect since arity might surprise sometimes and there are other issues... /RobertF