Composing a function from an unredesigned function

I am trying to create a curriable function that returns whether or not the specified length of a string is equal or not. I would like it to work as follows:

checkLength(3)('asdf') // => false
checkLength(4)('asdf') // => true

At first I tried this, but the order of the arguments is reversed, because it returns a curried function equals:

const checkLength = R.compose(R.equals(), R.prop('length'))
checkLength('asdf')(4)

I can fix it by wrapping it in a function like this:

const checkLength = (len) => R.compose(R.equals(len), R.prop('length'))

But it seems that a functional library can be used to solve this problem. Does anyone have any idea?

+4
source share
2 answers

- flip - , :

const checkLength = R.flip(R.uncurryN(2, R.compose(R.equals, R.prop('length'))))
checkLength(4)('asdf') // => true

useWith:

const checkLength = R.useWith(R.equals, [R.identity, R.prop('length')]);
checkLength(4)('asdf') // => true
+2

answer Bergi - , . , , . , . ES6 . .

Ramda curry, :

const checkLength = curry((len, str) => str.length === len);
checkLength(3)('abcd'); //=> checkLength(3, 'abcd'); //=> false

, useWith. Ramda REPL.

+1

Source: https://habr.com/ru/post/1627708/


All Articles