[OT] Partial functions?

Partials are great for deferring part of a computation
until later- they're useful in some of the same circumstances
as closures. They're also really easy to use in ruby already.

  def multiply(x, y); x * y; end

  multiply_by_3 = lambda { |x| multiply(3, x) }

···

-----Original Message-----
From: Berger, Daniel [mailto:Daniel.Berger@qwest.com]
Sent: Thursday, May 18, 2006 10:28 AM
To: ruby-talk ML
Subject: [OT] Partial functions?

Hi all,

I'm trying to understand a new feature of Python 2.5 (PEP 309) called
"partial functions". Here's the Python example I pulled from Vincent
Foley's blog:

import functional
  
def multiply(x, y):
  return x * y
  
multiply_by_3 = functional.partial(multiply, 3)
  
multiply_by_3(4) #=> Will return 12

I understand what it's doing, I just don't understand what purpose of
partial functions would be. Can someone provide a use case for me?

Also, can/should we do this in Ruby?

Regards,

Dan

This communication is the property of Qwest and may contain confidential
or
privileged information. Unauthorized use of this communication is
strictly
prohibited and may be unlawful. If you have received this communication

in error, please immediately notify the sender by reply e-mail and
destroy
all copies of the communication and any attachments.

Yesterday* Brian Mitchell *introduced me to the work of *MenTaLguY* where he
talks about 'Promise' and 'Future'; Future sounds very similar to partial
functions.

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6956dd6e4a00258

···

On 5/18/06, Joe Bowers <JoeBowers@bclip.com> wrote:

Partials are great for deferring part of a computation
until later- they're useful in some of the same circumstances
as closures. They're also really easy to use in ruby already.

  def multiply(x, y); x * y; end

  multiply_by_3 = lambda { |x| multiply(3, x) }

I've been keen to learn more about closures, but haven't made the time to do so. I just did something I think might be similar to this yesterday. To use the running example, instead of using the closure, I defined

def multiply_by_3(x)
  multiply(3,x)
end

I'm using this in a class as an instance method. As I understand it, if I were to use the closure syntax, I'd do

def multiply_by_3(x)
  lamda { |x| multiply(3,x) }
end

Am I following correctly? Or is this an opportunity for me to hit the books?

Michael Glaesemann
grzm seespotcode net

···

On May 18, 2006, at 23:39 , Joe Bowers wrote:

Partials are great for deferring part of a computation
until later- they're useful in some of the same circumstances
as closures. They're also really easy to use in ruby already.

  def multiply(x, y); x * y; end

  multiply_by_3 = lambda { |x| multiply(3, x) }

I did introduce that but it isn't quite right to call them the same
thing. One is delay of complete execution (Future/Promise) and the
other is delay for complete application of arguments (Partial
Application/Currying). There are, as mentioned, libraries that
implement currying in Ruby. There are also many ways to curry and many
semantics to chose (execution that is strict, eager, lazy, etc..).

I have used currying many times for real world things to specialize
the interface of some code while boosting the generic properties of
other code. I had curry extensions used in Rails validations for a
complex application. The plus came down to allowing me to write only a
handful (less than ten) core validators and have many more different
combinations actually used across some very rich models. This helped
me keep my code DRY and very maintainable.

Haskell, another beautiful language, makes good use of currying:

Jim's example in Haskell:
map (*3) [1,2,3]

To see even deeper we can check the infered type of (*) versus (*3):
(*) :: (Num a) => a -> (a -> a)
(*3) :: (Num a) => a -> a

Aside from explaining the syntax [1], you might notice that (*) is a
function which actually returns a function which returns a result on
that final application. So with this knowledge we could then take the
map example one step further:

mapMultBy3 = map (*3)
mapMultBy3 [1,2,3]

The type of mapMultBy3 is easily figured out by thinking of what you
pass in and get back out (lists of numbers):

mapMultBy3 :: (Num a) => [a] -> [a]

This parametric aggregation is really powerful when understood. It
also complements many other things like general high order programming
really well [2]. In the end it is useful in Ruby if the use is chosen
carefully (and sparingly). It may not be first class (for good reason
IMO) but as we've seen that is hardly a reason not to consider it in
some cases.

Hope this didn't get too confusing! </rambling>
Brian.

[1] I *highly* recommend people learn a _few_ functional programming
languages. Haskell is my favorite at the moment but others seem to
like OCaml and other ML dialects. Erlang is functional but is quite
different from Haskell and OCaml. Esoteric ones exist like Joy and
others that might be fun to check out. Last but not least, Scheme is
great for playing around with different combinations of things though
it doesn't always end up as clean as the above.

[2] There probably is a reason most FP languages feature some form of
currying ^^.

···

On 5/18/06, Madan Manoharan <madan.manoharan@gmail.com> wrote:

On 5/18/06, Joe Bowers <JoeBowers@bclip.com> wrote:
>
> Partials are great for deferring part of a computation
> until later- they're useful in some of the same circumstances
> as closures. They're also really easy to use in ruby already.
>
> def multiply(x, y); x * y; end
>
> multiply_by_3 = lambda { |x| multiply(3, x) }
>
Yesterday* Brian Mitchell *introduced me to the work of *MenTaLguY* where he
talks about 'Promise' and 'Future'; Future sounds very similar to partial
functions.

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6956dd6e4a00258