ramda-adjunct
ramda-adjunct copied to clipboard
nthOr
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.
Legitimate
@Undistraction: Can we set milestone to v2.8.0 for this issue ?