Search code examples
javascriptpointfreeramda.js

Point-free style capitalize function with Ramda


While writing a capitalize function is trivial, such that:

"hello" => "Hello" "hi there" => "Hi there"

How would one write it using point-free style using Ramda JS?

https://en.wikipedia.org/wiki/Tacit_programming


Solution

  • It would be something like that:

    const capitalize = R.compose(
        R.join(''),
        R.juxt([R.compose(R.toUpper, R.head), R.tail])
    );
    

    Demo (in ramdajs.com REPL).

    And minor modification to handle null values

    const capitalize = R.compose(
        R.join(''),
        R.juxt([R.compose(R.toUpper, R.head), R.tail])
    );
    
    const capitalizeOrNull = R.ifElse(R.equals(null), R.identity, capitalize);