Get the lower of two sensor readings where one sensor might be offline.

← Operators and Expressions · Ref: Q1212

The <? coalescing minimum operator returns the lesser of two Integer values, handling unset gracefully.

  lowerReading() as pure
    -> left as Integer, right as Integer
    <- rtn as Integer: left <? right

If both readings are set, <? returns the lower one. If one is unset (sensor offline), it returns the active sensor's reading. If both are unset, the result is unset.

This is a COMPARISON operator — it picks the smaller value. Do not confuse it with :=? (guarded assignment), which assigns only if the target is unset. See Q1226 for a side-by-side contrast.

See Q1092 for min/max coalescing overview. See Q1227 for all four coalescing operators.

Example

defines module qa.operators.coalescemin.integer

  defines function

    lowerReading() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left <? right

  defines program

    CoalesceMinIntegerDemo()
      stdout <- Stdout()

      // === BOTH SENSORS ONLINE — returns the lower reading ===

      sensorA <- 42
      sensorB <- 37
      lowestReading <- lowerReading(sensorA, sensorB)
      stdout.println(`Lowest reading: ${lowestReading}`)

      // === ONE SENSOR OFFLINE — returns the active one ===

      activeSensor <- 55
      offlineSensor <- Integer()
      onlyReading <- lowerReading(activeSensor, offlineSensor)
      stdout.println(`Active reading: ${onlyReading}`)

      // === BOTH SENSORS OFFLINE — result is unset ===

      downA <- Integer()
      downB <- Integer()
      noReading <- lowerReading(downA, downB)
      if noReading?
        stdout.println(`Reading: ${noReading}`)
      else
        stdout.println("No sensor data available")

Common mistakes

E50001 — EK9 has no 'Math.min()'; use the '<?' coalescing-minimum operator, which also handles unset values. See ek9 -h E50001 for details.

Incorrect:

      <- rtn as Integer: Math.min(left, right)

Correct:

      <- rtn as Integer: left <? right
Other ways to ask this
  • How do I find the minimum of two Integer values when one might be unset?
  • A monitoring system has two sensors but one could be offline — pick the lower reading.
  • In Java I'd need Optional and compareTo for nullable integers. What does EK9 use?
  • Migrating from C# where I use Math.Min with nullable checks — what is the EK9 pattern?

Coming from another language?

Java: Optional.ofNullable(a).flatMap(x -> Optional.ofNullable(b).map(y -> Math.min(x, y))).orElse(a != null ? a : b). Python: min(x for x in [a, b] if x is not None). EK9: left <? right — one operator handles all cases.

Keywords: coalescing, <?, offline, lower, minimum, integer, reading, sensor