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

How does an a function that increments a value work?

I'm trying to learn haskell after years of OOP. I'm reading Happy Haskell. It provides this code: plus :: Int -> Int -> Int plus x y = x + y plus' :: Int -> Int -> Int plus' = \x -> \y -> x + y increment :: Int -> Int increment = plus…
SilentDev
  • 20,997
  • 28
  • 111
  • 214
2
votes
1 answer

Isn't map takes a function and a list return a list?

map2_List :: (a -> b -> c) -> [a] -> [b] -> [c] map2_List f [] _ = [] map2_List f (a:as) bs = map (f a) bs ++ map2_List f as bs This is an example from my lecture, which try to apply a binary function to all pairs of elements of two lists. The part…
2
votes
1 answer

Unified interface between functions of different number of arguments

I have two functions like these: foo :: a -> b -> x -> x bar :: c -> y -> y I would like to unify them into single interface so they both could have same name. In my case it's guaranteed that types x and y are different (but there might be multiple…
Shersh
  • 9,019
  • 3
  • 33
  • 61
2
votes
3 answers

Is "a -> b -> (a -> b -> c) -> c" to apply two parameters a standard functional concept?

I came across the need for a function with the signature 'a -> 'b -> ('a -> 'b -> 'c) -> 'c to use for applying two arguments when piping: let apply2 x y f = f x y I needed this because I am using a function myFun : MyType -> TypeA -> TypeB ->…
cmeeren
  • 3,890
  • 2
  • 20
  • 50
2
votes
0 answers

Is there a nice way to partially-bind class parameters in Python?

Here's what I'm doing now: @memoized def reversed_matching(matching_cls): class ReversedMatching(ReversedSequenceMatching): def __init__(self, *args, **kwargs): super().__init__(matching_cls, *args, **kwargs) return…
Neil G
  • 32,138
  • 39
  • 156
  • 257
2
votes
2 answers

Python: Can I partially apply reduce with an initializer?

The initializer comes after the iterable. This causes problems for partial application. Consider these (trivial) examples: In [1]: from functools import reduce, partial In [2]: f = partial(reduce, lambda a,b: a+b, 100) In [3]:…
2
votes
2 answers

`apply` template compiles in g++ but not in clang++ and vc++

The following code compiles successfully in g++ 7.2.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors), but it fails to compile in clang++ 5.0.0 (with the same flags, -std=c++14 -Wall -Wextra -Werror -pedantic-errors) and…
2
votes
3 answers

What are the applications/advantages of using partially applied functions in scala?

We have partially applied functions in Scala- def sum(a:Int,b:Int,c:Int) = a+b+c val partial1 = sum(1,_:Int,8) I was wondering what are the advantages of using Partially applied functions. Or is it just a syntactical addition?
MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45
2
votes
0 answers

Emulating functions with an internal state

I have a working solution for what I am trying to achieve, but I am looking for simpler way to do it. I have a class that encapsulates a function and a user can pass a function (lambda expression) to it. Those functions always take one input data…
Johannes
  • 3,300
  • 2
  • 20
  • 35
2
votes
1 answer

Scala partial application via underscore when composing function literals

I am composing function literals, though unlike most examples I've seen I'm starting with a multi-argument function that is then curried. I have: //types case class Thing1(v: Double) case class Thing2(v: Double) case class Thing3(v: Double) type…
2
votes
2 answers

Need help in understanding types (based on curry)

curry f a b = f(a,b) I thought: the curry function takes function f a b and returns f(a, b), so i thought the type is: (a -> b -> c) -> (a, b) -> c so why the type is reversed?: ((a, b) -> c) -> (a -> b -> c)
Mariusz
  • 25
  • 1
  • 5
2
votes
1 answer

How to pass arguments automatically in LiveScript

How I can simplify line 3 (onChange property): Input do name: \input onChange: (event, value) ~> @limitInput { type: \string }, event, value
Developia
  • 3,928
  • 1
  • 28
  • 43
2
votes
1 answer

How To with Partial Function Application in Haskell

I am trying to use partial application to shorten the following valid function definition: ltest50 n = take 50 (iterate ltestonce n) I thought something like: ltest50 = take 50 (iterate ltestonce) or ltest50 = take 50 (iterate ltestonce$) would…
brander
  • 121
  • 4
2
votes
1 answer

Point Free Style Required for Optimized Curry

Say we have a (contrived) function like so: import Data.List (sort) contrived :: Ord a => [a] -> [a] -> [a] contrived a b = (sort a) ++ b And we partially apply it to use elsewhere, eg: map (contrived [3,2,1]) [[4],[5],[6]] On the surface, this…
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
2
votes
4 answers

C# define function with partial application as delegate

Consider the following method: int Foo(string st, float x, int j) { ... } Now I want to wrap it in a delegate of type Func by providing values for the parameters st and j. But I don't know the syntax. Can someone help? This is the…
Kjara
  • 2,504
  • 15
  • 42