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

Partial Function XCode 9 Issue

So I have the following partial function implementation: func addObserver(with notificationType: String) -> (@escaping (Notification) -> (Void)) -> NSObjectProtocol { return { (function: @escaping (Notification) -> (Void)) in …
0
votes
1 answer

Best use cases for currying functions and partially applied functions in scala

I'm trying to learn Scala and I'm confused when to use currying functions over partially applied functions. I'm pretty sure those concepts were not made to be redundant, but I can't see the real purpose of the different approaches. I was…
elaspog
  • 1,635
  • 3
  • 21
  • 51
0
votes
0 answers

composition and partial application on haskell

I'm trying to do this: Define the function evenOutcome, which applying it with an n number and an m number, returns true if the result of raising n to the number m is even. Note: Solve it using partial application and composition. I did this:…
cde33
  • 27
  • 5
0
votes
2 answers

Function currying in Haskell

I have a function: powerOf :: Int -> Int -> Int example os usage: *Main Data.List> powerOf 100 2 2 *Main Data.List> powerOf 100 5 2 I have two questions. First - why it doesn't works: map (powerOf 100) [2, 5] I want to get [2, 2]. And second…
ceth
  • 44,198
  • 62
  • 180
  • 289
0
votes
1 answer

JavaScript pre set function parameter

I have a series of events I'm listening to (not DOM ones), and an object containing event handler functions. Typically I'd do something like whenEventOccurs('event-name', handler.eventName) Except that I have to pass a parameter to the handler that…
Oliver
  • 1,576
  • 1
  • 17
  • 31
0
votes
2 answers

Javascript addEventListener and removeEventListener with partial function

I want to create javascript function adds an event listener to a div such that when the div is clicked, it runs a function, and ensures that the div can only be clicked once. My function looks like this right now: function clickOnce(divID,func){ …
user1763510
  • 1,070
  • 1
  • 15
  • 28
0
votes
2 answers

How to get parent object when context is function?

I would like to write curring function for object methods. I want to make this possible: Function.prototype.curry = function (){ var originalFunction = this; var args = ...; // here goes logic embracing arguments var bind = ???; //how to…
Marecky
  • 1,924
  • 2
  • 25
  • 39
0
votes
1 answer

Is there a way to predict infix function behavior for partially applied functions in Haskell?

I have two functions-- partialSubtractionWith5 :: (Num a) => a -> a partialSubtractionWith5 = (subtract 5) and partialSubtractionWith5' :: (Num a) => a-> a partialSubtractionwith5' = (`subtract` 5) calling partialSubtractionWith5 x returns the…
HandsomeGorilla
  • 585
  • 1
  • 3
  • 20
0
votes
1 answer

Java8 expressing conditionals as an array of method reference

I am representing a huge swath of objects (specifically MIPS32-instructions). My minimum working example will be describing an instruction in the R-format. MIPS32 background (R-type instruction) An R-type instruction is determined uniquely by the…
0
votes
1 answer

What is the mechanism by which functions with multiple parameter lists can (sometimes) be used with less than the required number of parameters?

Let me introduce this question by way of an example. This was taken from Lecture 2.3 of Martin Odersky's Functional Programming course. I have a function to find fixed points iteratively like so object fixed_points { println("Welcome to Fixed…
thetrystero
  • 5,622
  • 5
  • 23
  • 34
0
votes
1 answer

How to leverage power of TextWriterFormat for printfn style in combination with ConditionalAttribute which requires unit result

I set myself to creating a trace function that behaves like sprintf or printfn, but is disabled (JIT removes it on call site) for Release builds by using the ConditionalAttribute. Result so far: I don't think it is possible. The problem centers…
Abel
  • 56,041
  • 24
  • 146
  • 247
0
votes
0 answers

Partial application for list of functions

How do you partially apply a list of functions? The following minimal example: def myprint(a,b): print 'a: '+str(a)+', b: '+str(b) l1=[lambda x:myprint('x',x), lambda y:myprint('y',y)] l2=[] for f in l1: l2.append(lambda:f('!')) for g…
Jann Poppinga
  • 444
  • 4
  • 18
0
votes
2 answers

Haskell partial applications to add 1 and double

I have to write a program that takes in an Integer and then uses two partial applications to first increment the number by one and then the second partial application doubles it. I know that a partial application would be one that would take less…
Nived
  • 1,804
  • 1
  • 15
  • 29
0
votes
1 answer

Is there a way to partially apply getLine?

Is there a way to partially apply getLine to writeFile or a similar function (for file naming purposes)? I want to do something like this: main = writeFile ??? . computeSomething =<< somethingElse ??? should be a line entered by the user to name…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
0
votes
1 answer

How to call partial functions

I have this sample code(a .js library needs to be consumed) for formatting the date. Have never used partial functions before so not quite sure how to use it. How does one call them? Here is the utility library code: define(function() { 'use…
Surily
  • 121
  • 2
  • 10
1 2 3
16
17