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 plain data carriers (public fields, copied by value) — a good fit for search parameters.

2. Define the result type as a record:

   SearchResult with title as String and score as Float
   Give it 'default operator' to get comparison, $ and the other operators generated.

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 byScore
     | head maxResults
     | collect as List of SearchResult

The pipeline expresses the search algorithm declaratively.

5. Use a guard, then test for emptiness, in the caller:

   if results <- performSearch(criteria)
     if results is not empty
       showResults(results)

The guard only skips an absent or unset result. A collection is SET as soon as it exists, even when empty, so 'no results found' is the 'results is not empty' test inside the guard (or 'length results > 0').

Key EK9 choices:
- Records for data (public fields; operators from 'default operator')
- Pure functions for logic (testable, composable)
- Streams for collection processing (declarative, no loops)
- Guards to skip an absent or unset result; an emptiness test for 'nothing found'

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: stream, plan, record, architecture, search, guard