Qu

contains

Builtin index · Collections & strings

result = contains(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 = contains(collection, value)
collection Vec or Listcollection is a Vec or List.
value —value is any comparable element.
result bool
Returns a bool: membership test via structural equality.

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

s = "  Measurement Lane  "
print("[{trim(s)}]")              # [Measurement Lane]

print(upper(trim(s)))             # MEASUREMENT LANE

print(contains(s, "Lane"))        # true

print(replace(s, "Lane", "Window"))
output
[Measurement Lane]
MEASUREMENT LANE
true
  Measurement Window  

from agent-guide

tmp_svg = tmp_file() + ".svg"
figure()
polygon([0.0, 1.0, 1.0], [0.0, 0.0, 1.0], fill = "#ff0000")
savefig(tmp_svg)
print(contains(read_all_text(tmp_svg), "opacity=\"0.18\""))   # true

output
ยท savefig <tmp>.svg
  note: this figure uses screen styling and may not hold up in print -- call theme("publication") before saving for a paper (said once per session)
  note: fonts render using whatever is installed on the viewer's machine (none are embedded) -- pass embed_fonts=true (fontfamily preset only) or export .tikz for guaranteed appearance
true
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

from agent-guide

How it works

contains(collection, value) โ€” boolean membership test, over either a numeric Vec or a heterogeneous List (structural equality via values_equal, not the == operator, which doesn't compare Strs today โ€” a pre-existing, separate gap).

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