Reputation: 123
I am basically trying to achieve the code below within the R.applySpec
.
const fn = ({target, count}) => R.unnest (R.zipWith (R.repeat) (target, count))
const Data = {
target : ["a", "b", "c"],
count : [1, 2, 3],
}
const data1= {
result : fn (Data)
}
console.log ( 'data1:', data1.result);// ["a","b","b","c","c","c"]
What I cannot figure out is that arguments in the fn
seems to be uncaught within the R.applySpec
const data2_applySpec = R.applySpec({
result : R.lift ( R.zipWith ( fn )) ( R.prop ('target'), R.prop ('count'))
})
const data2 = data2_applySpec(Data)
console.log ('data2:', data2);//ERROR
How can I alter the fn
to make it work?
I use Ramda.js.
Thanks.
Upvotes: 0
Views: 85
Reputation: 50787
I think you're making this harder than it needs to be.
You already have the function you want to use inside applySpec
stored as fn
.
So you can just write:
const fn2 = applySpec ({
result: fn
})
Or, if your only use of fn
is inside this applySpec
call, then just inline it:
const fn3 = applySpec ({
result: ({target, count}) => unnest (zipWith (repeat) (target, count))
})
And if you have a fetish for point-free code, you can use the technique discussed in your earlier post:
const fn4 = applySpec ({
result: compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
})
(or the similar version from Ori Drori.)
All of these are shown in this snippet.
const fn1 = ({target, count}) => unnest (zipWith (repeat) (target, count))
const fn2 = applySpec ({
result: fn1
})
const fn3 = applySpec ({
result: ({target, count}) => unnest (zipWith (repeat) (target, count))
})
const fn4 = applySpec ({
result: compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
})
const data = {target : ["a", "b", "c"], count : [1, 2, 3]}
console .log (fn1 (data))
console .log (fn2 (data))
console .log (fn3 (data))
console .log (fn4 (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script> const {unnest, zipWith, repeat, applySpec, compose, apply, props} = R </script>
Upvotes: 1
Reputation: 191976
You can can an array of arrays ([target, count]) using R.props
, apply the array of array to R.zipWith(repeat)
, and then flatten the results with R.unnest
:
const { applySpec, pipe, props, apply, zipWith, repeat, unnest } = R
const Data = {
target : ["a", "b", "c"],
count : [1, 2, 3],
}
const data2_applySpec = applySpec({
result: pipe(props(['target', 'count']), apply(zipWith(repeat)), unnest)
})
const result = data2_applySpec(Data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>
Upvotes: 2