Write code to filter a list and collect the results into a new list.

← Streams and Pipelines · Ref: Q1035

Use a stream pipeline with filter and collect:

  longNames <- cat names
    | filter by isLongName
    | collect as List of String

The filter function must return Boolean. Items where the function returns true pass through.

  isLongName() as pure
    -> name as String
    <- rtn as Boolean: name.length() > 4

You can combine filter with other operations:

  cat names
    | filter by isLongName
    | sort
    | head 3
    | collect as List of String

This reads naturally: take names, keep long ones, sort them, take first 3, collect into a list.

For counting matches, use the collected list:

  matches <- cat items | filter by predicate | collect as List of T
  count <- matches.length()

Example

defines module qa.streams.coderfiltercollect

  defines function

    isLongName() as pure
      -> name as String
      <- rtn as Boolean?

      minLength <- 4
      rtn: name.length() > minLength

  defines program

    FilterCollectDemo()
      stdout <- Stdout()

      names <- ["Al", "Alice", "Bob", "Charlie", "Dave", "Elizabeth"]

      //Filter and collect
      longNames <- cat names
        | filter by isLongName
        | collect as List of String
      stdout.println(`Long names: ${longNames}`)

      //Filter, sort, and take first 2
      topTwo <- cat names
        | filter by isLongName
        | sort
        | head 2
        | collect as List of String
      stdout.println(`Top two: ${topTwo}`)

Common mistakes

E01010 — EK9 has no .append() or [] empty literal. Use a stream pipeline: cat source | filter by fn | collect as Type.

Incorrect:

      longNames <- []
      for name in names
        if name.length() > 4
          longNames.append(name)

Correct:

      longNames <- cat names
        | filter by isLongName
        | collect as List of String
Other ways to ask this
  • How do I filter items from a list into a new list in EK9?
  • Show the EK9 way to select matching items from a collection
  • Write a stream pipeline that filters and collects

Coming from another language?

EK9 uses 'cat source | filter by fn | collect as Type' instead of loops with conditionals.

Keywords: stream, filter, collect, coder, list, pipeline