DeOSphere
  • 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 DeOSphere?
  • Pricing
  • Security
  • About
DeOSphere
  • 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 DeOSphere?
  • 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

November 15, 2025
Организации bubnovsky38.ru ответственной игры а также опции защиты игроков во онлайн-казино
No Comments
November 27, 2025
Il modo migliore per ottenere il Wins casino massimo dei bonus aggiuntivi del casinò online
No Comments
December 5, 2025
Опции отвечающей Казино Олимп забавы вдобавок цифровые машины контролирования на онлайн-игорный дом
No Comments

Search

Categories

  • ! Без рубрики
  • 1
  • 100%A Z
  • 11
  • 111
  • 12
  • 1200 Z
  • 1200 Z
  • 13
  • 1500 Z
  • 1500 Z
  • 1500 Z
  • 1500A Z
  • 19
  • 2
  • 2000 Z
  • 2000A Z
  • 20bet
  • 25
  • 3
  • 4
  • 5
  • 500A Z
  • 6
  • 7
  • a16z generative ai
  • Ace Spinz Casino
  • All Check
  • ar-t1-aio
  • asterisk-support.rubadges 100
  • asterisk-support.ruquestion87844jetton-vhod 800
  • aug_bahsegel
  • aug_ch_common
  • aug_pinco
  • avite.ru a 1500
  • azurslot
  • Baxterbet Casino
  • best online casino
  • bestcasinogame151017-18
  • bestslotcasino21101
  • Betero Casino
  • Betfouru Casino
  • Betify
  • bh
  • bh_1
  • bh_main_may
  • bizzo casino
  • bk-pokerdom-bonus.clients.site 501
  • Blog
  • BLOG DE
  • blog-ch
  • bonus-kod-pokerdom-promokod.clients.site 510
  • Bonuskong Casino
  • bonusy-v-pokerdom.clients.site 520
  • boomerang
  • Bruno Casino Login
  • bt
  • bt_main
  • Business
  • campobet
  • casino
  • Casino AT
  • casino bez vkladu
  • casino ch
  • Casino DE
  • Casino Lizaro
  • Casino Snatch
  • Casino uden ROFUS
  • casino utan svensk licens
  • casino zonder crucks
  • Casino-GR
  • casino/betting/nutra
  • Casino1
  • Casino2
  • Casino3
  • Casino4
  • casino7k-cash.cam 700 b
  • Casperbets Casino
  • ch
  • ChanceBit Casino
  • Channel News
  • Chanze Casino
  • Chicken Road 2
  • Cloud News
  • cocobet
  • coldgrindorganic.com
  • comp-doma.ru a 1200
  • Computers, Games
  • Construction News
  • crossarea.ru a 1200
  • crownplay
  • crypto
  • CuppaWins Casino
  • Daily Mriya Social
  • de.trustpilot.com 1500
  • deutschklasse.ru a 1500
  • Developers Corner
  • dyuc-kontakt.ru a 1500
  • ehorussia.ru a 1200
  • Escort Jobs
  • Euteller Kasinot
  • Evolve Casino
  • exactlyawards.ru a 1500
  • Fair Go Casino
  • fairspin
  • Fatbet Casino
  • findfood.ru 2000
  • fr t2
  • friroll-na-pokerdom.clients.site 513
  • gamblezen
  • Gamblezen Casino
  • Gambling
  • gambling, games
  • Golden Ring Casino
  • HR News
  • Ilmaiskierrokset Ilman Talletusta
  • ipg-ltd
  • ivspeedcentr.ru a 1200
  • Jackbit Casino
  • Jeu Du Poulet
  • jule_canli_common
  • jule_ch_main
  • Jule_pb_common
  • jule_pb_ormanistanbul.com
  • jule_pb_zurichberg.com
  • jule_sb_main
  • jully_rb_sarosdanenerede.com
  • july_pinco_common
  • July_pinco_Sepi
  • july_rb_common
  • july_rb_shopharbourroad.com
  • kak-poluchit-10-na-pokerdom.clients.site 542
  • kak-popolnit-schet-v-pokerdom.clients.site 506
  • Kakadu Casino
  • kakoj-promokod-v-pokerdom.clients.site 506
  • kazino-pokerdom-ofitsialnyj-1789481061433.clients.site 508
  • Kingdom Casino Login
  • Kingz Casino
  • kz-ru
  • lagazzettacatanese.it 453
  • Lamabet Casino
  • legiano
  • lippsy.se
  • liustudentsecondhand.se
  • Lizaro
  • Lizaro Casino
  • Lolajack Casino
  • Looniegold Casino
  • Lucki Casino
  • Lucky Minning Casino
  • luxury casino
  • Mafia Casino
  • Magic Win Casino
  • magius
  • Millioner Casino
  • Millionz Casino
  • Mino Casino
  • mystake
  • Mystake Casino
  • Nana Casino
  • nauticboats.ru 72
  • News
  • Ninewin
  • novos-casinos-2026
  • Nv Casino Online
  • oktober-adm.ru 250 a
  • online casino zahranicni
  • onlinecasinogame211023-24
  • onlyspins
  • ozwin
  • pages
  • Parhaat Kasinobonukset
  • paroli-friroll-pokerdom-1789558662627.clients.site 513
  • Paysafecard Casino
  • pb
  • pc
  • pistolo
  • platinum play
  • playamo
  • Playzini Casino
  • poker-pokerdom.clients.site 505
  • pokerdom
  • pokerdom test 2
  • pokerdom test 2 – Copy
  • pokerdom-avtomaty.clients.site 501
  • pokerdom-bk.clients.site 520
  • pokerdom-bonus-bez-depozita.clients.site 503
  • pokerdom-bonus-promokod-pri.clients.site 511
  • pokerdom-brauzer.clients.site 526
  • pokerdom-forum.clients.site 509
  • pokerdom-friroll-parol-na.clients.site 505
  • pokerdom-gorjachaja-linija.clients.site 509
  • pokerdom-klient-na-android.clients.site 1003
  • pokerdom-na-realnye-dengi-na.clients.site 518
  • pokerdom-na-realnye.clients.site 1001
  • pokerdom-ofitsialnyj-sajt-1789712039484.clients.site 509
  • pokerdom-ofitsialnyj-sajt-1789713208145.clients.site 512
  • pokerdom-ofitsialnyj-sajt-1790057697145.clients.site 508
  • pokerdom-ofitsialnyj-sajt-i.clients.site b 1000
  • pokerdom-ofitsialnyj-sajt-na-1789656400084.clients.site 507
  • pokerdom-ofitsialnyj-skachat-1790070149183.clients.site 534
  • pokerdom-otzyvy-2026.clients.site 1002
  • pokerdom-paroli-friroll.clients.site 503
  • pokerdom-poker-skachat-na-pk.clients.site 503
  • pokerdom-skachat-pk.clients.site 516
  • pokerdom-vhod-na-sajt.clients.site 518
  • pokerdom-vyvody.clients.site 511
  • pokerdom-zablokirovali.clients.site 516
  • Post
  • press
  • Press Releases
  • pro-sarapul.ru 250 b
  • proverka
  • pu
  • public
  • Qbet Casino
  • Qzino Casino
  • rabochij-promokod-pokerdom-1789539101435.clients.site 515
  • rb_kasynolupa
  • rb_main
  • rc-langepas.ru 2000
  • reklama-pokerdom.clients.site 517
  • Retail News
  • review
  • Ringospin Casino
  • robocat
  • Rokubet
  • roobet
  • rprf.ru b 200
  • sadisibiri.ru a 1200
  • Sankra
  • sat-ch
  • sb_main
  • ska-khabarovsk.ru
  • skachat-pokerdom-dlja-androida.clients.site 537
  • skopinmuseum.ru b 1300
  • slota
  • Slotexo Casino
  • slotsgem
  • Slottyway
  • spinanga
  • Spinboss Bonus
  • Spinboss Login
  • Spinfin Casino
  • SpinRain Casino
  • Spins
  • Spins Castle Casino
  • spinyoo
  • sportaza
  • stage4u.ru 2000
  • Stake Casino France
  • studio-petukh.ru a 1300
  • Superlines Casino
  • t.mePokerdom_2026
  • tarif73.ru a 1300
  • Technology
  • textsHU
  • thebeautifullife
  • Uncategorized
  • Uudet Kasinot Ilman Rekisteröitymistä
  • Uudet Verovapaat Nettikasinot
  • Uusi Kasino
  • Vasy Casino
  • Vegas Hero
  • Vegashero Casino
  • velobet
  • Velobet Casino
  • Verovapaa Casino
  • Verywell Casino
  • Vidobet Casino
  • VipLuck Casino
  • wazamba
  • Westace Casino
  • winbay
  • Winbig21 Casino
  • wingaga
  • Winit Casino
  • Winlegends Casino
  • Winnita Casino
  • winshark
  • wiz slots
  • www.admsoln.ru a 1500
  • www.cashbaka.ru 2000
  • www.investigatore-privato-agenzia-investigativa-viterbo.it 451
  • www.notarius-falkova.ru a 1200
  • www.vitaconsacrataliguria.it 452
  • xn—-itbjbj2arv.xn--p1ai a 1500
  • Zimpler Kasino
  • zoome casino
  • Блог
  • Казино
  • Сплиты
  • яокб.рф a 1200

DeOSphere

DeOSphere 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 DeOSphere include compute, storage, networking, and databases.

Company

  • Why Choose DeOSphere?
  • Newsroom

Legal

  • Privacy Policy
  • Security
  • Pricing

Contact Us

  • info@deosphere.net