Filter positive numbers, double them, and sort the result.
← Streams and Pipelines · Ref: Q1134
Chain pipe-separated stages left to right:
cat items | filter by isPositive | map by doubleIt | sort > stdout
cat creates the stream, each | adds an operation, > stdout terminates. See Q235, Q1073.
Example
defines module qa.streams.filtermapsort defines function isPositive() as pure -> item as Integer <- rtn as Boolean: item > 0 doubleIt() as pure -> item as Integer <- rtn as Integer: item * 2 defines program FilterMapSortDemo() stdout <- Stdout() items <- [3, -1, 4, -5, 2, -3, 1] cat items | filter by isPositive | map by doubleIt | sort > stdout
Common mistakes
E01010 — EK9 uses 'cat source | operation | operation > output' syntax, not method chaining.
Incorrect:
items.stream().filter(isPositive).map(doubleIt).sorted()
Correct:
cat items | filter by isPositive | map by doubleIt | sort > stdout
Other ways to ask this
- I have a list with negative values and need to keep only positives, transform, then sort
- In Java I'd use stream().filter().map().sorted(). Write the EK9 pipeline
- Given mixed integers, build a pipeline: filter positives, double each, sort ascending
- Chain filter, map, and sort operations in a single stream pipeline
Coming from another language?
Java: stream().filter(x -> x > 0).map(x -> x * 2).sorted(). Python: sorted(x*2 for x in items if x > 0). EK9: cat items | filter by pred | map by fn | sort.
Keywords: chain, map, stream, filter, sort, pipeline