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

What is the best pattern to curry delegate parameters (using .NET 2.0 or later)?

Sometimes it is useful to take a method call, complete with parameters, and turn it into a MethodInvoker which will invoke the indicated function with those parameters, without having to specify the parameters at the time. At other times, it's…
supercat
  • 77,689
  • 9
  • 166
  • 211
11
votes
3 answers

F# passing an operator with arguments to a function

Can you pass in an operation like "divide by 2" or "subtract 1" using just a partially applied operator, where "add 1" looks like this: List.map ((+) 1) [1..5];; //equals [2..6] // instead of having to write: List.map (fun x-> x+1) [1..5] What's…
dan
  • 9,712
  • 6
  • 49
  • 62
11
votes
1 answer

Replace parameter in lambda expression

Considering this code: public class Foo { public int a { get; set; } public int b { get; set; } } private void Test() { List foos = new List(); foos.Add(new Foo()); foos.Add(new Foo()); Expression>…
Juan
  • 15,274
  • 23
  • 105
  • 187
10
votes
1 answer

How do I partially apply an infix function like Basics.+?

All of the examples I've seen so far create a "wrapper" function around Basics.+ and then partially apply that: sum x y = x + y plusOne = sum 1 However, I'm sure that there's a way to avoid the extra wrapping.
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
10
votes
4 answers

How to correctly curry a function in JavaScript?

I wrote a simple curry function in JavaScript which works correctly for most cases: const curry = (f, ...a) => a.length < f.length ? (...b) => curry(f, ...a, ...b) : f(...a); const add = curry((a, b, c) => a + b + c); const add2 =…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
10
votes
4 answers

Concise syntax for partial in Clojure

Learning Haskell some time ago, I felt in love with pointfree notation and especially convenient partial function application - just supply args you know. In Clojure, I have partial all the time. I think having a special syntax for partial in reader…
demi
  • 5,384
  • 6
  • 37
  • 57
10
votes
3 answers

Getting partial constructors for case classes "for free"

Consider an abstract class defining two properties abstract class A { def a: Int def b: Int // real A has additional members } which is the base class for various case classes such as case class Foo(a: Int, b: Int) extends A case class Bar(a:…
Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
9
votes
3 answers

Why scala can't infer the type in a partial method?

See this example: def hello(a:String, b:String) = println(a + ":" + b) val m1 = hello("aaa", _ ) m1("bbb") It can't be compiled, that I need to add the type to the partial method: val m1 = hello("aaa", _: String) Why scala doesn't know the 2nd…
Freewind
  • 193,756
  • 157
  • 432
  • 708
9
votes
1 answer

Static method signature type arguments and partial application

I've been looking into Functional Programming lately and wanting to bring some concepts to my C# world. I'm trying to compose functions to create services (or whatever you'd call them) instead of creating classes with injectable dependencies. I've…
Aage
  • 5,932
  • 2
  • 32
  • 57
9
votes
3 answers

Function application in Haskell

OK, it's been a long day and my brain may not function at Haskell level, but I just cannot understand one example from 'Learn You a Haskell'. The section is called Function Application with $, and there is example of how $ may be defined: ($) :: (a…
9
votes
1 answer

What is a list of curried programming languages?

I just learned from another question that Haskell is called a curried programming language because it applies function currying by default. What are other languages that display this behavior?
8
votes
3 answers

How is a partial application represented at runtime?

When I write something like map (1+) list in Haskell, what is the internal representation of (1+)? Since it is a partial application of (+), the argument 1 has to be saved somewhere, but I can't get my head around this. Can somebody give me a brief…
fuz
  • 88,405
  • 25
  • 200
  • 352
8
votes
1 answer

What are the performance characteristics between curried, partially applied, and 'normal' functions in Scala?

I took a look here: Scala currying vs partially applied functions, but the answers there answer more about the functional and semantic differences between currying, partial application, and normal functions in Scala. I'm interested in learning…
josiah
  • 1,314
  • 1
  • 13
  • 33
8
votes
2 answers

When is a scala partial function not a partial function?

While creating a map of String to partial functions I ran into unexpected behavior. When I create a partial function as a map element it works fine. When I allocate to a val it invokes instead. Trying to invoke the check generates an error. Is…
8
votes
2 answers

What are the rules to govern underscore to define anonymous function?

I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach…
1 2
3
16 17