以无点风格在Ramda中编写一个无参数函数

Writing a parameterless function in Ramda in a point free style?

本文关键字:参数 函数 一个 风格 Ramda      更新时间:2023-09-26

考虑下面的工作代码:

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是一个函数,它接受一个数字并返回一个RNG,当被调用时,该RNG将返回一个范围为[0,N-1]的随机int。所以这是一个特定RNG的工厂。

我一直在使用ramda.js,学习函数式编程理论,我的问题是:是否可以使用ramda以无点风格重写randN

例如,我可以写:

var badAttempt = pipe(multiply(Math.random()), Math.floor)

这将满足";无点风格";要求,但行为方式与randN不同:调用badAttempt(10)只返回1到10之间的单个随机数,而不是在调用时生成1到10间随机数的函数

我一直找不到一个ramda函数的组合,使我能够以无点的风格进行重写。我不知道这是我的失败,还是使用random的特殊之处,它破坏了引用的透明度,因此可能与无点样式不兼容。

更新

在与Denys讨论后,我自己对解决方案的细微变化:

randN = pipe(always, of, append(Math.random), useWith(pipe(multiply, Math.floor)), partial(__,[1,1]))

这将有助于使用一个额外的函数来抽象函数,以便在每次调用函数时重新评估其参数。

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是Sanctuary的S组合子,它的作用与R.converge(multiply, [Math.random, identity])相同。

然而,我只建议使用无点解决方案,如果它确实提高了函数的可读性。

我不知道使用特定库学习函数编程是否是个好主意,因为库的特性和函数范式不可避免地会混合在一起。然而,在实践中,Ramda非常有用。它弥合了Javascript:D中命令性现实和功能性幻想之地之间的差距

这是一种手动方法:

// 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 = 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>