These are chat archives for ramda/ramda

19th
Feb 2018
Pedr Browne
@Undistraction
Feb 19 2018 12:56

Hello all.

I'm wondering what the best way to handle a public-facing api that needs to accept implicit undefined values: api() and explicit undefined values: api(undefined) is? It appears that Ramda treats the two differently when it comes to curried functions:

const f1 = curry(a => toString(a))

f1(undefined) // 'undefined'
f1() // returns the curried function f1

REPL
Given that I have no control over the input, I need to handle both cases elegantly. It appears that a non-tacit function call irons this out:

const f1 = curry(a => toString(a))

const f2 = v => f1(v)

f2(undefined) // 'undefined'
f2() // returns the curried function f1

REPL
But wondering if there is a better approach?

matrixbot
@matrixbot
Feb 19 2018 13:51
Siilwyn Pedr Browne (Gitter): so you want f2 to return undefined too?
Brad Compton (he/him)
@Bradcomp
Feb 19 2018 16:48
@Undistraction Why do you need to curry the function at all?
Pedr Browne
@Undistraction
Feb 19 2018 17:34
@matrixbot , @Siilwyn This code above is a big simplification. I need f1 to be curried so it can be configured (it has other args), and it composes a load of other functionality that processes the value passed in. The problem is that given that it has to be curried , I need it to handle both implicit and explicit undefined values, which Ramda treats differently from one-another. The only way I have found to do this is to explicitly pass the value into the next function in the chain (rather than tacitly), as this makes the value explicit.
Brad Compton (he/him)
@Bradcomp
Feb 19 2018 17:41

That's one way to do it, but whether it's the best way really depends on the specifics. As a general rule, currying and optional parameters aren't a great match. A common pattern I use is to create a higher order function that will take in the configuration and return the function that I will use.

const f = (cfg1, cfg2) => (optionalParam) => doStuffWithAllOfIt...

That way you can take the required parameters, and the function that takes an optional parameter doesn't need to be curried.

Pedr Browne
@Undistraction
Feb 19 2018 19:32
@Bradcomp Thanks. Yes I think that is the way to go.
Lucas Constantino Silva
@lucasconstantino
Feb 19 2018 23:36
Hello! I'm having a hard time figuring if and how is it possible to have "sub-operations" on a single pipe with Ramda... say if I have an array of numbers, and want to return an array of the percentage each number is equivalent in the total, I'll have to first compute the sum of all numbers, keep this value at hand, and then use it to find the percentage each value corresponds to
I'm not really used to functional languages, so how does this kind of thing gets addressed in a functional way?