Jan Bodnar
Jan Bodnar

Reputation: 11657

Is there a way to pass a parameter to a predicate in JavaScript using ramda?

I have a functional code in JavaScript with ramda library. I would like to have a generic function hasNChars and dynamically pass the parameter n. I cannot do R.any(hasNChars(10), words) because the function is evaluated.

So is there a way to pass a value for the n parameter somehow?

var R = require('ramda');

let words = ['forest', 'gum', 'pencil', 'wonderful', 'grace',
    'table', 'lamp', 'biblical', 'midnight', 'perseverance', 
    'adminition', 'redemption'];

let hasNChars = (word, n=3) => word.length === n;

let res = R.any(hasNChars, words);

console.log(res);

Upvotes: 3

Views: 1210

Answers (1)

jordrake
jordrake

Reputation: 530

You were close, you just need to create another function that takes N that you can immediately evaluate without needing to input word too, this way the N value is in scope for the final evaluation.

let hasNChars = (n=3) => (word) => word.length === n;

Usage: let res = R.any(hasNChars(10), words);

Usage with default n=3: let res = R.any(hasNChars(), words);

Upvotes: 3

Related Questions