javascript / intermediate
Snippet
Functional Currying for Array Logic
Currying allows you to create specialized versions of functions by partially applying arguments, making them highly reusable in array methods like filter and map.
snippet.js
1
2
3
4
5
6
const hasMinLength = (min) => (str) => str.length >= min;const words = ['node', 'code', 'javascript', 'backend'];const longWords = words.filter(hasMinLength(7));console.log(longWords); // ['javascript', 'backend']
nodejs
Breakdown
1
(min) => (str) => ...
A higher-order function that takes a configuration value and returns a new function that accepts the actual data.
2
words.filter(hasMinLength(7))
Calls the outer function to create a predicate specifically looking for strings with at least 7 characters.