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

Explain Javascript's Function.prototype.bind browser shim

I'm just trying to really understand the following code which is from MDN. Its the shim for Function.prototype.bind: if (!Function.prototype.bind) { Function.prototype.bind = function(oThis) { if (typeof this !== 'function') { // closest…
gabereal
  • 304
  • 3
  • 11
0
votes
1 answer

Javascript currying: why does one closure scope example work, but another doesn't?

I'm trying to understand the video "Understanding Function Currying" on Vimeo ( http://vimeo.com/41238143 but not necessary to view it in order to understand this question). The example I understand Early in the video, We’re told that there’s a…
Ben Wheeler
  • 6,788
  • 2
  • 45
  • 55
0
votes
1 answer

Scala: reading lines from file with curried function in 'foreach'?

Reading lines from file I try to use curried function in 'foreach' : import scala.io.Source object CurriedTest { def main(args: Array[String]): Unit = { fun("one")("two")("three") (fun2)("three") val lst = List ("x", "y", "z") …
DarqMoth
  • 603
  • 1
  • 13
  • 31
0
votes
1 answer

Scala: curried function in foreach?

I am trying to use curried function when iterating collection with 'foreach' method: object CurriedTest { def main(args: Array[String]): Unit = { fun("one")("two")("three") (fun2)("three") val lst = List ("x", "y", "z") …
DarqMoth
  • 603
  • 1
  • 13
  • 31
0
votes
1 answer

functools.partial vs normal Python function

I am learning about partials and when to use them. In this page about partials vs lambdas, the accepted answer explains that one of the advantages of partials over lambdas, is that partials have attributes useful for introspection. So we can do the…
usual me
  • 8,338
  • 10
  • 52
  • 95
0
votes
1 answer

Tracing out the type of partial application of functions

I have been inspecting the type of Lens to understand it and have trouble figuring out the resulting type of partial application there. Initial type is like this: type RefF a b = forall f. Functor f => (b -> f b) -> (a -> f a) The above type applied…
Sibi
  • 47,472
  • 16
  • 95
  • 163
0
votes
1 answer

Is it possible to define hoisted curried functions in Livescript?

How do you define a curried function with the function keyword in Livescript? More succinctly, how can I get this function curry (arg1, arg2) do-something arg1, arg2 To act like this curry = (arg1, arg2) --> do-something arg1, arg2
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
0
votes
1 answer

Haskell: Partially applied function, function composition in a raytracer program

I have a problem understanding the function composition and the concept of partially applied functions. Actually I'm writing a small raytracer and have some example implemenentations which I dont understand exactly. Here is one example which I dont…
0
votes
2 answers

Why isn't Haskell Partial Application Working?

My work in Haskell comes in the form of re-working .Net F# projects in Haskell for the fun of it. I'm parsing a regular Windows configuration file--one key/value pair per line, key separated from value by =. This file is very simple and…
Jeff Maner
  • 1,179
  • 9
  • 23
0
votes
1 answer

Getting var vs val from partially applied function

I'm trying to create a convenience wrapper around my database calls in scala by using a partially applied function: def queryResult[B](connection: Connection, sql: String)(process: (CallableStatement,ResultSet) => B): B = using (connection) {…
stan
  • 4,885
  • 5
  • 49
  • 72
0
votes
2 answers

Partial application of operators

If I want to add a space at the end of a character to return a list, how would I accomplish this with partial application if I am passing no arguments? Also would the type be? space :: Char -> [Char] I'm having trouble adding a space at the end due…
user1670032
  • 750
  • 3
  • 12
  • 27
-1
votes
1 answer

Python partial function doesn't execute

I'm new to using partial functions in Python. Here's a piece of simple code, and I am expecting it to print out results, but somehow it doesn't print anything, or say otherwise show that firstfunc gets executed: from functools import partial class…
viva
  • 13
  • 4
-2
votes
2 answers

Are partially applied functions and Closures orthogonal in Scala?

Suppose I have the following code: val someNumbers = List(-11, -10, -5, 0, 5, 10) someNumbers.foreach( println _ ) val j = 10 (x: Int) => x + j My question is Are partially applied functions and Closures orthogonal in Scala? This presentation…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1 2 3
16
17