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 REPLACES the contents of a List, Set, Dict, PriorityQueue or JSON array with the stream's items (Unix > clobbers; a PriorityQueue keeps its comparator and size limit; a JSON object is replaced by the streamed objects merged into a fresh one) >> myList ADDS the stream's items to what the collection already holds (Unix >> appends; a JSON object is merged into) > total REPLACES a scalar (Integer, Float, String ...) with what the stream produces; >> total adds to it collect as Type materializes stream into new collection > file.output() CLOBBERS the file; >> file.output() APPENDS to it (println a header first, then use >>)
A > into a collection is all or nothing: the pipeline fills a new collection and replaces the target only when it completes, so an exception leaves the target as it was.
IMPORTANT: Use > like Unix redirect — NOT stdout <- cat items.
CORRECT:
cat items | filter by isValid > stdout cat numbers | sort by ascending | head 5 > stdout upper <- 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
#!ek9 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?
- How do I output stream results to stdout in EK9?
- How do EK9 stream pipelines output data?
- What is the > operator in EK9 streams?
- How do streams print to console 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: Unix, cat, collect, filter, map, output, pipe, pipeline, redirect, sink, stdout, stream, stream output, terminal