How do I filter a list and print the results to stdout in EK9?

← Streams and Pipelines · Ref: Q977

Use a stream pipeline: cat source | filter by predicate > stdout

The > operator sends stream results to stdout, like Unix pipe redirect.

PATTERN:

  cat collection | filter by checkFunction > stdout

The pipeline reads items from the collection, keeps only those passing the filter, and sends each to stdout.

Example

defines module qa.streams.filtertostdout

  defines function

    isHighScore() as pure
      -> score as Integer
      <- rtn as Boolean?

      threshold <- 80
      rtn: score > threshold

  defines program

    FilterToStdoutDemo()
      stdout <- Stdout()

      scores <- [45, 92, 78, 95, 63, 88, 71, 99]

      stdout.println("High scores:")
      cat scores | filter by isHighScore > stdout

Common mistakes

E50001 — EK9 streams use Unix pipe syntax: 'cat source | filter by fn > stdout'. There is no .stream() method or Java-style method chaining. See ek9 -h stream.

Incorrect:

      scores.stream().filter(isHighScore).forEach(stdout::println)

Correct:

      cat scores | filter by isHighScore > stdout
Other ways to ask this
  • Show me a stream pipeline that filters and outputs to stdout
  • How do I use cat, filter, and > stdout together?
  • What is the basic stream-to-stdout pattern in EK9?

Coming from another language?

Java Streams: list.stream().filter(x -> x > 5).forEach(System.out::println). EK9: cat scores | filter by isHigh > stdout — shorter, no lambda syntax.

Keywords: pipeline, cat, filter, stdout, stream