The \$\text{argwhere}\$ function takes a list of values and a predicate/boolean function as arguments and returns a list of indices where the predicate function returns true in the input list. For example,
argwhere([1, 2, 3, -5, 5], x -> x > 2)
would produce an output of [2, 4]
because those are the (0-indexed) indices whose values are greater than two.
Challenge
Implement the \$\text{argwhere}\$ function in your language of choice.
Format
For the purposes of this challenge, we will deal with lists of integers. You must accept a list of integers and a black box function and return a list of integers in any reasonable format. You may assume the input list will never be empty. Your output may be either 0-indexed or 1-indexed — please specify which.
Rules
- Builtins are allowed, but please consider adding a less trivial answer so we can see how \$\text{argwhere}\$ might be implemented in your language.
- Explaining your answer(s) is encouraged!
- Standard loopholes are forbidden.
- This is code-golf, so the code with the fewest bytes (in each language) wins.
Why argwhere?
argfoo is a naming convention where you don't want the elements themselves — you want their indices or some other quality. \$\text{argmax}\$, \$\text{argmin}\$, and \$\text{argsort}\$ are examples of this. Read more about it here. (Also, because my favorite programming language comes with this function and it's called arg-where
. 🤫)
Test cases
0-indexed
Input | Output |
---|---|
|
|
1-indexed
Input | Output |
---|---|
|
|
FindAll(Map(<array>, <expression>), 1)
. \$\endgroup\$W
\$\endgroup\$