question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recurring code pattern #1: currying

See original GitHub issue

In almost every module, I see this pattern:

function functionName (func, iter) { ... }

export default function curriedFunctionName (func, iter) {
  if (typeof iter === 'undefined') {
    return iter => functionName(iter)
  }
  return functionName (func, iter)
}

These patterns can be eliminated by using a curry wrapper:

// src/internal/curry.mjs

export default function curry (func, length) {
  const curried = (...args) => args.length < length
    ? (...suffix) => curried(...args, ...suffix)
    : func(...args)
  return curried
}
// src/function-name.mjs

import curry from './internal/curry'

function functionName (func, iter) { ... }

export default curry(functionName, 2)

I also saw that some test suites test currying support of their test subjects. A curry wrapper should eliminate the needs of writing those tests over and over again by replacing them all with 1 single test suite that tests curry() itself.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
KSXGitHubcommented, Jan 22, 2019

Are you going to work on it @KSXGitHub ?

Writing curry itself is relatively quick, but refactor the whole codebase to use the new curry function is repetitive, time-consuming, and boring. I prefer not to.

1reaction
KSXGitHubcommented, Jan 21, 2019
  • some function has variable args in the curried version. That logic needs to be moved into the original function here for example

We just need to change curry a little.

I don’t think this is necessary: The reason you want a named function is for it to appear in error stack trace, is this the only reason? When use an error-proof curry(), the only place where errors could occur is func (a.k.a. function functionName ()), which is always guaranteed to have a name.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Currying - Rosetta Code
This shows how to create syntactically natural currying functions in C#. · Translation of: Groovy · (def plus-a-hundred (partial + 100)) (assert ( ......
Read more >
Perpetual Currying in JavaScript - codeburst
Currying is about decomposing a function taking multiple arguments into numerous functions with single arguments. The definition holds true for primitive ...
Read more >
Currying in Java - Baeldung
Learn how to improve your functional programming with chains of single parameter function calls. Currying turns multiple parameters into a ...
Read more >
Function Currying To Make Reusable Code - James Doyle
Currying is just a fancy way to describe a function that returns a function. Pretty simple right? I mean this literally. There are...
Read more >
Understanding JavaScript Currying with a practical use case
Curring is commonly used in modern JavaScript code, at least in its simpliest form, and combining it with pipe allow for a clean,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found