# Partial functions?

**URL:** <https://www.ruby-forum.com/t/partial-functions/60918>\
**Category:** Ruby\
**Created:** [May 18, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/partial-functions/60918 "2006-05-18T16:30:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/partial-functions/60918/1 "2006-05-18T16:30:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/partial-functions/60918/2 "2006-05-18T16:46:17Z")

</div>

the partial function binds one of the arguments of that function, so  
that  
later on only the remaining arguments have to be specified. I think  
it’s  
also called currying.

I guess it could be done with a mix of wrapper functions and  
metaprogramming.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/partial-functions/60918/3 "2006-05-18T16:49:33Z")

</div>

On Thursday 18 May 2006 10:27 am, Berger, Daniel wrote:

> partial functions would be. Can someone provide a use case for me?  
> Hey,

I believe this is just a use of currying, a way of reducing the number  
of arguments to a function by ferreting some of them away (the boring  
ones).

I’ll give you a simple example (from last week):

# Warning, Scheme!

# … bind zip here …

(binary-search index (lambda (s) (string-compare zip (car s))))

# …

BINARY-SEARCH expects two arguments, an index and a function that takes  
_one_ argument. Well, I’ve got to compare two arguments. Thankfully,  
I’m always comparing against the same zip, so I may as well “curry” it  
(and store it in the function), and create a new one-argument function  
on the fly. I’ll pass that new function (from the LAMBDA) to  
BINARY-SEARCH and it’ll never know the difference.

More on currying here (and it’s even not Scheme):  
[http://moonbase.rydia.net/mental/blog/programming/currying-in-ruby.html](http://moonbase.rydia.net/mental/blog/programming/currying-in-ruby.html)

> **[Currying](https://en.wikipedia.org/wiki/Currying)**
>
> In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function 
>   
> f
> f
>   
> that takes three arguments creates a nested unary function 
>   
> g
> g
>   
> , so that the code
> gives 
>   
> x
> x
>   
> the same value as the code
> or called in sequence,
> In a more mathematical language, a function that takes two argume...

The Ruby library Murray was made for currying:  
[http://rubymurray.rubyforge.org/](http://rubymurray.rubyforge.org/)

HTH,  
Keith

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/partial-functions/60918/4 "2006-05-18T16:56:01Z")

</div>

This feature is very similar to currying:  
[Currying - Wikipedia](http://en.wikipedia.org/wiki/Currying) which is common in many functional  
languages (ex, ML automatically curries for you).

I can’t really think of a good usecase off the top of my head, but  
searching  
around for currying examples may help.

Maurice

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/partial-functions/60918/5 "2006-05-18T16:58:46Z")

</div>

Berger, Daniel wrote:

> 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:

[… curry example elided …]

> 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?

Suppose you have a collection of integers, say a = [1,2,3]. And you  
wanted to create a new array consisting of all the integers in a, but  
multiplied by 3. We have an array method (collect) that constructs a  
new array by calling a function of one argument on each element of the  
array. Now we just need to create a function of one argument to pass to  
collect:

# Convert a 2 arg operator (\*)

# into a 1 arg function (ie. a lambda).

multiply\_by\_3 = lambda { |n| n \* 3 }

a = [1,2,3]  
a\_times\_3 = a.collect(&multiply\_by\_3)

> Also, can/should we do this in Ruby?

We do it all the time. We just don’t generally bother to give the newly  
created function a name:

a\_times\_3 = a.collect { |n| n \* 3 }

– Jim W.
