无法使用 ramda 的 R.cond 读取 null 的 属性 'length'

Cannot read property 'length' of null with ramda's R.cond

这里有 2 种方法,未​​注释的一种有效,第二种使用 ramdajs 无效:

    // const dies = col && 2 > count > 3
    // const lives =
    //  (col && (count === 2 || count === 3)) || (!col && count === 3)

    // return dies ? null : lives ? true : null

    const result = (_col, _count) =>
        R.cond([
            [_col && (_count === 2 || _count === 3), true],
            [!_col && _count === 3, true],
            [_col && 2 > _count > 3, null],
            [R.T, null]
        ])

    return result(col, count)

我收到一个错误 Cannot read property 'length' of null

我觉得你把 Game of Life 规则写得太直接了。

我认为这样的事情会很好:

const nextGeneration = (col, count) => count == 3 || (col && count == 2)

这个 returns 是一个布尔值,而不是你的 true|null。这让我印象深刻,但如果你想要 null,只需在末尾添加一个 || null

这不使用 Ramda。我认为这里没有理由这样做(免责声明:我是 Ramda 的作者。)但是如果你愿意,我相信我们可以把它变成一些 Ramda 无积分版本,但它的可读性可能会低得多。

顺便说一句,您的版本似乎有问题。 2 > count > 3 是什么意思?即使在 JS 中确实扩展为 2 > count && count > 3,但事实并非如此,这永远不会是真的,因为 2 < 3.