In a ramda
pipe, I want to subtract the values of two array keys, to ultimately end up with an array of those differences.
For example, consider the following mice_weights
array. I want to get an array with the differences weight_post
minus weight_pre
, for the male mice only.
const mice_weights = [
{
"id": "a",
"weight_pre": 20,
"weight_post": 12,
"is_male": true
},
{
"id": "b",
"weight_pre": 25,
"weight_post": 19,
"is_male": false
},
{
"id": "c",
"weight_pre": 15,
"weight_post": 10,
"is_male": true
},
{
"id": "d",
"weight_pre": 30,
"weight_post": 21,
"is_male": false
}
]
So based on this answer, I can construct 2 equivalent pipes, get_pre()
and get_post()
:
const R = require("ramda");
filter_males = R.filter(R.path(["is_male"])) // my filtering function
const get_pre = R.pipe(
filter_males,
R.map(R.prop("weight_pre"))
)
const get_post = R.pipe(
filter_males,
R.map(R.prop("weight_post"))
)
res_pre = get_pre(mice_weights) // [20, 15]
res_post = get_post(mice_weights) // [12, 10]
const res_diff = res_pre.map((item, index) => item - res_post[index]) // taken from: https://stackoverflow.com/a/45342187/6105259
console.log(res_diff); // [8, 5]
Although [8, 5]
is the expected output, I wonder if there's a shorter way using ramda
's pipe such as:
// pseudo-code
const get_diff = R.pipe(
filter_males,
R.subtract("weight_pre", "weight_post")
)
get_diff(mice_weights) // gives [8, 5]
Is it possible to achieve something similar using ramda
? Perhaps there's a built-in functionality for such a task?