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

Partial function application with the original docstring in Python?

For partial function application, I know there are several ways to do that in Python. However, they seems not to preserve the original function's docstring. Take functools.partial as example: from functools import partial def foo(a, b, c=1): …
Drake Guan
  • 14,514
  • 15
  • 67
  • 94
8
votes
1 answer

Is it me, or does John Resig's popular blog post on partial application not work?

John Resig has a popular blog post on partial application: http://ejohn.org/blog/partial-functions-in-javascript/ It's mentioned in many places, and has However, the code in the blog post doesn't work. Here it is: Function.prototype.partial =…
Owen Versteeg
  • 342
  • 2
  • 14
8
votes
4 answers

Create data constructor for partially applied type in Haskell

Is it possible to create a data constructor for partially applied type in Haskell? ghci session: Prelude> data Vector a b = Vector {x::a, y::b} Prelude> :t Vector Vector :: a -> b -> Vector a b Prelude> type T1 = Vector Int Prelude> :t…
7
votes
1 answer

Partial application versus pattern matching: why do these Haskell functions behave differently?

I'm trying to understand something about Haskell functions. First, here is a Fibonacci function defined in the typical "slow" way (i.e. recursive with no memoization, and no infinite-list tricks) slowfib :: Int -> Integer slowfib 0 = 0 slowfib 1 =…
7
votes
1 answer

Partially applied type lambda in Scala with kind projector

Consider the following type definition: trait LiftF[F[_], G[_]] { def liftF[A](fa: F[A]): G[A] } When providing a requirement for an implicit of this type in context bounds (using kind projector plugin) we have to write it like this: def func[A,…
7
votes
1 answer

Java 8 partial function application /currying

public static Function partial(BiFunction f, T x) { return (y) -> f.apply(x, y); } In the expression above, I can understand that function partial returns another function, Function. That Function
microwth
  • 1,016
  • 1
  • 14
  • 27
7
votes
2 answers

Passing parameterized function handle in Python

I have a general function that defines a form of an ODE that I plan to integrate using scipy.integrate.odeint, for example: def my_ode(K, tau, y, u): return K*u/tau - y/tau # dydt I have several objects in my code that all have dynamics of the…
Engineero
  • 12,340
  • 5
  • 53
  • 75
7
votes
2 answers

Haskell recursive function example with foldr

I've taken up learning Haskell again, after a short hiatus and I am currently trying to get a better understanding of how recursion and lambda expressions work in Haskell. In this: YouTube video, there is a function example that puzzles me far more…
user3476725
7
votes
1 answer

Partial function application with generics

I'm working with an Observer API (ObserverSet) which have the following function : public func add(object: T, _ f: T -> Parameters -> Void) -> ObserverSetEntry It simply register an object then call the instance method f…
Yaman
  • 3,949
  • 4
  • 38
  • 60
6
votes
3 answers

Curried function defined in terms of its own partial application

The following SML code is taken from a homework assignment from a course at University of Washington. (Specifically it is part of the code provided so that students could use it to complete Homework 3 listed on the course webpage.) I am not asking…
lamc
  • 347
  • 2
  • 5
6
votes
2 answers

How to partially apply member functions in JavaScript?

I currently have a partial-application function which looks like this: Function.prototype.curry = function() { var args = []; for(var i = 0; i < arguments.length; ++i) args.push(arguments[i]); return function() { …
bfops
  • 5,348
  • 5
  • 36
  • 48
6
votes
1 answer

Is it possible to utilise partial application in Dart (partial / apply / fixing arguments)

From a function with multiple parameters can we partially apply just one or two parameters to it returning a new function that takes the remaining parameters? Javascript example using Ramda function buildUri (scheme, domain, path) { return…
atreeon
  • 21,799
  • 13
  • 85
  • 104
6
votes
2 answers

Privacy in JavaScript

Function scoping offers the only privacy in JavaScript. So the canonical: function Ctor(dep1, dep2) { this._dep1 = dep1; this._dep2 = dep2; } Ctor.prototype.foo = function() { // use this._dep1/2... } ...is problematic in that it offers no…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
6
votes
1 answer

Understanding Type Projection

Taken from typelevel/kind-projector, what's the distinction between: // partially-applied type named "IntOrA" type IntOrA[A] = Either[Int, A] and // type projection implementing the same type anonymously (without a name). ({type L[A] = Either[Int,…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
6
votes
5 answers

Partial application in Haskell with multiple arguments

Given some function f(x1,x2,x3,..,xN) it is often useful to apply it partially in several places. For example, for N=3 we could define g(x)=f(1,x,3). However, the standard partial application in Haskell does not work this way and only allows us to…