Pattern matching

The is and in blocks are a quick shortcut for conditionals.

is block#

if-is is equivalent to a == comparison. It checks if the value is equal to any of the provided options.

x = 4
if x
  is 1 2 3 4
    print x  # Prints 4
  else
    print "Does not match"

in block#

if-in applies a membership operation. Its behavior varies according to the data type:

Data typeBehavior
ArraysChecks if the value belongs to the set of array elements
StringsChecks if it’s a substring of the string
RangesChecks if it’s within the numeric range

Ranges#

x = 5
if x
  in 0..10
    print x  # Prints 5 because it's within the range
  else
    print "Out of range"

Arrays#

x = 3
if x
  in [1 2 3 4 5]
    print x  # Prints 3 because it belongs to the array
  else
    print "Not in the array"

Strings#

x = "world"
if x
  in "Hello world"
    print x  # Prints "world" because it's part of the string
  else
    print "Not part of the string"

Mix of blocks#

You can combine multiple is and in blocks in the same if block:

x = 5
if x
  is 1 2 3
    print "Is 1, 2 or 3"
  in 0..10
    print "Is in the range 0-10"
  in [20 30 40]
    print "Is 20, 30 or 40"
  else
    print "Does not match any option"

Value validation#

Useful to verify if a value is valid, avoiding cases like none, infinity or empty strings:

x = ""
if x
  is -infi infi nan none ""
     panic "The value is invalid"
  else
     print x * 2