Qu

indexof

Builtin index · Collections & strings

result = indexof(collection, value)

Input: collection, value · Output: result

What it is

collection is a Vec or List; value is any comparable element.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = indexof(collection, value)
collection Vec or Listcollection is a Vec or List.
value —value is any comparable element.
number —0-based index of the first match
Returns a number (0-based index of the first match) or Nothing if absent (composes with ??: indexof(xs, v) ?? -1).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

ys = [3, 1, 4, 1, 5]
print(contains(ys, 4))     # true

print(indexof(ys, 4))      # 2

print(cumsum(ys))          # [3, 4, 8, 9, 14]

print(numel(ys))           # 5

output
true
2
[3, 4, 8, 9, 14]
5

from collections-strings

How it works

indexof(collection, value) — index of the first element equal to value, or Value::Nothing if absent. Nothing (not -1) is Qu's existing "absent" marker (see how ??/ Expr::Coalesce already treats it specially) — composes directly with that operator: indexof(xs, v) ?? -1 for scripts that do want a sentinel index. index_of is accepted as well: it is the spelling most people reach for, and having only one of the two is a guessing game with no payoff.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.