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
3
votes
3 answers

JavaScript function currying does not work on instance method

I am learning function currying in JavaScript by reading online and writing some simple code. I got following example in online article function toArray(obj) { return Array.prototype.slice.call(obj); } Function.prototype.curry =…
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
3
votes
4 answers

In Haskell, what does the map function mean when you only pass it a list?

In a Haskell project I'm given to debug, there are instances in the code where map is used with only one parameter - a list - is passed. For example printReports :: [Report] -> IO () printReports = putStrLn . unlines . map show and printRuns' ::…
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
3
votes
3 answers

Is partial macro application / currying possible in the C preprocessor?

As an example of the problem, is there any way to implement the macro partialconcat in the following code? #define apply(f, x) f(x) apply(partialconcat(he),llo) //should produce hello EDIT: Here's another example, given a FOR_EACH variadic macro…
Dylan
  • 1,692
  • 1
  • 13
  • 24
3
votes
2 answers

Partial Application - Eloquent Javascript

I am reading Eloquent Javascript and am having a difficult time understand the example below. Would anyone be able to do a line by line type explanation? Specifically, I'm confused as to why the first loop is starting at one, and why the push method…
KMcA
  • 1,155
  • 1
  • 11
  • 21
3
votes
1 answer

boost::bind member function - partial application chaining

I'm trying to chain together curried functions using boost::bind, and getting compiler errors that I can't resolve. The simplest example I can make which fails to compile: #include #include class A { public: template…
NNN
  • 386
  • 1
  • 9
3
votes
3 answers

laziness in action? (Haskell)

In chapter 6 of Learn You a Haskell, the following function is introduced: zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith' _ [] _ = [] zipWith' _ _ [] = [] zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys The author gives a couple…
2
votes
1 answer

Make partially apply function from java function

I have java function like this public static CollectionReader createCollectionReader( Class readerClass, TypeSystemDescription typeSystem, Object... configurationData) I'd like to make a partially apply…
Tg.
  • 5,608
  • 7
  • 39
  • 52
2
votes
2 answers

Reversing partial function parameters sequence

I want to write List.map (fun x -> x % 3) into a form like List.map ((%) 3). The issue with the latter is that it translates to List.map (fun x -> 3 % x) Not what I want. Is it possible to write List.map (fun x -> x % 3). in a more succinct…
SAm
  • 2,154
  • 28
  • 28
2
votes
1 answer

Is there a name for this partial-application--like functional programming technique?

I have a function f: (a, b, c = 5, d = 0) -> {...} that takes between 2 and 4 arguments. I want to pass a "bound" version of this function that always uses the defaults for the last arguments, but uses specific values (say 1 and 2) for the first two…
Domenic
  • 110,262
  • 41
  • 219
  • 271
2
votes
2 answers

How to dynamically add method to class with `functools.partial()`

I am having trouble with the right incantation to get a dynamic method added to a class using functools.partial in the following situation. The following has a Creator class to which I want to add a create_someclass method, which is partially…
HoosierDaddy
  • 720
  • 6
  • 19
2
votes
1 answer

Typo in Learn you a Haskell for Great Good?

In Type Synonyms we read Just like we can partially apply functions to get new functions, we can partially apply type parameters and get new type constructors from them. How can a parameter be applied to something else? I think it should actually…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
1 answer

Is partial application of higher order functions possible in C?

Let's say I have a function int myfun (int arg1, int arg2, int arg3, int arg4) { /* function body */ } and I would like to write a function pass_last_elements() that has signature int (*)(int, int) pass_last_elements(int (*myfun)(int, int, int,…
gosbi
  • 755
  • 1
  • 6
  • 13
2
votes
3 answers

How can I avoid accidental partial application in a compact way?

I have a side-effecting function, f : int -> string -> unit which I am calling using f 1 "hi". To make sure I get an error in the call site if the function is changed to need more arguments, I call the function using () = f 1 "hi" |> ignore. This…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
2
votes
1 answer

Haskell: Question about Partial Application

I am reading the book 'Learn You a Haskell for Great Good!' by Miran Lipovaca and learning about higher-order functions in Chapter 5. One of the examples involves the following function: applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x) The…
2
votes
0 answers

Decorators and partial functions

I'm trying to learn decorators and partial, and I think I make some sense. I understand decorators as a way to add additional functionality to an object by passing in the arguments from the function that calls the decorator into the nested function…
XRaycat
  • 1,051
  • 3
  • 16
  • 28