How do I output stream results to stdout or a collection in EK9?

← Streams and Pipelines · Ref: Q970

EK9 stream pipelines use Unix-style > for terminal output. The > operator sends stream results to a sink (like Stdout, a collection, or a file).

TERMINAL OPERATORS:

  > stdout          sends each item to stdout (like Unix > redirect)
  > myList          sends items into a collection
  >> myList         appends items to existing collection
  collect as Type   materializes stream into new collection

IMPORTANT: Use > like Unix redirect — NOT stdout <- cat items.

CORRECT:

  cat items | filter by isValid > stdout
  cat numbers | sort by ascending | head 5 > stdout
  cat names | map with toUpperCase | collect as List of String

WRONG:

  stdout <- cat items | filter by isValid   WRONG — do not use <- for stream output
  result = cat items | collect               WRONG — use 'collect as Type'

Bridge: Like Unix pipes: ls | grep foo | head 5 > output.txt. The > in EK9 streams works exactly like > in shell — it redirects the pipeline output to a destination.

See Q235 for full stream operations reference. See Q237 for streams vs loops comparison.

Example

defines module qa.streams.outputpatterns

  defines function

    <?-
      Check if a number is even.
    -?>
    isEven() as pure
      -> numberToCheck as Integer
      <- rtn as Boolean: numberToCheck mod 2 == 0

  defines program

    StreamOutputDemo()
      stdout <- Stdout()

      // > stdout — sends each item to standard output
      stdout.println("Even numbers:")
      cat [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        | filter by isEven
        > stdout

      // collect as — materializes into a new collection
      stdout.println("Collected evens:")
      evens <- cat [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        | filter by isEven
        | collect as List of Integer
      stdout.println($evens)

      // head N — take first N then > to sink
      stdout.println("First 3:")
      cat [10, 20, 30, 40, 50]
        | head 3
        > stdout
Other ways to ask this
  • How do stream terminals work in EK9?
  • What does > mean in EK9 streams?
  • How do I redirect stream output in EK9?
  • How do I collect stream results in EK9?

Coming from another language?

Unix shell: command | filter | head > output — same concept in EK9. Java Streams: .collect(Collectors.toList()) — EK9: collect as List of T. Python: list comprehension — EK9: cat source | filter | collect.

Keywords: stream output, sink, stdout, collect, terminal, redirect