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
1
vote
1 answer

How to type a partially applied function in typescript?

How can I properly type the return type of the following function without using any? It's a function that, depending on the presence of one parameter, returns a string or a function. function useFetchResource(resourceType: string, id?: string):…
1
vote
1 answer

F# - Partial application of a Int32.TryParse

I was trying to do this let TryParseAnyNumberStyle = Int32.TryParse(style= NumberStyles.Any, privider=CultureInfo.CurrentCulture) But the compiler complains The member or object constructor 'TryParse' does not take 0 argument(s). An overload was…
OrdinaryOrange
  • 2,420
  • 1
  • 16
  • 25
1
vote
3 answers

Applying Seq.map using 2 sequences to a method which takes 2 parameters

I'm writing a quick DB perf test, and chose F# so I can get more practice. I've created a method, measureSelectTimes, which has the signature Guid list * Guid list -> IDbCommand -> TimeSpan * TimeSpan. Then, I call it: let runTests () = let…
codekaizen
  • 26,990
  • 7
  • 84
  • 140
1
vote
0 answers

Can I rewrite this code to avoid the F# error

I have the following code in F# live version at https://repl.it/repls/CarefulGiganticExtraction let inline tryParse text = let mutable r = Unchecked.defaultof<_> (^a : (static member TryParse: string * ^a byref -> bool) (text, &r)), r let…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
3 answers

How to make this function more reusable/specific/better design?

I wrote this function below which transforms the passed array of products by product type and currency type function getProductsByCurrency(products, type, exchangeRate = 1) { var productsRetrieved = products.map(item => ({id: item.id, …
1
vote
1 answer

How does the outermost evaluation strategy evaluate partial application of a function and application of a curried function

Programming in Haskell by Hutton says When evaluating an expression, in what order should the reductions be performed? One common strategy, known as innermost evaluation, is to always choose a redex that …
1
vote
2 answers

cleanest partial application of subtraction operator

If I want a function that subtracts an int argument from the number 2, I can do let two_minus = (-) 2 But what if I want a function that subtracts 2 from an int argument? In Haskell, I can do let minus2 = flip (-) 2 But in Ocaml 4.02, flip is not…
Captain_Obvious
  • 520
  • 5
  • 15
1
vote
1 answer

Get arguments back from partially applied function in scala

Is there a way in scala to get the arguments back from a already partially applied function? Does this even make sense, should be done, or fits into any use case? example: def doStuff(lower:Int,upper:Int,b:String)= for(turn <- lower to upper)…
JaimeJorge
  • 1,885
  • 16
  • 15
1
vote
3 answers

Can't understand the result of the high-order function invocation provided with not partially applied function as an argument

I have a high-order function declaration to apply a function given as an argument twice: twice :: (a -> a) -> a -> a twice f x = f (f x) The confusion comes from this GHCi session: *Main> let _4 = twice twice *Main> let __4 = twice twice…
nyarian
  • 4,085
  • 1
  • 19
  • 51
1
vote
3 answers

Partial application to a specific parameter slot

Hello i want to know if it is possible to provide a parameter to a method at a specific position for further using point-free-notation: readData::Text->[Int] readData =catMaybes . maybeValues where maybeValues=mvalues.split.filterText …
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
1 answer

Python - How to change a lambda function to partial function?

The lambda function is: lambda x: x.split('=') the partial function would look something like: str.split('=')
Vasantha Ganesh
  • 4,570
  • 3
  • 25
  • 33
1
vote
1 answer

Partial application of type synonyms

I'm having trouble with unsaturated type synonyms in the following example: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE LiberalTypeSynonyms #-} module TypeFamilyHackery where data T k v a = T type family CollectArgTypes arr where CollectArgTypes…
1
vote
1 answer

adding a value with composition and partial application haskell

I'm trying to write this function applying composition and partial application with Haskell: function m n = (m^2) + n I tried this with: function m = (m^2).(+)
cde33
  • 27
  • 5
1
vote
0 answers

How to partially apply a function to yield a polymorphic rank-1 type?

I want to partially apply a function f :: T to a value x :: [Double] to get a resulting function f' :: forall a . Floating a => [a] -> a. What should T be? I can't figure it out. One difficulty is that, inside f, I need to combine x :: [Double] with…
1
vote
1 answer

Creating a function that returns a curried function (SML)

I've written a function that calculates a value of x, of a polynomial made from a list of reals. infixr 5 ^^; fun (x:real) ^^ 0 = 1.0 | (x:real) ^^ n = x*(x^^(n-1)); fun poly [] (x:real) = 0.0 | poly (hd::tl) (x:real) = hd*(x^^(length tl)) +…