Recurring code pattern #1: currying
See original GitHub issueIn 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:
- Created 5 years ago
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Writing
curry
itself is relatively quick, but refactor the whole codebase to use the newcurry
function is repetitive, time-consuming, and boring. I prefer not to.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 isfunc
(a.k.a.function functionName ()
), which is always guaranteed to have a name.