How do I output stream results to stdout in EK9?

← Streams and Pipelines · Ref: Q896

EK9 stream pipelines use '>' for output redirection — just like Unix shell pipes. The '>' sends stream results to a target like stdout.

BASIC PATTERN

Like Unix: ls | grep foo > output.txt
EK9: cat items | filter by predicate > stdout

The '>' is the stream terminal — it sends each item to the target.

STREAM TO STDOUT

  cat items | filter by isPositive > stdout

This is like Unix: cat file | grep pattern > /dev/stdout

STREAM TO COLLECTION

  filtered <- cat items | filter by isPositive | collect as List of Integer

The 'collect' terminal gathers items into a new collection.

STREAM PIPELINE OPERATORS

- cat: source items (like Unix cat)
- filter: keep items matching predicate (like Unix grep)
- map: transform items (like Unix awk/sed)
- sort: order items
- head: take first N items (like Unix head)
- tail: take last N items (like Unix tail)
- skip: skip first N items
- tee: fork output (like Unix tee)
- group: group items (like Unix uniq)
- join: merge groups
- flatten: expand nested structures
- > : redirect output (like Unix >)
- collect: gather into collection

COMMON MISTAKE

Do NOT use <- to assign stream output directly:

  stdout <- cat items   WRONG — this declares stdout
  cat items > stdout     CORRECT — this redirects output

See Q140 for stream basics. See Q141 for filter and map.

Example

defines module qa.streams.redirect

  defines function

    isPositive() as pure
      -> candidate as Integer
      <- rtn as Boolean: candidate > 0

    doubleIt() as pure
      -> candidate as Integer
      <- rtn as Integer: candidate * 2

  defines program

    StreamOutputDemo()
      stdout <- Stdout()

      numbers <- [5, -3, 8, -1, 12, -7, 3]

      // Like Unix: cat numbers | grep positive > stdout
      stdout.println("Positive numbers:")
      cat numbers | filter by isPositive > stdout

      // Like Unix: cat numbers | grep positive | awk '{print $1*2}' > stdout
      stdout.println("Doubled positives:")
      cat numbers | filter by isPositive | map by doubleIt > stdout

      // Like Unix: cat numbers | sort > stdout
      stdout.println("Sorted:")
      cat numbers | sort > stdout

      // Like Unix: cat numbers | head -3 > stdout
      stdout.println("First 3:")
      cat numbers | head 3 > stdout

      // Tail — like Unix: cat numbers | tail -3 > stdout
      stdout.println("Last 3:")
      cat numbers | tail 3 > stdout
Other ways to ask this
  • How do EK9 stream pipelines output data?
  • What is the > operator in EK9 streams?
  • How do I redirect stream output in EK9?
  • How do streams print to console in EK9?

Coming from another language?

Unix: ls | grep foo | sort > output.txt — EK9 streams follow the same model. Java: stream().filter().map().forEach() — method chaining instead of pipes. Python: list comprehensions or generator expressions. Rust: iter().filter().map().collect() — similar to Java. EK9: cat items | filter by predicate | sort > stdout — Unix pipe syntax with typed pipeline stages.

Keywords: stream, pipe, map, Unix, redirect, stdout, output, pipeline, collect, filter, cat