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
4
votes
2 answers

Performant way to partially apply in Python?

I am looking for a way to partially apply functions in python that is simple to understand, readable, resusable and as little error prone to coder mistakes as possible. Most of all I want the style to be as performant as possible - less frames on…
LudvigH
  • 3,662
  • 5
  • 31
  • 49
4
votes
1 answer

Haskell partial function application with $

I'm new to Haskell and looking at a simple example of using function application with $. It seems straightforward - it takes a function and applies it to a value. So this makes sense: > (+3) $ 2 5 This also makes sense: > ($) (+3) 2 5 This makes…
4
votes
2 answers

Partial functions with bind

So recently I discovered that you can do partial functions/currying with js using bind. For example: const foo = (a, b, c) => (a + (b / c)) foo.bind(null, 1, 2) //gives me (c) => (1 + (2 / c)) However this only works if the parts you want to curry…
Vangogh500
  • 939
  • 1
  • 7
  • 17
4
votes
0 answers

What are the advantages of std::bind allowing and discarding extra arguments?

Consider this code, #include #include void pacifist() { std::cout << "I don't get involved in arguments. I'm a pacifist.\n"; } int main() { auto x = std::bind(pacifist); x(); //OK: Makes perfect…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4
votes
2 answers

Partial application of data constructor

I don't understand why the following exercise "works" in Haskell Programming from First Principles: type Subject = String type Verb = String type Object = String data Sentence = Sentence Subject Verb Object deriving (Eq, Show) s1 =…
gogurt
  • 811
  • 1
  • 8
  • 24
4
votes
2 answers

How to use map with a function that needs more arguments

I am trying to use map with (string-split "a,b,c" ",") to split strings in a list. (string-split "a,b,c" ",") '("a" "b" "c") Following works if string-split is used without ",": (define sl (list "a b c" "d e f" "x y z")) (map string-split…
rnso
  • 23,686
  • 25
  • 112
  • 234
4
votes
1 answer

Why does the partial application `foldr id` typecheck?

In Haskell, I don’t understand why the partial application foldr id typechecks. Relevant types are > :t foldr id foldr id :: a -> [a -> a] -> a > :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b > :t id id :: a -> a The first argument of foldr is…
Kin Pu
  • 400
  • 1
  • 4
  • 13
4
votes
1 answer

Operations on n-th argument of curried functions in scala

I'm working with a lot of curried functions, taking similar arguments, but not quite. For this reason I would find very beneficial to have a way to perform transposition, application and composition of n-th argument, as well as the 'final' result.…
Turin
  • 2,208
  • 15
  • 23
4
votes
1 answer

Haskell, is it possible to create a curry function that can curry any number of tuple elements

The current curry function takes a function accepting a tuple of 2 elements and allows the resulting function to be curried or partially applied. let x = curry (\(x, y) -> x + y) x 1 2 -- 3 Is it possible to create a curry function that can deal…
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
4
votes
2 answers

Why can you define function without parameter in haskell

I have function add which I apply partially to create a new function addOne. add :: Int -> (Int -> Int) add x y = x + y addOne can be defined with explicit parameter addOne :: Int -> Int addOne y = add 1 y or without explict parameter addOne ::…
Rene
  • 289
  • 1
  • 4
  • 8
4
votes
1 answer

Dynamic generation of partial functions in Python 2.7.x

Say I want to dynamically create a function on an IPython shell from the following lambda: f = lambda x, ci: np.percentile(x, 100-ci) that fixes ci to a new value. It would be something like the following (create_new_f is what I am looking for). ci…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
4
votes
2 answers

Is there a way I can write this Haskell code in Scala?

I'm going through a few functional programming languages, learning things of interest, and I'm looking at Scala now. What I'm trying to do is figure out the simplest way to write a function called double which take one argument and doubles it. What…
user377628
3
votes
3 answers

Producing a partially applied function from method of type in an Option

Suppose I'm writing a GUI class Kitteh (val age: Int) { require (age < 5) def saveMeow(file: File) = { /* implementation */ } def savePurr(file: File) = { /* implementation */ } } The frame has a field for the current Kitteh, which is an…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
3
votes
1 answer

Why does ($ 3) have signuature (a -> b) -> b?

In Learn you a Haskell, it is given the following example: map ($ 3) [(4+), (10*), (^2), sqrt] [7.0,30.0,9.0,1.7320508075688772] However, I don't understand why this works. The signatures of the functions are Prelude> :info ($) ($) :: (a -> b) ->…
Our
  • 986
  • 12
  • 22
3
votes
2 answers

About mapping on the eventual result of a multi-argument function

I known that r -> a is a Functor in a, and that fmap = (.) for it. This means that when I do fmap f g, with g :: r -> a, f is applied to the result of g as soon as the latter is fed with a value of type r. However, if a is a function type, e.g. a…
Enlico
  • 23,259
  • 6
  • 48
  • 102