Take a look at this functioning code snippet:
var randNum = x => () => Math.floor(x*Math.random());
var random10 = randNum(10)
times(random10, 10) // => [6, 3, 7, 0, 9, 1, 7, 2, 6, 0]
The function randNum
creates a random number generator that will output an integer between 0 and N-1 when invoked. It serves as a factory for specific RNGs.
While experimenting with ramda.js and delving into functional programming concepts, I wondered if it's possible to refactor randNum
into a point-free style using ramda?
One approach could be:
var unsuccessfulTry = pipe(multiply(Math.random()), Math.floor)
This implementation meets the "point-free style" criteria, but it diverges from the behavior of randNum
: executing unsuccessfulTry(10)
only results in a single random number instead of generating random numbers within the specified range each time it's called.
Despite numerous attempts, I haven't found the right combination of ramda functions to achieve a point-free rewrite. I'm unsure if this is due to my limitations or perhaps some intricacy related to utilizing random
, which compromises referential transparency and might conflict with a point-free approach.
Update
Following discussions with Denys, here's a slight modification to the solution I came up with:
randNum = pipe(always, of, append(Math.random), useWith(pipe(multiply, Math.floor)), partial(__,[1,1]))