Ormuco
  • Solutions
    • Decentralized IaaS
    • Edge PaaS
    • Infrastructure Provisioning
    • Virtual SysAdmin
  • Why Ormuco?
    • Our Partners
    • Support
    • Use Cases
  • Pricing
  • About
    • About Us
    • Careers
    • Legal
  • Request a Demo
  • fr_CAFR
Ormuco
  • Solutions
    • Decentralized IaaS
    • Edge PaaS
    • Infrastructure Provisioning
    • Virtual SysAdmin
  • Why Ormuco?
    • Our Partners
    • Support
    • Use Cases
  • Pricing
  • About
    • About Us
    • Careers
    • Legal
  • Request a Demo
  • fr_CAFR

Currying Functions in JavaScript, Recursively

byLaurent Jacob inDevelopers Corner posted onJuly 25, 2019
0
0
Currying Functions in JavaScript, Recursively

The goal of this post is to be able to modify any JavaScript function so that it can be partially applied. This process is also known as currying. An example of currying could be a simple add:

function
add (x, y, z) { return x + y + z }

We would like to be able to do the following:

// yields a function that needs 2 more arguments
let partialAdd = add(1)// yields 6
let result = partialAdd(2, 3)

It means that we can call the function however we want (fully or partially). If the function is partially called, it returns a function that expects the remaining arguments:

add(1, 2, 3) // yields 6
add(1)(2)(3) // yields 6

Starting point

We will start by looking at a function found online here.

function schonfinkelize(fn) {
     var slice = Array.prototype.slice,
        stored_args = slice.call(arguments, 1);
     return function () {
        var new_args = slice.call(arguments),
              args = stored_args.concat(new_args);
        return fn.apply(null, args);
     };
}

This is an interesting function and a good attempt at making functions partially in JavaScript. The basic idea of this function is that it takes the original arguments and returns a function that waits for the remaining arguments.

You can use it this way:

// yields a function
let addSecondHalf = schonfinkelize(add, 1)// yields 6
addSecondHalf(2, 3)

The issue with this function is that we can’t do something like:

// Doesn't work
schonfinkelize(add, 1)(2)(3)

The reason it doesn’t work is because schonfinkelize is not called recursively by looking if we have enough arguments to call the function. In order to make the function work, we would have to do something like the following:

// yields 6
schonfinkelize(schonfinkelize(add, 1)(2))(3)

Solving it recursively

The first thing we need to ask ourselves is how we know if we have enough arguments to call the function. It turns out JavaScript has this built-in. Every function in JavaScript has the attribute length, which returns the number of arguments a function is expecting. So in our case:

// yields 3
add.length

With this information, we know that the base case of our recursive call will be called if we have enough arguments to call the function. With this in mind, let’s look at the first solution:

function partialApply(fn) {
    let slice = Array.prototype.slice
    let curr_args = slice.call(arguments, 1)    if (fn.length > curr_args.length) {
        return function() {
            let new_args = slice.call(arguments)
            let args = curr_args.concat(new_args)
            return partialApply.apply(null, [fn].concat(args))
        }
    }
        
    return fn.apply(null, curr_args)
}

So what does this function do exactly? We recursively call partialApply as long as we don’t have enough arguments. This ensures that we will wait until enough arguments are passed before computing any result. The base case is reached when enough arguments have been passed through one or many calls. Since we return in the recursive if statement, we can simply return the result at the end (that satisfies the base case).

This function allows us to do something like:

partialApply(add, 1)(1)(2) // yields 4
partialApply(add)(1)(1)(2) // yields 4
partialApply(add)(1, 1)(2) // yields 4
partialApply(add, 1, 1, 2) // yields 4

Using the function prototype

This is great, but it feels awkward to have to pass the function as a parameter. It would be great if we could curry directly on the function:

add.partialApply(1)(1)(2) // yields 4
add.partialApply(1, 1)(2) // yields 4
add.partialApply(1, 1, 2) // yields 4

This solution is very similar to the one above but it will be bound to the function prototype. Here is the final implementation of currying properly in JavaScript:

Function.prototype.partialApply = function() {
    let fn = this
    let slice = Array.prototype.slice
    let curr_args = slice.call(arguments)    if (fn.length > curr_args.length) {
        return function () {
            let new_args = slice.call(arguments)
            let args = curr_args.concat(new_args)
            return fn.partialApply.apply(fn, args)
        }
    }
        
    return fn.apply(null, curr_args)
}

In this function, we utilize the fact the JavaScript functions are first-class objects. This means that any function is an instance of the Function prototype and therefore will have access to our function partialApply. The this keyword in JavaScript refers to the instance calling that function.

The base case remains unchanged (it simply computes according to the original function definition). It gets interesting when we look at the recursive call because we need to ensure that context is properly passed.

return fn.partialApply.apply(fn, args)

We can’t simply call partialApply. We need to apply it because we only have access to the arguments in the form of an array. The other interesting part of this line is that we have to pass in fn as the first argument, since it defines what this (the context) will be in the function call. If we simply did:

return fn.partialApply.apply(null, args)

It would yield the error (“Cannot read property ‘apply’ of undefined”), since we would not bind this to the right function in the recursive call.

Usage

So now, how can I make it so all my functions can be partially applied? You can simply call .partialApply() on any function declaration you make. It looks like this.

let add = function(x, y, z) {
    return x + y + z
}.partialApply()let a = add(1, 2, 3) // yields 6
let b = add(1)(2)(3) // yields 6
let c = add(1)(2, 3) // yields 6

So this is how you curry your functions in JavaScript!

This article was originally published on Medium.

OpenStack Summit

Share:
Laurent Jacob

Laurent is on the team that’s developing Ormuco's dynamic application deployment platform. He graduated with a Bachelor of Software Engineering from McGill University in 2015. After selling a video game he co-developed shortly after graduation, Laurent worked in the cryptocurrency start-up space before joining Ormuco. He's been coding since age 14.

Previous

How to Set Up Ambassador with Socket.io

Next

Making a React application with reverse proxy and environment-awareness

Keep on reading

Making a React application with reverse proxy and environment-awareness
August 1, 2019
Making a React application with reverse proxy and environment-awareness
No Comments
A Quickstart Guide to Deploying Qinling in Production
July 11, 2019
A Quickstart Guide to Deploying Qinling in Production
No Comments
How to Set Up Ambassador with Socket.io
July 18, 2019
How to Set Up Ambassador with Socket.io
No Comments

Search

Categories

  • Business
  • Developers Corner
  • Press Releases
  • Technology

Ormuco Inc.

Ormuco's virtual user interface runs any application by bypassing processing on end-user device operating systems and cloud computing. With Ormuco, you can launch all your apps through a single platform that’s safer and faster than using apps on your device and in the cloud

Request a Demo

Company

  • Who Is Ormuco
  • Why Choose Ormuco
  • Newsroom
  • Careers

Solutions

  • Decentralized IaaS
  • Edge PaaS
  • Infrastructure Provisioning
  • Virtual SysAdmin

Legal

  • Privacy Policy
  • Pricing

Get in Touch

  • info@ormuco.com
© 2020 Ormuco Inc.