Jiyus
  • All Resources & Services
    • Compute
      • Virtual Instances
      • Bare Metal Instances
      • Managed Kubernetes
      • Big Data
      • Functions Engine
      • Data Protection
    • Services
      • Managed Databases
      • Applications Marketplace
      • Orchestration
      • Developer Tools
      • Management Tools
      • User Interface
    • Storage
      • Object Storage
      • Block Storage
      • File Storage
    • Networking
      • Software-Defined Networking
  • Why Choose Jiyus?
  • Pricing
  • Security
  • About
Jiyus
  • All Resources & Services
    • Compute
      • Virtual Instances
      • Bare Metal Instances
      • Managed Kubernetes
      • Big Data
      • Functions Engine
      • Data Protection
    • Services
      • Managed Databases
      • Applications Marketplace
      • Orchestration
      • Developer Tools
      • Management Tools
      • User Interface
    • Storage
      • Object Storage
      • Block Storage
      • File Storage
    • Networking
      • Software-Defined Networking
  • Why Choose Jiyus?
  • Pricing
  • Security
  • About

Currying Functions in JavaScript, Recursively

byLaurent Jacob inUncategorized posted onJuly 25, 2019
0
0

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

March 7, 2019
The Internet of Things or IoT: The Sweet Taste of Decentralized Computing
No Comments
September 20, 2018
How to Build GDPR-Compliant Cloud Services
No Comments
February 6, 2018
[PR] Ormuco Fuels Global Expansion of Ormuco Stack, Announces Partnerships with ALSO, Oi, VEON
No Comments

Search

Categories

  • Business
  • Developers Corner
  • Press Releases
  • Technology
  • Uncategorized

Jiyus

Jiyus is an infrastructure-as-a-service platform that aggregates the computing resources of local data center providers, and transforms these resources into a global network of decentralized computing. Resources available on Jiyus include compute, storage, networking, and databases.

CREATE AN ACCOUNT

Company

  • Why Choose Jiyus?
  • Newsroom

Legal

  • Privacy Policy
  • Security
  • Pricing

Contact Us

  • info@jiyus.com
© 2020 Ormuco Inc.