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

Clojure Partial Application - How to get 'map' to return a collection of functions?

I have a function that I basically yanked from a discussion in the Clojure google group, that takes a collection and a list of functions of arbitrary length, and filters it to return a new collection containing all elements of the original list for…
6
votes
3 answers

Why can't scala infer the type of the omitted parameters in partial application?

consider this : scala> def sum(x:Int,y:Int) = x+y sum: (x: Int, y: Int)Int scala> sum(1,_:String) :9: error: type mismatch; found : String required: Int sum(1,_:String) Apparently Scala is very well aware of the exact…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
5
votes
2 answers

Syntax for partial application of curried functions with reverse-associative infix notation

In other words, is there a good reason why this shouldn't compile? def f(xs: List[Int]) = xs.foldLeft(0) _ // OK def f(xs: List[Int]) = (xs :\ 0) _ // OK def f(xs: List[Int]) = (0 /: xs) _ :15: error: missing arguments for method /:…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
5
votes
5 answers

anyone know how to use a partially applied three argument function infix (haskell)

I want to apply a 3 argument function in different ways based on a boolean value (one of the arguments). I'd like to be able to apply it in an infix manner so I can chain it (example below). something like the following but that actually works. f ::…
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
5
votes
2 answers

Haskell "Non type-variable argument in the constraint"

I've created a list of partially applied functions in my REPL like so: listOfPartiallyAppliedFunctions = map (*) [1..100] I would then like to create the list of results from completing the function application, which I can easily do by providing a…
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
5
votes
4 answers

F# Partially apply the second argument

in F# if I take a function that takes two arguments, for example, mod (%): 13 % 10 // val it : int = 3 which is the same as (%) 13 10 // val it : int = 3 is there any way to write it in the pipe notation, currying the 13? Obviously, the…
Ilya Kharlamov
  • 3,698
  • 1
  • 31
  • 33
5
votes
1 answer

Partial function application in Kotlin

I am having trouble with syntax for partial function application. The following code works fine, and it outputs: two-three-four import kotlin.coroutines.experimental.* inline fun Iterable.forEachFrom(beg:Int, act:(T)->Unit) { var i=0; if…
sirksel
  • 747
  • 6
  • 19
5
votes
3 answers

Haskell - Currying? Need further explanation

So something like addList :: [int] -> int addList = foldl1 (+) Why does this work? The Currying part. Why no variable?
Matt
  • 7,049
  • 7
  • 50
  • 77
5
votes
6 answers

Partial application of functions and currying, how to make a better code instead of a lot of maps?

I am a beginner at Haskell and I am trying to grasp it. I am having the following problem: I have a function that gets 5 parameters, lets say f x y w z a = x - y - w - z - a And I would like to apply it while only changing the variable x from 1 to…
5
votes
1 answer

Is there a difference between partial application and returning a function?

In terms of under the hood: stack/heap allocation, garbage collection, resources and performance, what is the difference between the following three: def Do1(a:String) = { (b:String) => { println(a,b) }} def Do2(a:String)(b:String) = { println(a,b)…
Alex
  • 11,479
  • 6
  • 28
  • 50
5
votes
1 answer

What is a useful example of partial function application in Swift?

I see that Swift offers convenient syntax for declaring curried functions. The manual gives partial function application as an example of where curried function will come in handy. Can someone give me an example where partial function application…
Mihai Damian
  • 11,193
  • 11
  • 59
  • 81
5
votes
1 answer

Caching internals with a partially applied function

Imagine such a function: bar :: Foo -> A -> B -> C -> IO () That function performs some IO stuff using a Foo and other values. The Foo value has to be passed to bar, and can be retrieved from IO via this: foo :: X -> IO Foo Now, A, B, C and X are…
phaazon
  • 1,972
  • 15
  • 21
5
votes
3 answers

Scala partial application unclear

I don't have very clear the partial application of functions in Scala... I will do an example: def myOperation(x: Int)(y: Int): Int = { val complexVal = complexCalc(x) println("complexVal calculated") complexVal + y } def…
rascio
  • 8,968
  • 19
  • 68
  • 108
5
votes
3 answers

Explanation of partial application - join

Why does partial application of functions with different signatures work? Take Control.Monad.join as an example: GHCi> :t (=<<) (=<<) :: Monad m => (a -> m b) -> m a -> m b GHCi> :t id id :: a -> a GHCi> :t (=<<) id (=<<) id :: Monad m => m (m b) ->…
David Unric
  • 7,421
  • 1
  • 37
  • 65
4
votes
1 answer

How does compose function work with multiple parameters?

Here's a 'compose' function which I need to improve: const compose = (fns) => (...args) => fns.reduceRight((args, fn) => [fn(...args)], args)[0]; Here's a practical implementation of one: const compose = (fns) => (...args) =>…