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

Zero or all optional but typed parameters

I am just about to learn some typed functional programming, so just started with an implementation of partial application - which should be type safe. Problem: I am trying to make a function that takes a function and zero or all of its parameters as…
philipp
  • 15,947
  • 15
  • 61
  • 106
3
votes
1 answer

No instance for (Num (Int -> Int)) arising from a use of syntactic negation

I have written the following Haskell code to return the primary and secondary diagonal of [[Int]] getDiagonal' :: [[Int]] -> Int -> (Int -> Int) -> [Int] getDiagonal' [] _ _ = [] getDiagonal' (x:xs) i fn = i' : getDiagonal' xs (fn i) fn where i' =…
mattematt
  • 445
  • 3
  • 10
3
votes
1 answer

Partial functions keeping their signature

We can use purrr::partial to create partial functions: f <- function(x, y) { print(x) print(y) return(invisible()) } ff <- purrr::partial(f, y = 1) ff(2) #> [1] 2 #> [1] 1 Created on 2020-02-19 by the reprex package (v0.3.0) This can often…
Wasabi
  • 2,879
  • 3
  • 26
  • 48
3
votes
2 answers

Dynamically adding builtin methods to point to a property's built-ins

I have a couple classes and a function: from functools import partial def fn(other, self, name): print(f"calling {name} with {other}") func = getattr(self.a, name) return func(other) class A: def __add__(self, other): …
MBeale
  • 730
  • 1
  • 6
  • 11
3
votes
1 answer

kind-projector returns strange results

I have these types: SomeTypeClass A higher kinded type which has one type parameter of kind * => * => * trait SomeTypeClass[P[_, _]] { def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T]) (pab: P[A, B]) …
3
votes
3 answers

How to partially apply a function with desired order in Elm?

Suppose I have a function that takes 3 parameters as input. How to partially apply this function in Elm so it takes first and last parameters and waits for the second parameter to return the final result? This can be done in Ramda with R.__ which is…
user6435535
3
votes
2 answers

Using map with function that has multiple arguments

Is it possible to use map with a function that takes multiple arguments? I want to use map's second and third arguments repeatedly as the function's arguments. As in mapF x y z = map (f y z) [1, 2, 3] So it'll evaluate f with the same y and z…
3
votes
0 answers

What is difference between Partial Application and Currying?

I feel like this is the chicken and egg problem. To make the comparison: Currying: x => y => z => u => value Partial Application: f_ab = (z,u) => value From my understanding: Currying creates a chain of unary functions. Partial…
user8459437
3
votes
1 answer

Haskell filter function with multiple parameters

I'm trying to learn Haskell and wondered how to filter a given list, with a function that takes multiple parameters, passing each element of the list with other unchanging elements to the function, to create a new list. I understand that I can do…
3
votes
1 answer

ordered reduce for multiple functions in python

Ordered list reduction I need to reduce some lists where, depending on element types, the speed and implementation of the binary operation varies, i.e. large speed reductions can be gained by reducing some pairs with specific functions first. For…
jawknee
  • 168
  • 1
  • 9
3
votes
1 answer

Partially applied recursive functions

def mainCaller() = { val name = "xyz" someList.foreach { u:Map => foo(name, u) } } def foo(name:String)(map:Map): Unit = { //match case.... //recursive call to foo in each case where name remains same, but map changes } how can I write…
scout
  • 2,336
  • 4
  • 21
  • 22
3
votes
1 answer

Why the difference between type signatures of the same F# function in module vs class?

Closely related to my question here, but actually a different question... Consider the following F#:- type TestClass() = let getValFromMap m k = Map.find k m let mutable someMap : Map = Map.empty let getValFromMapPartial key…
Bellarmine Head
  • 3,397
  • 2
  • 22
  • 31
3
votes
2 answers

Why does this point-free F# function behave differently from the non-point-free version?

Consider the following F#:- type TestClass() = let getValFromMap m k = Map.find k m let addToMap map k i = map |> Map.add k i let mutable someMap : Map = Map.empty let getValFromMapPartial key = getValFromMap someMap…
Bellarmine Head
  • 3,397
  • 2
  • 22
  • 31
3
votes
2 answers

SML map function

I have the function: map(map(fn x =>[x])) [[],[1],[2,3,4]]; Which produces: val it = [[],[[1]],[[2],[3],[4]]] I don't understand how this function works. Doesn't each map function need both a function and a list? It seems that there isn't enough…
user2796815
  • 465
  • 2
  • 11
  • 18
3
votes
3 answers

Understanding Jon Resig's implementation of partial application

The following code is taken from Jon Resig's book Secrets of JavaScript Ninja to explain how to use closures to implement partial application of functions. However I have issues understanding the intent of the variable arg. Why is it required and…
Geek
  • 26,489
  • 43
  • 149
  • 227