以无点风格在 Ramda 中编写无参数函数?
Writing a parameterless function in Ramda in a point free style?
考虑下面的工作代码:
var randN = x => () => Math.floor(x*Math.random());
var rand10 = randN(10)
times(rand10, 10) // => [6, 3, 7, 0, 9, 1, 7, 2, 6, 0]
randN
是一个函数,它接受一个数字和 returns 一个 RNG,当被调用时,它将 return 一个范围为 [0, N-1] 的随机整数.所以它是特定 RNG 的工厂。
我一直在使用 ramda.js,学习函数式编程理论,我的问题是:是否可以使用 ramda 以无点风格重写 randN
?
例如,我可以写:
var badAttempt = pipe(multiply(Math.random()), Math.floor)
这将满足“无点样式”要求,但与 randN
的行为方式不同:调用 badAttempt(10)
只是 return 一个介于 1 和10,而不是 函数,它在调用时生成 1 到 10 之间的随机数。
我一直无法找到使我能够以无点样式进行重写的 ramda 函数组合。我不知道这是否只是我的失败,还是使用 random
的特殊之处,它破坏了引用透明性,因此可能与无点样式不兼容。
更新
在与 Denys 讨论后,我自己对解决方案的细微改动:
randN = pipe(always, of, append(Math.random), useWith(pipe(multiply, Math.floor)), partial(__,[1,1]))
var randN = R.converge(R.partial, [R.wrap(R.pipe(R.converge(R.multiply, [Math.random, R.identity]), Math.floor), R.identity), R.of])
var rand10 = randN(10)
alert(R.times(rand10, 10)) // => [3, 1, 7, 5, 7, 5, 8, 4, 7, 2]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.19.1/ramda.js"></script>
这将有助于一个额外的函数来抽象一个函数,以便在每次调用它时重新计算它的参数。
thunk = fn => R.curryN(fn.length, (...args) => () => fn(...args))
此函数的唯一目的是在给定的 fn
函数中产生一些副作用。
一旦我们有了thunk
函数,我们就可以像这样定义randN
:
randN = thunk(R.pipe(S.S(R.multiply, Math.random), Math.floor))
R.times(randN(10), 5) // e.g. [1, 6, 9, 4, 5]
注意:S.S
这里的 S combinator from Sanctuary 与 R.converge(multiply, [Math.random, identity])
.
的作用相同
不过,我确实只建议使用无点解决方案,前提是它确实提高了函数的可读性。
我不知道使用特定库学习函数式编程是否是个好主意,因为库的特性和函数范式不可避免地会混合在一起。然而,在实践中,Ramda 非常有用。它在 Javascript :D
中弥合了命令式现实与功能性 Fantasy Land 之间的差距
这是一个手动方法:
// a few generic, reusable functions:
const comp = f => g => x => f(g(x)); // mathematical function composition
const comp2 = comp(comp)(comp); // composes binary functions
const flip = f => x => y => f(y)(x); // flips arguments
const mul = y => x => x * y; // first class operator function
// the actual point-free function:
const randN = comp2(Math.floor)(flip(comp(mul)(Math.random)));
let rand10 = randN(10); // RNG
for (let i = 0; i < 10; i++) console.log(rand10());
值得一提的是 randN
是不纯的,因为根据定义随机数是不纯的。
考虑下面的工作代码:
var randN = x => () => Math.floor(x*Math.random());
var rand10 = randN(10)
times(rand10, 10) // => [6, 3, 7, 0, 9, 1, 7, 2, 6, 0]
randN
是一个函数,它接受一个数字和 returns 一个 RNG,当被调用时,它将 return 一个范围为 [0, N-1] 的随机整数.所以它是特定 RNG 的工厂。
我一直在使用 ramda.js,学习函数式编程理论,我的问题是:是否可以使用 ramda 以无点风格重写 randN
?
例如,我可以写:
var badAttempt = pipe(multiply(Math.random()), Math.floor)
这将满足“无点样式”要求,但与 randN
的行为方式不同:调用 badAttempt(10)
只是 return 一个介于 1 和10,而不是 函数,它在调用时生成 1 到 10 之间的随机数。
我一直无法找到使我能够以无点样式进行重写的 ramda 函数组合。我不知道这是否只是我的失败,还是使用 random
的特殊之处,它破坏了引用透明性,因此可能与无点样式不兼容。
更新
在与 Denys 讨论后,我自己对解决方案的细微改动:
randN = pipe(always, of, append(Math.random), useWith(pipe(multiply, Math.floor)), partial(__,[1,1]))
var randN = R.converge(R.partial, [R.wrap(R.pipe(R.converge(R.multiply, [Math.random, R.identity]), Math.floor), R.identity), R.of])
var rand10 = randN(10)
alert(R.times(rand10, 10)) // => [3, 1, 7, 5, 7, 5, 8, 4, 7, 2]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.19.1/ramda.js"></script>
这将有助于一个额外的函数来抽象一个函数,以便在每次调用它时重新计算它的参数。
thunk = fn => R.curryN(fn.length, (...args) => () => fn(...args))
此函数的唯一目的是在给定的 fn
函数中产生一些副作用。
一旦我们有了thunk
函数,我们就可以像这样定义randN
:
randN = thunk(R.pipe(S.S(R.multiply, Math.random), Math.floor))
R.times(randN(10), 5) // e.g. [1, 6, 9, 4, 5]
注意:S.S
这里的 S combinator from Sanctuary 与 R.converge(multiply, [Math.random, identity])
.
不过,我确实只建议使用无点解决方案,前提是它确实提高了函数的可读性。
我不知道使用特定库学习函数式编程是否是个好主意,因为库的特性和函数范式不可避免地会混合在一起。然而,在实践中,Ramda 非常有用。它在 Javascript :D
中弥合了命令式现实与功能性 Fantasy Land 之间的差距这是一个手动方法:
// a few generic, reusable functions:
const comp = f => g => x => f(g(x)); // mathematical function composition
const comp2 = comp(comp)(comp); // composes binary functions
const flip = f => x => y => f(y)(x); // flips arguments
const mul = y => x => x * y; // first class operator function
// the actual point-free function:
const randN = comp2(Math.floor)(flip(comp(mul)(Math.random)));
let rand10 = randN(10); // RNG
for (let i = 0; i < 10; i++) console.log(rand10());
值得一提的是 randN
是不纯的,因为根据定义随机数是不纯的。