[英]Ramda array to multiple argument function
Given the input of string 6-10
, I want to obtain a list of numbers [6,6.5,7,7.5,8,8.5,9,9.5,10]
.给定字符串
6-10
的输入,我想获得一个数字列表[6,6.5,7,7.5,8,8.5,9,9.5,10]
。
This is what I have come up with:这就是我想出的:
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]
How do I pipe the output of sizeMinMax(x)
which is an array to sizeStepped
function with 2 arguments?我如何 pipe
sizeMinMax(x)
的 output 是sizeStepped
function 和 2 arguments 的数组?
You can do it in vanilla JavaScript by implementing a range method.你可以通过实现一个范围方法在香草 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);
Wrap sizesStepped
with R.apply:包装尺寸采用
sizesStepped
:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.