From: Masaki Fukushima Date: 2000-07-20T18:30:33+09:00 Subject: [ruby-talk:04143] Re: What are you using Ruby for? Clemens Hintze wrote: > def f1 > return proc { |x, y| return } > end > > f1.call(1,2) > register_proc "f1", f1, "compute something" > > g1 = make_g_from_proc f1 > register_proc "g1", g1, "compute something scaled by scale" > > Now it looks equal and you have to use the same calling > convention. But I admit this is not too pretty ... Especially as > calling it via f1.call will do one indirection indeed! Using '[]' instead of 'call' makes it a little bit neater, at least to the eye. f1.call(1,2) => f1[1,2] > > def make_g_from_f(f): > > def temp(x, y, f=f): # ugly python lack of closures > > return f(x, y)*scale(x, y) > > return temp > > > > def f1(x, y): > > return > > register_function("f1", f1, "Compute something") > > > > g1 = make_g_from_f(f1) # useful python 1st-class functions > > register_function("g1", g1, "Compute something scaled by scale") Above code fragment can be written as follows using '[]': def make_g_from_f(f) return proc{|x,y| f[x, y] * scale(x, y) } end f1 = proc{|x, y| } register_function("f1", f1, "Compute something") g1 = make_g_from_f(f1) register_function("g1", g1, "Compute something scaled by scale") In a way this looks simpler than the original in python. You only have to remember to use '[]' instead of '()' when calling Proc. Masaki Fukushima