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

When do I have to treat my methods as partially applied functions in Scala?

I noticed that when I'm working with functions that expect other functions as parameters, I can sometimes do this: someFunction(firstParam,anotherFunction) But other times, the compiler is giving me an error, telling me that I should write a…
Senthess
  • 17,020
  • 5
  • 23
  • 28
22
votes
4 answers

Is performance of partial or curried functions well defined in Haskell?

In the following code: ismaxl :: (Ord a) => [a] -> a -> Bool ismaxl l x = x == maxel where maxel = maximum l main = do let mylist = [1, 2, 3, 5] let ismax = ismaxl mylist --Is each call O(1)? Does each call remember maxel? let…
18
votes
3 answers

Does Kotlin support partial application?

Since Kotlin supports many concepts from functional programming, I was wondering if there is a way to do partial application of a function in Kotlin as well? One such example of where partial application can be useful is: // In one class fun…
idunnololz
  • 8,058
  • 5
  • 30
  • 46
18
votes
4 answers

Is there a reason why `this` is nullified in Crockford's `curry` method?

In Douglas Crockford's book "Javascript: The Good Parts" he provides code for a curry method which takes a function and arguments and returns that function with the arguments already added (apparently, this is not really what "curry" means, but is…
RustyToms
  • 7,600
  • 1
  • 27
  • 36
17
votes
2 answers

In Python, partial function application (currying) versus explicit function definition

In Python, is it considered better style to: explicitly define useful functions in terms of more general, possibly internal use, functions; or, use partial function application to explicitly describe function currying? I will explain my question…
16
votes
2 answers

Partially applying a function that has an implicit parameter

Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can…
0__
  • 66,707
  • 21
  • 171
  • 266
14
votes
3 answers

When (if ever) can type synonyms be partially applied?

Apparently a bit absent-mindedly, I wrote something like the following: {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE TypeFamilies #-} class Foo f where type Bar f :: * retbar :: Bar f -> IO f type Baz f = (Foo f, Eq f) --…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
14
votes
2 answers

Why is a built-in function applied to too few arguments considered to be in weak head normal form?

The Haskell definition says: An expression is in weak head normal form (WHNF), if it is either: a constructor (eventually applied to arguments) like True, Just (square 42) or (:) 1 a built-in function applied to too few arguments (perhaps none)…
13
votes
4 answers

Partial application left to right

I started with haskell yesterday and am still completely lost on the shore of this brave new world. Now I have run into the following issue: Let's assume I have some function that does some magic to an integer and another variable: makeTuple :: Int…
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
13
votes
2 answers

JavaScript curry function

I have implemented a curry function this way: function curry (fn) { var slice = Array.prototype.slice, args = slice.apply(arguments, [1]); return function () { fn.apply(null, args.concat(slice.apply(arguments))); …
dheerosaur
  • 14,736
  • 6
  • 30
  • 31
13
votes
3 answers

Confusion about Function.prototype.bind()

I'm a huge fan of ES5's Function.prototype.bind and currying arguments (basically creating default arguments for functions). I was fooling around with that a bit, but I can't for the life of me figure out my own construct anymore. This is my…
jAndy
  • 231,737
  • 57
  • 305
  • 359
13
votes
5 answers

In Haskell, (+) is a function, ((+) 2) is a function, ((+) 2 3) is 5. What exactly is going on there?

How is this possible, what is going on there? Is there a name for this? What other languages have this same behavior? Any without the strong typing system?
13
votes
3 answers

Partial Application with Infix Functions

While I understand a little about currying in the mathematical sense, partially applying an infix function was a new concept which I discovered after diving into the book Learn You a Haskell for Great Good. Given this function: applyTwice :: (a…
12
votes
1 answer

Why and when do I need to follow a method name with _?

I'm a bit shaky on the rules as to when you need a _ after a method to use it as a function. For example, why is there a difference between Foo's and Nil's :: in the following? def square(n: Int) = n * n object Foo { def ::(f: Int => Int) = f(42)…
Matt R
  • 9,892
  • 10
  • 50
  • 83
12
votes
4 answers

Haskell dollar operator application

I'm having trouble with understanding how function application works with currying in haskell. If I have following function: ($) :: (a -> b) -> a -> b I understand that to partially apply this function I need to provide (a -> b) function ($'s first…
1
2
3
16 17