Filter a product list to items over 50 USD and sort by price ascending.
← Streams and Pipelines · Ref: Q1203
Define a pure predicate function for the filter and a named constant for the threshold. Stream pipeline: cat | filter by | sort by | collect.
NAMED THRESHOLD
threshold <- 50.00#USD named constant avoids magic literal
PREDICATE FUNCTION
isExpensive() as pure -> item as Product <- rtn as Boolean: item.price > threshold
COMPARATOR FUNCTION
byPrice() as pure -> left as Product right as Product <- rtn as Integer: left.price <=> right.price
PIPELINE
expensive <- cat products | filter by isExpensive | sort by byPrice | collect as List of Product
The filter stage applies the predicate. 'sort by byPrice' orders by price ascending (Webcam 65.00, Headset 79.50, Monitor 299.99). A bare '| sort' would use the record's default <=>, which compares the properties in declaration order - productName first - so it would sort by NAME, not price. Named constants make the threshold explicit and changeable.
See Q1134 for filter-map-sort. See Q1189 for money comparison. See Q1184 for named constants.
Example
defines module qa.streams.filterexpensiveproducts defines record Product productName as String: String() price as Money: Money() Product() as pure -> theName as String thePrice as Money this.productName :=: theName this.price :=: thePrice default operator defines function isExpensive() as pure -> item as Product <- rtn as Boolean? threshold <- 50.00#USD rtn: item.price > threshold byPrice() as pure -> left as Product right as Product <- rtn as Integer: left.price <=> right.price defines program FilterExpensiveProductsDemo() stdout <- Stdout() // === CREATE PRODUCT LIST === products <- List() of Product products += Product("Mouse", 19.99#USD) products += Product("Keyboard", 49.99#USD) products += Product("Monitor", 299.99#USD) products += Product("Headset", 79.50#USD) products += Product("Cable", 9.99#USD) products += Product("Webcam", 65.00#USD) // === FILTER AND SORT BY PRICE === // Prints Webcam (65.00), Headset (79.50), Monitor (299.99) expensive <- cat products | filter by isExpensive | sort by byPrice | collect as List of Product stdout.println("Expensive products (sorted):") cat expensive > stdout // === DIRECT OUTPUT === // Same order, written straight to stdout stdout.println("Expensive to stdout:") cat products | filter by isExpensive | sort by byPrice > stdout
Common mistakes
E50060 — EK9 List has no .stream() method; stream it with 'cat products' and pipe it through named functions. See ek9 -h E50060 for details.
Incorrect:
expensive <- products.stream()
Correct:
expensive <- cat products | filter by isExpensive | sort by byPrice | collect as List of Product
Other ways to ask this
- Use a stream pipeline to filter products by price and sort the result.
- I need to find all expensive items from a list and display them in order.
- In Java I'd use stream().filter(p -> p.getPrice() > 50).sorted(). What is the EK9 equivalent?
- Given products with Money prices, filter by a named threshold and sort.
Coming from another language?
Java: stream().filter(p -> p.getPrice().compareTo(threshold) > 0).sorted(). Python: [p for p in products if p.price > 50]. Rust: iter().filter(|p| p.price > 50).sorted(). Go: manual loop with append. EK9: cat products | filter by isExpensive | sort by byPrice | collect — pipe syntax with a named predicate and a named comparator.
Keywords: product, sort, expensive, stream, named constant, filter, pipeline, money, comparator, predicate, threshold