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
2
votes
1 answer

How to map a function in Common Lisp?

I made this function in Common Lisp (defun f (&key n p x) (* (combinacion n x) (expt p x) (expt (- 1 p) (- n x)))) and it works fine. The thing is that I want to make a function in Common Lisp lake the following Haskell function ff n p x = sum…
2
votes
2 answers

What is the difference between the reader monad and a partial function in Clojure?

Leonardo Borges has put together a fantastic presentation on Monads in Clojure. In it he describes the reader monad in Clojure using the following code: ;; Reader Monad (def reader-m {:return (fn [a] (fn [_] a)) :bind (fn [m k] …
hawkeye
  • 34,745
  • 30
  • 150
  • 304
2
votes
2 answers

What is the difference between multiple argument lists and returning a function?

What is the difference between def f(x: Int)(y: Int) = x + y and def f(x: Int) = (y: Int) => x + y? The REPL doesn’t seem happy when I treat the former the same as the latter: scala> def f(x: Int)(y: Int) = x + y f: (x: Int)(y: Int)Int scala>…
user1804599
2
votes
1 answer

javascript and currying

I am reading through John Resig's Secrets of Javascript ninja and was trying out one of the examples on currying and parital functions. The code is as follows:
2
votes
4 answers

binding a partially applied function in Haskell

I'm a Haskell newbie, so please excuse me if you find this question trivial: How would I get GHCi to accept a declaration of this sort: let foo = fmap (*3) . fmap (+10)? I tried adding a type declaration to foo (let foo :: [Int] -> [Int] = etc) to…
planarian
  • 2,047
  • 18
  • 18
1
vote
2 answers

Is there an equivalent of partial application for return values?

If papply returns a function of less arity than an input function, is there a similar FP operation with returns a function which returns a value regardless of the value of the input function? If so, is there a C# equivalent? Consider a C# function…
1
vote
2 answers

Is it possible to get a (n-1)-argument function out of a n-argument function by setting one argument to a fixed value?

I was wondering if in C++ it was possible to get a function taking (n-1) arguments out of a function taking n arguments by setting the value for the nth argument to some value (to be determined at runtime)? E.g. I would like to do something like the…
matt
  • 13
  • 2
1
vote
1 answer

I need to provide a callback when instantiating a class from an API. How can I bind the instance itself to the callback, eagerly?

I've encountered this problem with a few different major third-party libraries and frameworks now. Let me try to boil it down to the essentials: The API provides a class Example, where the constructor expects a callback parameter. When some event…
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1
vote
1 answer

Terminology: Partial application where the unbound argument is a function?

... partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. I would like to find out if there is a specific name for the following:…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
1 answer

Passing _.groupBy to _.partialRight seems to give incorrect results

Here's a JavaScript object, const obj = {a: [{ id: 1 }, {id: 1}, {id: 2}, {id: 3}], b: [{ id: 4 }, {id: 5}, {id: 5}, {id: 6}] }; and here's a code that correctly groups the items by .id in each of the two arrays ojb.a and obj.b, const res1 =…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
2 answers

F# partial application function wrapper does not keep return type generic for parameter function

TL DR: I am trying to create a function wrapper. The wrapped function takes no parameter but returns a value. The reason I want to do this is to create a function similar to "lock" but for a semaphore. Here it is let functionWrapper (param1)…
Jul
  • 63
  • 5
1
vote
1 answer

Partially applying function arguments in a composition

I'm trying to write a function composition that partially applies an argument at each step and ends up calling a curried two-argument function. There is a set of example functions to compose. I removed the calculations that there are supposed to do…
1
vote
2 answers

Why does `let f : int -> int list -> int list = (::);;` fail?

I would think that OCaml would read the following as an instance of partial application, and store the function as f. However, the compiler complains that the function is applied to too few arguments. Any insight into this would be appreciated,…
Addem
  • 3,635
  • 3
  • 35
  • 58
1
vote
2 answers

changing list of strings to atoms using maplist?

How to change string to atoms using maplist. This does not work : ?- maplist(atom_string,["a","b","c"]). first because atom_string/2 has arity of two (How do you do partial-function//currying in prolog). But even if partial-fun worked the…
sten
  • 7,028
  • 9
  • 41
  • 63
1
vote
1 answer

Using point free style for parts of a function definition

Given this function (btw, should I say it's defined by cases? How do I refer to functions defined like this?), f :: Int -> Int -> Int f 0 x = x f x _ = x I'm wandering what is the reason, if one exists, why I cannot write it like this: f :: Int ->…
Enlico
  • 23,259
  • 6
  • 48
  • 102