Daishi Harada <daishi / cs.berkeley.edu> writes:

> Hi,
> 
> I'm wondering what the right way to do the following in Ruby, expressed
> here in Lisp:
> 
> (defun wrap (fn) (lambda (x) (some-fn (fn x))))
> 
> I was looking at the Procedure object, but it appears that
> Procedures require the method invocation 'call' to execute the
> actual procedure body. Is there syntactic sugar somehow which makes
> Procedure objects callable using the "normal" syntax of fn(x)?
> (Somewhat like defining __call__ in Python?) I realize I'm mashing
> functional idioms into an OO language. Perhaps someone can generally
> enlighten me as to the right way to do this kind of thing?

You can use '[]' as an alias for call, so something like

  def some_fn(n)
    n * 2
  end

  def wrap(&fn)
    proc {|x| some_fn(fn[x])}
  end


  add_3_times_2 = wrap {|x| x + 3}

  add_3_times_2[4]  #=> 14
  add_3_times_2[-2] #=> 2


I'm not sure if there's a parsing reason that you can't overload '()'.


Dave