Reputation: 123
I am trying to duplicate target:[ "a", "b", "c"]
with count:[1, 2, 3]
Desirable output is ["a", "b", "b", "c", "c", "c"]
It's not working with this code :
const fn = ({ target, count }) => R.map (R.repeat (target, count))
const Data = { target : ["a", "b", "c"], count : [1, 2, 3] }
const result = fn(Data)
I am trying to find solution with Ramda.js.
Thanks.
Upvotes: 0
Views: 224
Reputation: 50797
Another fairly simple solution:
const fn = ({target, count}) =>
unnest (zipWith (repeat) (target, count))
const data = {
target: ['a', 'b', 'c'],
count: [1, 2, 3]
}
console .log (fn (data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script> const {unnest, zipWith, repeat} = R </script>
A point-free function of target
and data
is easy enough:
compose (unnest, zipWith (repeat))
If they are wrapped in an object and you really want point-free, then the answer from Hitmands seems best, or a variant using this technique:
compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
Upvotes: 3
Reputation: 14179
Probably something like the following:
const fn = R.pipe(
R.props(['target', 'count']),
R.apply(R.zip),
R.chain(R.apply(R.repeat)),
);
// ===
const data = {
target : ["a", "b", "c"],
count : [1, 2, 3],
}
console.log(
fn(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
Upvotes: 1