Consider the working code below:
var randN = x => () => Math.floor(x*Math.random()); var rand10 = randN(10) times(rand10, 10)
randN
is a function that takes a number and returns an RNG, which when called returns a random int in the range [0, N-1]. So this is a factory for specific RNGs.
I used ramda.js and studied the theory of functional programming, and my question is: is it possible to rewrite randN
in free style using ramda?
For example, I could write:
var badAttempt = pipe(multiply(Math.random()), Math.floor)
This will satisfy the dot-free requirement, but cannot behave the same as randN
: calling badAttempt(10)
simply returns a single random number from 1 to 10, not a function that generates a random number from 1 to 10 when called.
I was not able to find a combination of ramda functions that allows me to rewrite in style without restriction. I canβt say whether this is just a failure on my part or something special in using random
, which violates referential transparency and therefore may not be compatible with a dotless style.
Update
my little variation of the solution, after a discussion with Denys:
randN = pipe(always, of, append(Math.random), useWith(pipe(multiply, Math.floor)), partial(__,[1,1]))