2

How can I make the following function pointfree (using Ramda)?

const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
1
  • xprod might also be worth looking at. It outputs arrays though... R.xprod(["a"], ["b", "c", "d"]) returns [["a", "b"], ["a", "c"], ["a", "d"]] Commented May 9, 2017 at 13:16

3 Answers 3

7

Also, this should work

R.useWith(R.map, [R.concat, R.identity])

(R.identity is there for proper arity of the function, see @scott-sauyet's comment.)

see Ramda REPL

P.S: But I personally think, that compose is better – using partial application of argument is more functional approach. e.g. R.compose(R.map, R.concat)('a')(['a','b'])

Sign up to request clarification or add additional context in comments.

4 Comments

If you do use this, I would recommend a slight variant: prefixAll = R.useWith(R.map, [R.concat, R.identity]). This has the same behavior as above, but this function will properly report its arity.
@ScottSauyet Could you please elaborate more? I don't follow the point. Thanks.
Adding identity simply says that there is a second parameter, but it is not modified before being passed into map. R.useWith(R.map, [R.concat]).length; //=> 1, but R.useWith(R.map, [R.concat, R.identity]).length; //=> 2. I find it a matter of being a good citizen to have my functions properly report their arities (the number of parameters they accept.) Of course Ramda's currying muddies the water since it allows you to use these functions with varying arities, but I think the full number of parameters is the correct behavior.
@ScottSauyet I see. Thanks a lot, makes sense.
2

I entered the code into pointfree.io (well I first converted it to Haskell \prefix list -> map (prefix ++) list), and it returned:

map . (++)

So the JavaScript equivalent should be:

const prefixAll = R.compose(R.map, R.concat);

When I test this in the ramda's REPL I think I obtain the desired result.

5 Comments

Ramda functions are curried by default so I think R.curry is not needed here
@marzelin: thanks. I am actually not familiar with ramda at all, but well converting back and forward in several languages can usually do the trick.
well, it doesn't work when both arguments are given at once
@marzelin: based on what I read, I think it is due to the fact that compose does some currying itself, but for some reason fails to find out that there are two arguments.
all arguments given to compose are passed to the first function, but in this case the second argument needs to be passed to the result of the composition. useWith seems to be the right tool for this job.
0

IMO the simplest way is using comprehensions which are achieved in this case with R.lift lifting the R.concat function.

const prefixAll = R.lift(R.concat);
prefixAll(['hi-'], ['you', 'there']); // => ["hi-you", "hi-there"]

Note that the prefix must be passed as a single-element array. This function is automatically curried by Ramda for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.