[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04143] Re: What are you using Ruby for?

From: Masaki Fukushima <fukusima@...>
Date: 2000-07-20 09:30:33 UTC
List: ruby-talk #4143
Clemens Hintze <c.hintze@gmx.net> wrote:
>   def f1
>     return proc { |x, y| return <something> }
>   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 <something>
> > 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| <something> }
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

In This Thread

Prev Next