Plan a search feature in EK9. What constructs should I use?

← Getting Started · Ref: Q1036

Search feature plan using EK9 constructs:

1. Define the search criteria as a record:

   SearchCriteria with keyword as String and maxResults as Integer
   Records are immutable data carriers — perfect for search parameters.

2. Define the result type as a record:

   SearchResult with title as String and score as Float
   Immutable results can be safely passed between components.

3. Write a pure matching function:

   matchesCriteria() as pure
     Takes an item and criteria, returns Boolean.
   Pure functions are testable without mocking.

4. Use a stream pipeline to search:

   results <- cat allItems
     | filter by matchesCriteria
     | sort by score
     | head maxResults
     | collect as List of SearchResult
   The pipeline expresses the search algorithm declaratively.

5. Use guard expressions for the caller:

   if results <- performSearch(criteria)
     Display results
   The guard handles the case where no results are found.

Key EK9 choices:
- Records for data (immutable, automatic operators)
- Pure functions for logic (testable, composable)
- Streams for collection processing (declarative, no loops)
- Guards for result handling (null-safe, concise)

Example

defines module qa.gettingstarted.plansearch

  defines function

    matchesKeyword() as pure
      -> title as String
      <- rtn as Boolean: title.length() > 0

    formatTitle() as pure
      -> title as String
      <- rtn as String: `Found: ${title}`

  defines program

    SearchDemo()
      stdout <- Stdout()

      titles <- ["EK9 Streams Guide", "EK9 Guard Patterns", "Getting Started"]

      //Stream pipeline for search
      cat titles
        | filter by matchesKeyword
        | map with formatTitle
        > stdout
Other ways to ask this
  • How should I structure a search feature in EK9?
  • What EK9 patterns work best for implementing search?
  • Plan the architecture for a search module in EK9

Coming from another language?

EK9 planning uses records for data, pure functions for logic, streams for collection processing, and guards for result handling.

Keywords: record, plan, stream, guard, search, architecture