"Jim Weirich" <jweirich / one.net> wrote in message
news:87y9bsd7fb.fsf / traken.access.one.net...
> >>>>> "mikkelfj" == mikkelfj-anti-spam  <MikkelFJ> writes:
>
>     mikkelfj> Finally, Ruby cannot too soon get better closure
>     mikkelfj> integration as first class values and partial evaluation
>     mikkelfj> support. This is an absolute killer feature of
>     mikkelfj> OCaml. Most of what Ruby does great is thanks to its use
>     mikkelfj> of closures and most of what Ocaml does better is thanks
>     mikkelfj> to its improved use of closures.
>
> I'm curious.  Could you give an example of this?

In the past year there have been some threads on this topic and some
examples of how this can be accoplished in Ruby search for "Ruby partial
evaluation" in Google groups.

I should remind that I've been of the group for a while and there may be
have been issues with the closure syntax since then.

An example of an anonymous closure stored in a variable:
def dosomething (x y)
  x(y)
end

sqr = {|a| a*a}
puts sqr(2) # => 4
puts dosomething(sqr, 3) # ==> 9

You can do this now, but it involves writing somewhat more.
Notice both that a function, or closure, can be stored in a variable and be
passed around to other functions.
It can be evaluated by putting parentheses on it.
The next step is partial evaluation, and for this to work, it is essential
that you can store a closure in a variable.
A closure is a microclass: it has some data and some code. The normal
closure is just a pointer to some code.
With partial evaulation, the closure also stores the value of some of the
parameters (and sometimes also parts
of the environment).

Let's partially evaulate a sum function:

sum = {|a,b| a + b}
add3 = sum(3)
puts add3(2) #==> 5

Another kind of partial evaluation that would be insanely cool is where the
object parameter is partially evaulated. This means that you can pass around
a member function. Since all functions in Ruby are member functions, this is
actually necessary in order to have function pointers, or delegates (the
infamous fight between Microsoft and Sun).
class X
  def initialize
    @name = "rock"
  end
  def identify()
    puts @name
  end
  def rename(s)
    @name = s
  end
end

Notice in the following that when no parentheses is used, the function is
not evaluated, its closure
becomes the value. This is not standard Ruby and a future Ruby syntax would
probably need to
invent something else.

a = new X
b = a.y
b() #=> rock
a.rename("flower")
b() #=> "flower"


Mikkel