Files
pplus-web/mojo/stress/optional-lookup.mojo
T
2026-10-05 03:03:38 +00:00

23 lines
729 B
Mojo

# probe: a function returning Optional, tested and unwrapped
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: ada at 0
# expect-stdout: cy at 2
# expect-stdout: dee missing
# expect-stdout: 0 None
def index_of(xs: List[String], x: String) -> Optional[Int]:
for i in range(len(xs)):
if xs[i] == x:
return i
return None
def main():
var names: List[String] = ["ada", "bo", "cy"]
for q in ["ada", "cy", "dee"]:
var r = index_of(names, q)
if r:
print(q, "at", r.value())
else:
print(q, "missing")
print(index_of(names, "ada"), index_of(names, "zed"))