
In this article, we’ll look at a simple Verse function that searches through an array and finds the first element that matches a condition. The condition is defined by a custom function you pass in. This is similar to how find or filter works in other programming languages.
(Input:[]t where t:type).FindUsingFunction<public>(FindFunc(Ele:t)<transacts> : logic)<decides><transacts>: tuple(t, int)=
{
var FoundElement :?tuple(t, int)= false;
# For each Element
var Index : int = 0;
for(Element : Input)
{
# Check if the function returns true on the element.
if(FindFunc(Element) = true)
{
# Set the Option.
if(not FoundElement?)
{
set FoundElement = option{Element, Index};
}
}
# Increment the counter by one
set Index += 1;
}
#Return the option.
return FoundElement?;
}
Breaking down the function signature
(Input:[]t where t:type).FindUsingFunction<public>(FindFunc(Ele:t)<transacts> : logic)<decides><transacts>: tuple(t, int) =
- (Input:[]t where t:type)The function is defined as an extension method on arrays ([]t).
- t:type means the array can hold any type, as long as all elements are of the same type t.
- Input is the array we’ll search in.
- .FindUsingFunction<public>The name of the function is FindUsingFunction.
- <public> makes it available outside the current module.
- <public> makes it available outside the current module.
- FindFunc(Ele:t)<transacts> : logic
This is the parameter — a function called FindFunc that takes one element of type t and returns a logic (true/false).
- <transacts> means the function can have side effects (like updating state).
- <transacts> means the function can have side effects (like updating state).
- <decides><transacts>
These are modifiers:
- <decides> means the function’s return value depends only on its inputs.
- <transacts> again means it can interact with state.
- : tuple(t, int)
The return type is a tuple containing:
- the element we found (t), and
- its index (int) in the array.
