Ramda 数组到多参数函数
Ramda array to multiple argument function
给定字符串 6-10
的输入,我想获得一个数字列表 [6,6.5,7,7.5,8,8.5,9,9.5,10]
。
这是我想出的:
const x = "6-10";
const sizeMinMax = R.pipe(R.split('-'), R.map(parseInt))
sizeMinMax(x); //[6,10]
const sizesStepped = (min, max) => R.unfold(minSize => minSize > max ? false : [minSize, minSize + 0.5], min)
sizesStepped(8,10); // [8,8.5,9,9.5,10]
如何将数组 sizeMinMax(x)
的输出通过管道传递给具有 2 个参数的 sizeStepped
函数?
您可以通过实施范围方法在原版 JavaScript 中完成。
const range = (start, stop, step) =>
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
const x = '6-10';
const [s, e] = x.split('-');
const step = 0.5;
const ret = range(+s, +e, step);
console.log(ret);
将 sizesStepped
换成 R.apply:
const sizeMinMax = R.pipe(R.split('-'), R.map(parseInt))
const sizesStepped = (min, max) => R.unfold(minSize => minSize > max ? false : [minSize, minSize + 0.5], min)
const fn = R.pipe(sizeMinMax, R.apply(sizesStepped));
const x = "6-10"
const result = fn(x)
console.log(result); // [8,8.5,9,9.5,10]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>
给定字符串 6-10
的输入,我想获得一个数字列表 [6,6.5,7,7.5,8,8.5,9,9.5,10]
。
这是我想出的:
const x = "6-10";
const sizeMinMax = R.pipe(R.split('-'), R.map(parseInt))
sizeMinMax(x); //[6,10]
const sizesStepped = (min, max) => R.unfold(minSize => minSize > max ? false : [minSize, minSize + 0.5], min)
sizesStepped(8,10); // [8,8.5,9,9.5,10]
如何将数组 sizeMinMax(x)
的输出通过管道传递给具有 2 个参数的 sizeStepped
函数?
您可以通过实施范围方法在原版 JavaScript 中完成。
const range = (start, stop, step) =>
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
const x = '6-10';
const [s, e] = x.split('-');
const step = 0.5;
const ret = range(+s, +e, step);
console.log(ret);
将 sizesStepped
换成 R.apply:
const sizeMinMax = R.pipe(R.split('-'), R.map(parseInt))
const sizesStepped = (min, max) => R.unfold(minSize => minSize > max ? false : [minSize, minSize + 0.5], min)
const fn = R.pipe(sizeMinMax, R.apply(sizesStepped));
const x = "6-10"
const result = fn(x)
console.log(result); // [8,8.5,9,9.5,10]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>