繁体   English   中英

如何使用 ramda pipe 减去两个数组值(来自不同的键)?

[英]How to subtract two array values (from different keys) using a ramda pipe?

ramda pipe中,我想减去两个数组键的值,最终得到一个包含这些差异的数组。

例如,考虑以下mice_weights数组。 我想得到一个具有差异weight_post减去weight_pre的数组,仅适用于雄性小鼠。

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

因此,基于这个答案,我可以构造 2 个等效管道get_pre()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]

虽然[8, 5]预期的 output,但我想知道使用ramda的 pipe 是否有更短的方法,例如:

// pseudo-code

const get_diff = R.pipe(
    filter_males,
    R.subtract("weight_pre", "weight_post")
)

get_diff(mice_weights) // gives [8, 5]

是否可以使用ramda实现类似的功能? 也许这样的任务有内置功能?

我建议使用propsreduceRight函数来实现:

const getProps = R.props(['weight_pre', 'weight_post'])

const subtract = R.reduceRight(R.subtract)(0)

const get_diff = R.pipe(
    R.filter(R.path(['is_male'])),
    R.map(R.pipe(getProps, subtract))
)

console.log(get_diff(mice_weights));

To get a the weight diff in a single object, create a function using R.pipe that takes the relevant props values with R.props , and applies them to R.subtract .

现在您可以创建一个过滤项目的函数,并使用权重计算 function 映射对象:

 const { pipe, props, apply, subtract, filter, prop, map, } = R const calcWeightDiff = pipe( props(['weight_pre', 'weight_post']), apply(subtract) ) const fn = pipe( filter(prop('is_male')), map(calcWeightDiff) ) 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}] const result = fn(mice_weights) console.log(result) // gives [8, 5]
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

抱歉,我不知道 ramda 管道,但这对于数组过滤和映射来说是一件小事。

const get_diff = (n, v) => // this takes a field and value to filter
       mice_weights 
       .filter(f => f[n] === v) // this keeps only datasets that have the field/value combo you're seeking
       .map(f => f.weight_pre - f.weight_post) // this gets the diff

 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 } ] const get_diff = (n, v) => mice_weights.filter(f => f[n] === v).map(f => f.weight_pre - f.weight_post) console.log(get_diff('is_male', true)) // gives [8, 5]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM