Questions tagged [partial-application]

Partial application is a programming technique for passing less than the full number of arguments to a function, in order to yield a new function that can be used later. It is particularly common in functional languages that support currying.

Partial application is a programming technique for passing less than the full number of arguments to a function, in order to yield a new function that can be used later. It is particularly common in languages that support .

Example (OCaml)

(* `add` is a function with arity 2 *)
let add a b = a + b

(* `add` is partially applied with the arguemnt `2`,
 * yielding a function of arity 1 *)
let add2 = add 2

(* `4` is applied to `add2`, making it fully applied
 * and yielding the result of evaluating `add`: `6` *)
let x = add2 4

Example (Python)

Consider the following function:

def add_together(a, b):
    return a + b

If we want to hold a constant, we can manually create the same function with a constant, for example, 5:

def add_together(b):
    return 5 + b

In programming, we typically want a programmatic way of setting our constant and generating our partial function. In Python, we can do this with a closure:

def set_a_on_add_together(a): # the outer function takes a parameter, a
    def add_a_to(b):          # outer function def's new function w/ parameter, 5
        return a + b          # the new function returns a, held constant, plus b
    return add_a_to           # outer function returns the newly created function

and would be used like this:

add_to_five = set_a_on_add_together(5)
add_to_five(4)

would return 9, and

add_to_five(10)

would return 15.

253 questions
494
votes
16 answers

What is the difference between currying and partial application?

I quite often see on the Internet various complaints that other peoples examples of currying are not currying, but are actually just partial application. I've not found a decent explanation of what partial application is, or how it differs from…
328
votes
8 answers

How does functools partial do what it does?

I am not able to get my head on how the partial works in functools. I have the following code from here: >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) 3 >>> def sum2(x, y): return x + y >>> incr2 =…
237
votes
7 answers

Python: Why is functools.partial necessary?

Partial application is cool. What functionality does functools.partial offer that you can't get through lambdas? >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) 3 >>> def sum2(x, y): return x + y >>>…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
187
votes
17 answers

JavaScript curry: what are the practical applications?

I don’t think I’ve grokked currying yet. I understand what it does, and how to do it. I just can’t think of a situation I would use it. Where are you using currying in JavaScript (or where are the main libraries using it)? DOM manipulation or…
Dave Nolan
  • 3,009
  • 4
  • 30
  • 32
95
votes
3 answers

Ordering of parameters to make use of currying

I have twice recently refactored code in order to change the order of parameters because there was too much code where hacks like flip or \x -> foo bar x 42 were happening. When designing a function signature what principles will help me to make the…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
94
votes
17 answers

Does Java support Currying?

I was wondering if there is any way to pull that in Java. I think it is not possible without native support for closures.
user855
  • 19,048
  • 38
  • 98
  • 162
86
votes
4 answers

What's the difference between multiple parameters lists and multiple parameters per list in Scala?

In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a…
den bardadym
  • 2,747
  • 3
  • 25
  • 27
81
votes
5 answers

Why does Scala provide both multiple parameters lists and multiple parameters per list?

Multiple parameters lists, e.g. def foo(a:Int)(b:Int) = {} and multiple parameters per list, e.g. def foo(a:Int, b:Int) = {} are semantically equivalent so far as I can tell, and most functional languages have only one way of declaring multiple…
Yuvi Masory
  • 2,644
  • 2
  • 27
  • 36
81
votes
7 answers

How can I bind arguments to a function in Python?

How can I bind arguments to a Python function so that I can call it later without arguments (or with fewer additional arguments)? For example: def add(x, y): return x + y add_5 = magic_function(add, 5) assert add_5(3) == 8 What is the…
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
72
votes
3 answers

What exactly is meant by "partial function" in functional programming?

According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python: >>> def add(x,y): ... return x+y ... >>> new_function =…
69
votes
13 answers

Can one partially apply the second argument of a function that takes no keyword arguments?

Take for example the python built in pow() function. xs = [1,2,3,4,5,6,7,8] from functools import partial list(map(partial(pow,2),xs)) >>> [2, 4, 8, 16, 32, 128, 256] but how would I raise the xs to the power of 2? to get [1, 4, 9, 16, 25, 49,…
beoliver
  • 5,579
  • 5
  • 36
  • 72
48
votes
11 answers

Using Function.prototype.bind with an array of arguments?

How can I call Function.prototype.bind with an array of arguments, as opposed to hardcoded arguments? (Not using ECMA6, so no spread operator). I'm trying to put a promises wrapper around a module that uses callbacks and I want to bind all of the…
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
41
votes
3 answers

functools.partial wants to use a positional argument as a keyword argument

So I am trying to understand partial: import functools def f(x,y) : print x+y g0 = functools.partial( f, 3 ) g0(1) 4 # Works as expected In: g1 = functools.partial( f, y=3 ) g1(1) 4 # Works as expected In: g2 = functools.partial( f, x=3…
usual me
  • 8,338
  • 10
  • 52
  • 95
29
votes
4 answers

How to efficiently partially apply a function in R?

Suppose I have a function in R that takes multiple arguments, and I'd like to reduce it to a function of fewer arguments by setting some of the arguments to pre-specified values. I'm trying to figure out what is the best way to do this is in R. For…
k13
  • 713
  • 8
  • 17
28
votes
3 answers

How can go-lang curry?

In functional programming likes Haskell, I can define function add a b = a+b Then add 3 will return a function that take one parameter and will return 3 + something How can I do this in GO? When I define a function that take more than one (say n)…
lazywei
  • 11,955
  • 5
  • 20
  • 25
1
2 3
16 17