Background
Background

Searching Arrays in Verse with a Custom Function

FindUsingFunction returns the first element accepted by a custom test, plus its zero-based index.

Put imports and functions at module level, outside the device class; call Example() from existing device code in the same module.

Helper

using { /Verse.org/Verse }

(Input:[]t where t:type).FindUsingFunction<public>(
    FindFunc(Ele:t)<transacts>:logic
)<decides><transacts>:tuple(t, int) =
    var FoundElement:?tuple(t, int) = false
    var Index:int = 0

    for (Element : Input):
        if (FindFunc(Element) = true):
            if (not FoundElement?):
                set FoundElement = option{(Element, Index)}

        set Index += 1

    FoundElement?

The test returns logic: true accepts, false skips. It runs for every element, even after the first match; later matches never replace it.

<transacts> supports rollback: prefer read-only tests with compatible effects. Callback changes may be undone by a failed condition or enclosing failure context. No suspending or irreversible calls.

Example

<decides> means the helper can fail. Call it with [] in if; no match or an empty array takes else.

IsOverTen(Value:int)<transacts>:logic =
    logic{Value > 10}

Example():void =
    Values := array{3, 12, 18, 12}

    if (Match := Values.FindUsingFunction[IsOverTen]):
        Print("Found {Match(0)} at index {Match(1)}")
    else:
        Print("No matching value")

Expected from the code: (12, 1). In the returned tuple, Match(0) is the value and Match(1) its index.

This sample has not been compiled or run in UEFN.

Move over the bright dot and follow the line. P pauses. Escape closes the game.

Sigil Trace

Move over the sigil before time runs out.

Score0000
Spells00
Best on this browser0000
I · KINDLE SPELL 01
START

Trace the Sigil with your mouse

Start at the glowing dot and follow the line. No click needed.

or press Enter
Move your mouse onto the bright dot. No click needed.