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

reasonml type higher order function

given the following module the compiler raises an error 41 │ }; 42 │ 43 │ module TestB = { 44 │ let minFn = (a, b) => a < b ? a : b; . │ ... 54 │ let max = reduceList(maxFn); 55 │ }; 56 │ 57 │ // module Number = { The…
hesxenon
  • 501
  • 3
  • 12
0
votes
3 answers

Is a section the result of currying?

In Programming in Haskell by Hutton In general, if # is an operator, then expressions of the form (#), (x #), and (# y) for arguments x and y are called sections, whose meaning as functions can be …
Tim
  • 1
  • 141
  • 372
  • 590
0
votes
2 answers

Not getting the same result from a partial application as with an infix opperatior "(%) x y <> x % y -> wtf"

I've just been doing some validation on value to see it is a product of three. Great use the modulus function. I want to pipe to it. Great use a partial application. But apparently not. This is an example from my fsi in vs code. > 27 % 3 - -…
Craig.C
  • 561
  • 4
  • 17
0
votes
1 answer

How to reason about partially applied method chaining

I am trying to understand how to reason about the types for partial applied method chaining . I do not understand why : :t (+)(+2) is (a->a)->a->a or why: :t (+)(+) is (a->a->a)->a->a->a I mean for the first example I do not understand , when…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0
votes
1 answer

How to have Bash scripts reference local files

My specific problem My goal is to provide a command-line utility for converting PowerPoint files to PDFs. I have tried solutions that use unoconv, but they don't do the conversion properly. I found a nice workflow which I saved as…
Damien Martin
  • 132
  • 1
  • 9
0
votes
3 answers

Is it possible to get the name of a partially applied function?

Suppose I have defined a function: def hello(name:String, words:String) = println("Hello!" + name + words) Then I defined a partial function: def p = hello _ Print p, displayed: (String, String) => Unit = There's no function name…
Freewind
  • 193,756
  • 157
  • 432
  • 708
0
votes
1 answer

How do I express partial function application in Typescript 3.x in a type-safe way?

I'm working on an Angular codebase that does some standard postprocessing on most API calls. This is done in a service class that wraps HttpClient.get() etc. in methods that pipe the returned observable through a bunch of intercepting methods. To my…
millimoose
  • 39,073
  • 9
  • 82
  • 134
0
votes
1 answer

Partially apply a function and dynamically call that function at runtime

Using F#, if I partially apply a function like this: let sleep x = Async.Sleep x |> Async.RunSynchronously let log date message= printfn "%s %s" date message let getDate = DateTime.Now.ToString() let logg = log getDate logg "First" sleep 1000 logg…
gmn
  • 4,199
  • 4
  • 24
  • 46
0
votes
2 answers

How does Haskell evaluate this signature?

ggt_euklid :: Nat1 -> (Nat1 -> Nat1) I am trying to learn partial application, I know that in this case, if the parentheses would be left out, I would get the same result, but I do not know how this signature should be evaluated. As far as I have…
Minimax
  • 121
  • 1
  • 5
  • 20
0
votes
1 answer

point-free style and partially applied functions

In Haskell there are two concepts that doesn't look like they are the same, but I don't understand the difference. They are "point-free style" and "partially applied functions". For point-free styles I'm going to get this example: instead of: sum xs…
user9640427
0
votes
1 answer

Scala Partial Function Composition

I am trying to compose these two functions: // some case class that just holds data case class DataMap( ... ) val action(i: Int)(data: DataMap): DataMap = { ... } val tryFunction: DataMap => Try[DataMap] = Try.apply[DataMap] val actionFunction:…
user3685285
  • 6,066
  • 13
  • 54
  • 95
0
votes
1 answer

Currying in_array() in PHP

Don't necessarily have a problem with how PHP does this or anything, more just a question out of curiosity. I am familiar with functional programming but am by no means an expert. I am writing functions currently and although I have no requirement…
0
votes
2 answers

Bound function not returning reference to object

let someArgs = {x:1, y:2} let dog = args => {return args} let cat = dog.bind(null, someArgs) someArgs = {x:3, y: 4} cat() // {x:1, y:2} Can someone explain why the call to cat does not return {x:3, y:4}? I thought objects were passed by reference?
0
votes
1 answer

Exposing Partially Applied Functions To Application

I'm working on a validation library containing a number of validators. Each validator is curried with a message as the first argument followed by any configuration values. A user of the library can pass in a map of message functions which will be…
0
votes
1 answer

spy on all args passed to a function composition

I'm trying to spy on all the params passed to a side-effect-ey function, which is composed with an anonymous function container receiving a final param (actually i want to stub it, but spying would be a start) classA.js const classB =…