(we won't be finding a Find
or catching a tryCatch
, though)
This is part two of a multi-part series of implementing some interesting R functions. Part one can be found here.
The task:
You are to implement R's match
function in as few bytes as possible.
Input:
x
, a possibly empty list/array of integerstable
, a possibly empty list/array of integersnomatch
, a single integer valueincomparables
, a possibly empty list/array of integers
Output:
- a single array/list of integers
O
of equal length tox
, where each valueO[i]
represents either:- The index
j
of the first value intable
wheretable[j]==x[i]
nomatch
, indicating that no value intable
is equal tox[i]
OR thatx[i]
is in the list ofincomparables
.
- The index
Test Cases
All in the form x, table, nomatch, incomparables -> output
outputs
[], [1,2,3], 0, [5] -> []
[1, 2, 3], [], 0, [5] -> [0, 0, 0]
[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]
[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]
More test cases can be generated as needed.
Additional rules:
- R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.
- If you chosen language has a builtin that does this, please also implement your own solution.
- Explanations are appreciated.
This is code-golf, so shortest solution in bytes wins!
4
is inincomparables
, so it can't be matched. If your language can't support negative numbers, then it's fine to require non-negative numbers, but state that assumption in your submission. \$\endgroup\$make
. \$\endgroup\$