DEV Community

myleftshoe
myleftshoe

Posted on

A curry one-liner

What did the Indian restaurant manager say when a crowd gathered at the door? One at a time please.

Sorry.

The real question is what came first, the joke or the code? Let's just say they were mutually inspirational. Any excuse for a post and a bit of working stuff out.

const curry = (fn, ...args) => (args.length < fn.length) ? (...more) => curry(fn, ...args, ...more) : fn(...args)

function add(a, b, c, d, e, f)  {
    return a + b + c + d + e + f
}

console.log(add(1, 2, 3, 4, 10, 20)) // 40

const korma = curry(add)

console.log(korma(1)(2)(3)(4)(10)(20)) // 40

const vindaloo = curry(add, 10, 20)

console.log(vindaloo(1)(2)(3)(4)) // 40
console.log(vindaloo(1, 2)(3)(4)) // 40

const mixedVegetable = curry(add, 1)(2, 3, 4)

console.log(mixedVegetable(10, 20)) // 40
Enter fullscreen mode Exit fullscreen mode

There's probably some edge (or even inner-suburban) cases that are missing, it's all I need at the moment though.

Top comments (0)