# Re: Partial functions?

**URL:** <https://www.ruby-forum.com/t/re-partial-functions/94976>\
**Category:** Ruby\
**Created:** [May 18, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/re-partial-functions/94976 "2006-05-18T17:09:53Z")\
**Posts on this page:** 4\
**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, 5:09pm UTC](https://www.ruby-forum.com/t/re-partial-functions/94976/1 "2006-05-18T17:09:53Z")

</div>

> > partial functions would be. Can someone provide a use case for me?
> 
> # Convert a 2 arg operator (\*)
> 
> created function a name:
> 
> a\_times\_3 = a.collect { |n| n \* 3 }
> 
> – Jim W.

Ah, ok. Is there any kind of speed advantage to this approach? Or is  
it more of a design/DRY technique?

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, 5:36pm UTC](https://www.ruby-forum.com/t/re-partial-functions/94976/2 "2006-05-18T17:36:52Z")

</div>

On 5/18/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > I’m trying to understand a new feature of Python 2.5 (PEP
> > 
> > 1. called
> > 
> > > “partial functions”. Here’s the Python example I pulled  
> > > from Vincent  
> > > Foley’s blog:
> > 
> > [… curry example elided …]

I think partial functions is unapropriate name. The first thing I  
thought of when I read the subject of this email was the mathematical  
concept of it[0].

> > 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 }
> 
> Ah, ok. Is there any kind of speed advantage to this approach? Or is  
> it more of a design/DRY technique?

I think giving it a name allows you to reuse it multiple times without  
rewriting it. Everytime you introduce a new name function you have a  
new abstraction. You could later redefine it (e.g. for speed/space  
optimization) and that change would propagate to all its uses.

Having high order functions is a nice thing to have because you can  
use them to build complex functions out of simpler ones. I asked a  
similar question sometime ago[1].

Cheers,  
Ed

[0] [Partial Function -- from Wolfram MathWorld](http://mathworld.wolfram.com/PartialFunction.html)  
[1] [http://tinyurl.com/rtuuz](http://tinyurl.com/rtuuz)

---

<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 19, 2006, 4:00am UTC](https://www.ruby-forum.com/t/re-partial-functions/94976/3 "2006-05-19T04:00:18Z")

</div>

On 5/18/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ah, ok. Is there any kind of speed advantage to this approach? Or is  
> it more of a design/DRY technique?

In functional languages, the compiler can reduce all the computation  
involving the curried args, storing the computation.  
This can make things faster.

f x y = (do\_something\_expensive\_with x) + y  
f\_15 = f 15 # takes a while because reduces  
# (do\_something\_expensive\_with 15) to a value  
(1…10000).map(f\_15) # executes very fast

In ruby, it makes things slower 🙂  
(Two calls instead of one, no caching of results.)

---

<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 19, 2006, 5:32am UTC](https://www.ruby-forum.com/t/re-partial-functions/94976/4 "2006-05-19T05:32:03Z")

</div>

On 5/18/06, Edgardo H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think partial functions is unapropriate name. The first thing I  
> thought of when I read the subject of this email was the mathematical  
> concept of it[0].

Exactly. Partial functions are another term for what some references  
call ‘recursively enumerable’ functions, meaning a function that can  
be computed by some Turing machine. I believe the appropriate term is  
‘Curried functions’, from the logician who first proposed them,  
someone called Haskell Curry (yes, _that_ Haskell).

I’ve been programming in Ocaml too, and there it seems that curried  
functions are extremely useful. In Ocaml most often I’ve used it to  
call a function with several arguments multiple times with only a few  
arguments changing between calls, or for passing a function with say 4  
arguments to an HOF that expects a function with only one, I create a  
curried function that keeps 3 of the 4 arguments fixed and pass that.  
Since it is indeed possible to program in the functional style in  
Ruby, I could imagine that the technique would be just as usable.
