1

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].

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?

2
  • 1
    Try sizesStepped.apply(null, sizeMinMax(x)); Commented Nov 15, 2020 at 10:04
  • var sizesStepped = (min, max) => Array.from({length: (max - min) * 2 + 1}, (_, i) => min + i*0.5) Commented Nov 15, 2020 at 10:07

2 Answers 2

2

Wrap sizesStepped with 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>

Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in vanilla JavaScript by implementing a range method.

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);

1 Comment

While I appreciate the solution to my problem with vanilla javascript, I was looking more for a Ramda solution, I apologise for not stating this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.