How do I use enums with stream pipelines in EK9?

← Enumerations · Ref: Q224

EK9 enumerations are first-class stream sources. Use 'cat EnumType' to feed all declared values into a pipeline. No .values() conversion, no array wrapping.

BASIC STREAMING

Stream all enum values to output: 'cat Season > stdout'. Or collect into a list: 'cat Season | collect as List of Season'.

FILTER AND TRANSFORM

Use filter and map in pipelines:

  cat Season | filter by isWarm | collect as List of Season

Where isWarm is a function taking Season and returning Boolean.

FOR-IN ITERATION

'for season in Season' iterates all declared values in order. This is the simplest form when you need to process each value.

COLLECTION FROM ENUM

Collect enums into lists for further processing:

  allSeasons <- cat Season | collect as List of Season
  length allSeasons returns the count of declared values.

COMBINING WITH FUNCTIONS

Pipe enum values through functions:

  cat Season | map with describeFunc > stdout

This transforms each enum value through a function and outputs the results.

See Q59 for function pipelines. See Q99 for basic enumeration. See Q122 for collect as. See Q219 for auto-generated operators.

Example

defines module qa.enums.streams

  defines type

    Rank
      Two
      Three
      Four
      Five
      Six
      Seven
      Eight
      Nine
      Ten
      Jack
      Queen
      King
      Ace

    Suit
      Hearts
      Diamonds
      Clubs
      Spades

  defines function

    isFaceCard() as pure
      -> r as Rank
      <- face <- Boolean()
      face: r > Rank.Ten

    rankLabel() as pure
      -> r as Rank
      <- label <- String()
      label: $r

  defines program

    EnumStreamsDemo()
      stdout <- Stdout()

      // === BASIC STREAMING ===

      stdout.println("All suits:")
      cat Suit > stdout

      // === COLLECT INTO LIST ===

      allRanks <- cat Rank | collect as List of Rank
      stdout.println(`Total ranks: ${length allRanks}`)

      // === FILTER WITH FUNCTION ===

      faceCards <- cat Rank | filter by isFaceCard | collect as List of Rank
      stdout.println(`Face cards: ${length faceCards}`)

      // === MAP WITH FUNCTION ===

      stdout.println("Rank labels:")
      cat Rank | map with rankLabel > stdout

      // === FOR-IN ITERATION ===

      for suit in Suit
        stdout.println(`Suit: ${suit}`)

      // === STREAM SUITS AND COUNT ===

      suitList <- cat Suit | collect as List of Suit
      stdout.println(`Total suits: ${length suitList}`)

Common mistakes

E50060 — Stdout does not have a display() method. The correct method is println(). Calling a non-existent method triggers E50060 — method not resolved. See ek9 -h E50060 for details.

Incorrect:

stdout.display(`Face cards: ${length faceCards}`)

Correct:

stdout.println(`Face cards: ${length faceCards}`)
Other ways to ask this
  • Can I stream over enum values in EK9?
  • How do I iterate and transform enum values with pipelines?
  • How does cat work with EK9 enumerations?

Coming from another language?

Java: Arrays.stream(Season.values()) verbose, requires importing Arrays, values() returns mutable array copy. Python: list(Season) simpler but no pipeline, must chain with map/filter manually. Rust: needs strum::IntoEnumIterator external crate for iteration, .iter() not built-in. Go: must manually create slice of constants, no built-in iteration. Kotlin: entries.forEach or entries.filter, entries property since 1.9. C#: Enum.GetValues() returns Array, needs casting. EK9: 'cat Season' directly in pipeline, first-class stream source, zero conversion.

Keywords: stream, functional, enumeration, map, iterate, collect, pipeline, list, filter, enum, cat