Ramda: if func returns truthy value, use it in compose, else return as is

Vla*_*kyi 1 ramda.js

is there any neat Ramda way of writing something like this:

 const ifOkCompose = (...args) => {
            const r = foo(...args);

            if (r) {
                return compose(bar1, bar2)(r);
            }

            return r;
        };
Run Code Online (Sandbox Code Playgroud)

If foo() returns truthy value, send it to compose, otherwise just return it. There is 'when', but it would return arguments instead of returned value of 'foo(...)'.

In other words, I have a function foo which may return undefined, I use it in compose as compose(bar1, bar2, foo). If foo(...) returns undefined I want to kick out of compose and just return undefined.

Sco*_*yet 5

的docs示例R.composeWith接近您想要的。

const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res))

composeWhileNotNil([bar1, bar2, foo])
Run Code Online (Sandbox Code Playgroud)

最大的不同是,这将停止任何无响应的合成,而不仅仅是第一个。显然,如果您只想停止undefined而不想停止,则可以轻松地对其进行更改null