Search code examples
javascriptfunctional-programmingpointfreeramda.js

Pointfree Join Array to String, by key in object, in Ramda


Can this be done pointfree?

var joinByKey = R.curry(function(key, model){
    return R.assoc(key, R.join(',' ,R.prop(key, model)), model);
});

var input = { a: ['1', '2', '3'] };
var result = joinByKey("a", input); // {"a": "1,2,3"}

Solution

  • Yes, it can be done like this :

    const joinByKey = key => R.over(
        R.lensProp(key),
        R.join(',')
    );
    
    const input = { a: ['1', '2', '3'] };
    const result = joinByKey("a")(input); // {"a": "1,2,3"}
    

    If you want to use it uncurrified :

    const joinByKey = R.curry((key, model) => R.over(
      R.lensProp(key),
      R.join(',')
    )(model));
    
    var input = { a: ['1', '2', '3'] };
    joinByKey("a", input); // {"a": "1,2,3"}
    

    The second one works both currified or uncurrified.

    I find it more readable than your version, in contrary of what @naomik says ...