DEV Community

myleftshoe
myleftshoe

Posted on

3 2

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay