Search code examples
javascriptfunctional-programmingramda.jspointfree

Function composition of form f(x)(g(x)) using Ramda


I can't figure out a transform for f(x)(g(x)) using the functions available in Ramda. I'm hoping for something like R.something(f, g)(x) ideally - just so long as x only appears once and is the final argument.

f is a function taking x that returns a function

g is a function taking x that returns a value

I've tried pipe,compose,chain, but tbh I'm guessing pretty hard and no one of them work. I recently asked a similar question and I'd love to know a resource with loads of useful identities if one exists so I don't need to keep asking on SO :)


Solution

  • chain was close: it does chain(f, g)(x) == f(g(x))(x) on functions. So you just need to flip f before passing it into chain:

    R.chain(R.flip(f), g)(x)
    

    or

    const something = R.compose(R.chain, R.flip)
    something(f, g)(x)