Write a complete EK9 program that reads a list of scores, filters passing ones, and prints the results.

← Functions and Methods · Ref: Q1063

Here is a complete, compilable EK9 program:

defines module scores.processor

  defines function
    isPassing() as pure
      -> score as Integer
      <- rtn as Boolean?
      passingMark <- 60
      rtn: score >= passingMark
    formatScore() as pure
      -> score as Integer
      <- rtn as String: `Score: ${score} - PASS`
  defines program
    ScoreProcessor()
      stdout <- Stdout()
      scores <- [85, 42, 91, 67, 73, 55, 88, 96, 61, 79]
      cat scores
        | filter by isPassing
        | map with formatScore
        > stdout
      passing <- cat scores
        | filter by isPassing
        | collect as List of Integer
      stdout.println(`Passing count: ${passing.length()}`)

Key elements:
- Module declaration at the top
- Functions in a 'defines function' section
- Program in a 'defines program' section
- Pure functions for pipeline use
- Stream pipeline for processing
- collect as List for gathering results

See Q1061 for reusable filter functions. See Q1062 for transform functions.

Example

defines module scores.processor

  defines function

    isPassing() as pure
      -> score as Integer
      <- rtn as Boolean?

      passingMark <- 60
      rtn: score >= passingMark

    formatScore() as pure
      -> score as Integer
      <- rtn as String: `Score: ${score} - PASS`

  defines program

    ScoreProcessor()
      stdout <- Stdout()

      scores <- [85, 42, 91, 67, 73, 55, 88, 96, 61, 79]

      //Pipeline: filter passing, format, print
      cat scores
        | filter by isPassing
        | map with formatScore
        > stdout

      //Collect passing scores
      passing <- cat scores
        | filter by isPassing
        | collect as List of Integer
      stdout.println(`Passing count: ${passing.length()}`)

Common mistakes

E01010 — EK9 requires 'defines function' section header, type annotations on parameters, and <- for return declaration. No 'def' or 'return'.

Incorrect:

  def isPassing(score):
    return score >= 60

Correct:

  defines function

    isPassing() as pure
      -> score as Integer
      <- rtn as Boolean?
Other ways to ask this
  • Write a full EK9 program from scratch
  • Show me a complete working program with functions and a pipeline
  • Code a program that processes a list using filter and output

Coming from another language?

Java: class with main method, imports, System.out. Python: def + if __name__. EK9: module + defines function + defines program.

Keywords: complete, filter, write, code, pipeline, coder, program