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
1
vote
1 answer

Are the concepts of partial application and currying interchangeable in Haskell?

In some functional languages, partially applied functions and curried functions are two similar but distinct concepts. Reading the book Learn You a Haskell for Great Good, the author appears to apply these interchangeably. [Curried functions]…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1
vote
1 answer

Lodash partialRight padStart map Gives Bad Result

Running into a weird situation with lodash (v4.6.1) where I'm passing in a _.partialRight function of _.padStart into a _.map callback and getting bum results. Repro: _.padStart("0", 2, "0"); // "00" var f = _.partialRight(_.padStart, 2,…
nwayve
  • 2,291
  • 23
  • 33
1
vote
0 answers

scala: pick right override method for the partially applied function

Seq(1, 2, 3).reduce(math.max) // ok: gives 3 Seq[Long](1, 2, 3).reduce(math.max) // error: type mismatched ^^^^^^^^ Why it cannot deduce the Long version of math.max? Seq[Long](1, 2, 3).reduce(math.max(_:Long, _:Long)) //…
cinsk
  • 1,576
  • 2
  • 12
  • 14
1
vote
1 answer

Formatting 2D array with older version of NumPy

I have a code snippet to format a 2D array to a specific string format. The code snippet works well with recent versions of NumPy (e.g. 1.9.2), but the same fails with NumPy 1.4.1, which is the current version for CentOS 6.6. import numpy as np cfmt…
Mike T
  • 41,085
  • 18
  • 152
  • 203
1
vote
3 answers

Can somebody help me write and understand the partial application pattern?

I'm trying to write a function utilizing callback that will mimic this behavior: var fullName = function (firstName, lastName) { return firstName + ' ' + lastName; }; var michaelName = partial(fullName, 'Michael'); michaelName('Moore'); //…
1
vote
1 answer

Using bind for partial application without affecting the receiver

If I want to partially apply a function I can use bind, but it seems I have to affect the receiver of the function (the first argument to bind). Is this correct? I want to perform partial application using bind without affecting the receiver. …
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
1
vote
2 answers

What are the rules in JS on a variable referencing the same data across a loop?

I just needed to use partial application to pass data into a callback, and I found I needed a layer of indirection to prevent subsequent runs of a loop from changing the data I'm passing to the callback. See here for a very simple working example:…
Finn
  • 45
  • 4
1
vote
2 answers

Partial application and sortBy

I'm learning Haskell and I've been experimenting with partial application. I tried to pertially apply sortBy. I don't undestand the type of the resulting function. And how should it be done properly? let mf = sortBy compare :t mf mf :: [()] -> [()]
jira
  • 3,890
  • 3
  • 22
  • 32
1
vote
2 answers

Miranda Type error

Can anyone tell me where goes wrong? b f x = f x (f x) My understanding is: because f at the left takes one argument but on the right side it has two arguments? Any more detail explanations?
geasssos
  • 419
  • 1
  • 5
  • 10
1
vote
3 answers

How does Haskell evaluate this function defined with partial application?

I'm trying to understand how Haskell evalutes pp1 [1,2,3,4] to get [(1,2),(2,3),(3,4)] here: 1. xnull f [] = [] 2. xnull f xs = f xs 3. (/:/) f g x = (f x) (g x) 4. pp1 = zip /:/ xnull tail I start like this: a) pp1 [1,2,3,4] = (zip /:/ xnull…
Fof
  • 407
  • 2
  • 8
1
vote
2 answers

C/C++: A (*eval(A (*function)(B),B b))(){ ... } possible?? (possibly pre-C++11)

in C/C++ (possibly pre-C++11), is it possible to do A (*eval(A (*function)(B), B b))(){ // ... ?? } i.e., a function taking a function returning an A value from a B value, a B value to be fed to that function, which returns - a function…
1
vote
2 answers

Currying Javascript function with custom order of fixed parameters

Currying functions can be usefull: function tag(name, value) { return '<' + name + '>' + value + ''; } var strong = tag.bind(undefined, "strong"); strong("text"); // text Now imagine we have to use another…
opengrid
  • 1,942
  • 4
  • 16
  • 25
1
vote
2 answers

Partially applied functions in Scala

Wondering if you can comment on why following two scenarios behave differently: The following works: var la= List(12, 13 , 14 ,15); var func = (x:Int) => println(x) la.foreach(func) // 1 la.foreach(func(_)) // 2 But…
Shalab
  • 651
  • 5
  • 6
1
vote
1 answer

Is there an explicit type constructor for (->) in Coq?

I'm trying to define a class that provides identity and composition. Besides other useful instances (List with nil and concatenation; Relations with, well, identity and composition ;-) ), I'd like to have an instance for functions. Given Class Cat…
nobody
  • 4,074
  • 1
  • 23
  • 33
0
votes
1 answer

F# Is it possible to get arguments of partially applied function?

Having a function taking two arguments: let yolo x y = x + y Is it possible to get information (or preferably, value) of one of the applied arguments after an application? Below is a pseudo-code that summarizes what I want to achieve. let yolo_x…
zajer
  • 649
  • 6
  • 17