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

lodash flow and multiple arguments

I have 2 functions that I will add in a lodash flow: function normalizedFormFields(fields) { // needs only 1 argument return _.mapValues( fields, function( value ) { return { 'content': value }; } ); } function…
Robert Brax
  • 6,508
  • 12
  • 40
  • 69
2
votes
0 answers

Currying a variadic function in JavaScript

I'm trying to write a function sum that accepts any number of arguments (variadic) and is also curried. Example use: sum(5)(8)(3) // 5 + 8 + 3 = 16 sum(2)(1)(1)(5)(3)(2) // 2 + 1 + 1 + 5 + 3 + 2 = 14 Essentially, we…
2
votes
0 answers

How can I constrain a function to not accept partially applied functions

I have a serialization function that I feed data into, some times I miss an argument to the data generating function, and as a result I serialize the partially applied function. Json.Net does not throw up about this, but provides a nice empty object…
Vidar
  • 272
  • 1
  • 12
2
votes
1 answer

Why does partial application require to specify a parameter type?

Why does partial function application in Scala require a type to be supplied, like in: def func(a: Int, b: Int) = ??? def func1 = func(_ : Int, 1) // compiles fine def func1x = func(_, 1) // does not compile // error: missing parameter type for…
Suma
  • 33,181
  • 16
  • 123
  • 191
2
votes
2 answers

Partial application right to left?

When using bind in JS, one can create functions with predefined arguments, e. g.: var add = function (a, b) { return a + b; }; var addToThree = add.bind(null, 3); But how to I do this if I want to predefine the second, third etc. argument, but…
lupor
  • 138
  • 7
2
votes
2 answers

function templates, partial application and template argument deduction

I try to get the following main function to compile and work like expected: int main() { auto square = [](int x){ return x*x; }; typedef std::vector Row; typedef std::vector Mat; Mat mat; auto squareElements =…
2
votes
2 answers

Lift Req object

in liftbook, there's an example of creating of a Req instance by using apply : case Req(List("api", "expense", eid), "", GetRequest) => () => showExpense(eid) but when I look into api documentation, there are two apply() methods, but I don't know…
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
2
votes
2 answers

php advanced functional programming - creating a curry method similar to ramdaJS

Requirements: place holder support partial applied functions can be applied to partially applied functions currying 5.6 PHP support here is my attempt however it only supports hhvm 3.7 as you can see in the example on http://3v4l.org/0i5FV
arcanine
  • 1,933
  • 1
  • 15
  • 22
2
votes
1 answer

Missing arguments for method toArray in trait List when converting from java ArrayList to scala Array

I have this simple code: import java.util import scala.collection.JavaConversions._ def f(x: util.List[Int]): Array[Int] = { x.toArray[Int] } It is failing on error: missing arguments for method toArray in trait List However the source code for…
mirelon
  • 4,896
  • 6
  • 40
  • 70
2
votes
2 answers

What's the best practice for passing a Javascript function with parameters without executing it?

What's the most efficient/the best practice for passing a Javascript function with parameters without executing it, and why? Here is the choice I know : Make a anonymous method jQuery("#target").click(function() { …
2
votes
2 answers

How to use functools.partial with os.path.join?

Have a list of paths + filenames, all starting with the same root_dir. How do I use partial? Attempt from os.path import join as path_join from functools import partial from tempfile import gettempdir root_dir = gettempdir() root_join =…
2
votes
3 answers

F# point free style division

I'm looking for a way to do point-free style division. Point free style works great with most mathematical operators where the order of the arguments doesn't matter as in the case of multiplication, subtraction or addition. But the problem is with…
Overly Excessive
  • 2,095
  • 16
  • 31
2
votes
2 answers

Function Composition - Haskell

again another question generated by my attempts at the Project Euler questions (follow on from a previous question). I'm having trouble understanding the following line: print (maximum (map (product . take 13) (tails number))) Specifically map…
Dave0504
  • 1,057
  • 11
  • 30
2
votes
3 answers

zip function requires also a second list, how can it work with only one argument list

I started learning Haskell and found a nice exercise. It's the following: grouping: Int -> [Student]->[(Team, Student)] grouping teamNumber = zip ys where ... So, the exercise wants that i try to fill the rest. The function…
user3097712
  • 1,565
  • 6
  • 27
  • 49
2
votes
1 answer

How to partial conj?

I'm trying to create a function that applies several processes to a map, including adding / updating some standard items to each map using "conj". I'm doing it by composing several other functions using "comp". So I tried doing this (defn…
interstar
  • 26,048
  • 36
  • 112
  • 180