Operators on values, lists, strings and ranges

The complete source of operators_basics.ek9, compiled by the EK9 compiler. Shown on operators.html.

#!ek9
defines module site.operators.basics

  defines function

    addition()
      stdout <- Stdout()

      result <- 6 + 8

      stdout.println(result)

    listAddition()
      stdout <- Stdout()

      //Simple list of string values (note this is shorthand for a List and is not an array)
      aList <- [ "A", "B", "C" ]
      //aList remains unmodified as does "D"
      list2 <- aList + "D"
      //Now modify list2 - but note the different operator
      list2 += "E"

      //Create another list to be used.
      endList <- [ "X", "Y", "Z" ]
      //Same syntax and semantics, but different parameters
      finalList <- list2 + endList
      //But the result finalList is obvious as ["A", "B", "C", "D", "E", "X", "Y", "Z"]

      stdout.println(aList)
      stdout.println(finalList)

    comparisons()
      stdout <- Stdout()

      one <- 1
      two <- 2
      three <- 3
      alsoThree <- 3
      six <- 6
      eight <- 8
      lessThan <- one < two //results in true
      greaterThan <- one > two //results in false
      greaterOrEqual <- three >= two //results in true
      lessOrEqual <- three <= two //results in false
      notEqual <- three <> alsoThree //results in false
      alsoNotEqual <- three != alsoThree //results in false
      equal <- three == alsoThree //results in true
      inRange <- three in two ... eight //results in true
      notInRange <- six not in two ... eight //results in false

      stdout.println(`${lessThan} ${greaterThan} ${greaterOrEqual} ${lessOrEqual} ${notEqual}`)
      stdout.println(`${alsoNotEqual} ${equal} ${inRange} ${notInRange}`)

    stringOperators()
      stdout <- Stdout()

      steve <- "Steve"
      capitalTe <- "Te"
      lowerTe <- "te"
      stevenPattern <- /[S|s]te(?:ven?|phen)/
      noMatch <- steve contains capitalTe //results in false
      hasMatch <- steve contains lowerTe //results in true
      matchesLeft <- steve matches stevenPattern //results in true
      matchesRight <- stevenPattern matches steve //also results in true

      stdout.println(`${noMatch} ${hasMatch} ${matchesLeft} ${matchesRight}`)

  defines program

    Basics()
      addition()
      listAddition()
      comparisons()
      stringOperators()

Download operators_basics.ek9

Output

14
["A", "B", "C"]
["A", "B", "C", "D", "E", "X", "Y", "Z"]
true false true false false
false true true false
false true true true