Match | MATCH

Syntax

MATCH( [value], [cases], [default] )

Parameters

value
Any data may be used as the input.

cases
A MAP of case-result pairs. Each case may be a value used to test for equality with the VALUE. A case may also be a BLOCK that matches if it's result returns a truthy value. Each result may be a value which is returned if it's corresponding case matches. A result may also be a BLOCK that is called with the VALUE if it's corresponding case matches with the BLOCK's returned value used as the final result value.

default
May be any value. It is returned if no case matches the VALUE.

Return Value

TRUE or FALSE

Description

Matches a value against an ordered set of cases and returns the value of the matching case.

Examples

MATCH("OK", [
"ERROR" => "An error occurred",
"OK" => "Everything is fine"
], "Not Found")
# returns "Everything is fine"
MATCH("OTHER", [
"ERROR" => "An error occurred",
"OK" => "Everything is fine"
], "Not Found")
# returns "Not Found"
MATCH(3, [
0 => "Is zero",
{|x| $x > 15} => "Larger than 15",
{|x| $x < 5} => "Smaller than 5",
], "Between 5 and 15")
# returns "Smaller than 5"
MATCH(5, [
0 => "ZERO",
{|x| => true} => {|x| => TEXT_CONCAT("DOUBLED: ", $x * 2)}
])
# returns "DOUBLED: 10"