ramda-adjunct icon indicating copy to clipboard operation
ramda-adjunct copied to clipboard

nthOr

Open Undistraction opened this issue 6 years ago • 2 comments

Is your feature request related to a problem? Please describe.

Ramda offers a R.propOr, but nothing for dealing with indexes. This function will return the nth element of the given list/string or default provided value. If n is negative the element at index length + n is returned.

Describe the solution you'd like

const nthOr = curry((defaultValue, n, a) => defaultTo(defaultValue, nth(n, a)))

const a = [1,2,3]

const nthOrX = nthOr('X')

nthOrX(0, a) // => 1
nthOrX(1, a) // => 2
nthOrX(2, a) // => 2
nthOrX(3, a) // => X

Describe alternatives you've considered

--

Additional context

Useful in situations where mapping values from one list to another where the lists might be of different lengths.

REPL

Undistraction avatar Apr 05 '18 09:04 Undistraction

Legitimate

char0n avatar Apr 11 '18 19:04 char0n

@Undistraction: Can we set milestone to v2.8.0 for this issue ?

guillaumearm avatar Apr 25 '18 09:04 guillaumearm