Rodrigo Ruiz
Rodrigo Ruiz

Reputation: 4355

Apply parameter twice with Ramda

Basically what I need is to replace this (x) => f(x)(x) with a functional point free approach using Ramda.

Is there a way to to do it?

Upvotes: 0

Views: 226

Answers (2)

Scott Sauyet
Scott Sauyet

Reputation: 50787

You can also use chain directly, by combining with identity.

const f = (x) => (y) => `f(${x})(${y})`

console.log(R.chain(f, R.identity)('x'))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Upvotes: 0

Scott Christopher
Scott Christopher

Reputation: 6516

In Ramda, you can utilise R.unnest (often join in other languages). While this is commonly used to flatten a nested list, as pointed out in @ftor's comment, it can also act on the Chain instance for functions.

unnest :: Chain c => c (c a) -> c a

-- when used with lists
unnest :: [[a]] -> [a]

-- when used with functions
unnest :: (r -> r -> a) -> (r -> a)

Here's an example of producing a function that squares a given number by passing R.multiply to R.unnest:

const sq = R.unnest(R.multiply)

console.log(sq(5)) //=> 25
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Upvotes: 5

Related Questions