-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Ramda Conventions
Tomáš Konrády edited this page Nov 27, 2018
·
6 revisions
This page gathers rules followed by Ramda developers that have sublimed over the years of creating the library.
Function with the suffix By must take a unary function to derive information from data it needs to return a result.
// Bad
minWith(x => mod(x), [-1, 2, 5, -7])
// Good
minBy(x => mod(x), [-1, 2, 5, -7])On contrary, the suffix With is used for functions that take a function with arity greater than N where N > 1 to be applied to N items from data above which the function operates.
// Bad
uniqBy((a, b) => length(a) === length(b))([[1, 2], '12', 1, 2])
// Good
uniqWith((a, b) => length(a) === length(b))([[1, 2], '12', 1, 2])Notes:
- Function sortWith can take list of predicates.
- Neither startsWith or endsWith does apply this convention.
// Bad
log('Hello') // logs to console
// Good
logBy(window.console)('Hello')// Bad
add(1, 2) !== add(1)(2)
// Good
add(1, 2) === add(1)(2)// bad
sum(1, 2, 3) // 6
// good
sum([1, 2, 3]) // 6// bad
const incAll = (xs) => xs.map(x => x + 1)
// good
const incAll = (xs) => {
let ys = [];
for (let i = 0; i<xs.length; i++) {
ys.push(xs[i] + 1);
}
return ys;
}Ramda's often overlooked feature is support for transducers. Take in consideration if your function should support it too.