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 | 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

PIPELINE

  expensive <- cat products | filter by isExpensive | sort | collect as List of Product

The filter stage applies the predicate. The sort stage uses <=> from default operator. 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

  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 ===

      expensive <- cat products | filter by isExpensive | sort | collect as List of Product
      stdout.println("Expensive products (sorted):")
      cat expensive > stdout

      // === DIRECT OUTPUT ===

      stdout.println("Expensive to stdout:")
      cat products | filter by isExpensive | sort > stdout

Common mistakes

E50060 — EK9 uses stream pipelines with named predicate functions, not lambda expressions or method calls. See ek9 -h E50060 for details.

Incorrect:

expensive <- products.stream().filter(p -> p.price > 50).collect()

Correct:

expensive <- cat products | filter by isExpensive | sort | 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 | collect — pipe syntax with named predicate.

Keywords: threshold, predicate, sort, filter, expensive, named constant, stream, product, pipeline, money