Chapter 28: async is a stage, built

Put async in a pipeline whose elements are functions and each one is called concurrently. Chapter 28's AsyncLookups declares one abstract function, priceLookup, four lookups that are priceLookup, and a program that sends them through async three ways. The IDE writes the function type and the four lookups' signatures; you type each answer and the pipelines. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-async-lookups.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.vol2.streams.async

  defines function

    priceLookup() as abstract
      <- rtn as String?

    lookupKeyboard() is priceLookup
      <- rtn <- "keyboard 45.0"

    lookupMonitor() is priceLookup
      <- rtn <- "monitor 220.0"

    lookupMouse() is priceLookup
      <- rtn <- "mouse 15.5"

    lookupLaptop() is priceLookup
      <- rtn <- "laptop 999.0"

  defines program

    AsyncLookups()
      stdout <- Stdout()
      lookups <- [lookupKeyboard, lookupMonitor, lookupMouse, lookupLaptop]

      stdout.println("in completion order:")
      cat lookups | async > stdout

      stdout.println("sorted after async:")
      cat lookups | async | sort > stdout

      stdout.println("async then head 2, then sorted:")
      cat lookups | async | head 2 | sort > stdout

//EOF

Step 1: priceLookup — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

priceLookup is the function's name. Set Returns to String and tick as abstract - a function type: a contract with no body. Choose Preview.

The EK9 IDE just before choosing Preview: priceLookup is the function's name

The preview shows priceLookup about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows priceLookup about to be written

Select the priceLookup() as abstract line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the priceLookup() as abstract line and, above it, type the blank line the book opens its first function with

Step 2: lookupKeyboard — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

lookupKeyboard is the function's name. Set Returns to String, type priceLookup into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: lookupKeyboard is the function's name

The preview shows lookupKeyboard about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lookupKeyboard about to be written

Select the stub, from <- rtn as String: String() down to its //AI line, and type lookupKeyboard's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String

Step 3: lookupMonitor — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

lookupMonitor is the function's name. Set Returns to String, type priceLookup into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: lookupMonitor is the function's name

The preview shows lookupMonitor about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lookupMonitor about to be written

Select the stub, from <- rtn as String: String() down to its //AI line, and type lookupMonitor's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String

Step 4: lookupMouse — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

lookupMouse is the function's name. Set Returns to String, type priceLookup into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: lookupMouse is the function's name

The preview shows lookupMouse about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lookupMouse about to be written

Select the stub, from <- rtn as String: String() down to its //AI line, and type lookupMouse's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String

Step 5: lookupLaptop — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

lookupLaptop is the function's name. Set Returns to String, type priceLookup into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: lookupLaptop is the function's name

The preview shows lookupLaptop about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lookupLaptop about to be written

Select the stub, from <- rtn as String: String() down to its //AI line, and type lookupLaptop's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String

Step 6: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

AsyncLookups is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: AsyncLookups is the program's name

The preview shows AsyncLookups about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows AsyncLookups about to be written

Select the AsyncLookups() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the AsyncLookups() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type AsyncLookups's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type AsyncLookups's body exactly as the chapter shows it

Step 7: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

The four lookups in the order they finished - which can differ from run to run - then the same four sorted, then the first two started, sorted: keyboard 45.0 and monitor 220.0. Choose Close.

The EK9 IDE just before choosing Close: The four lookups in the order they finished - which can differ from run to run - then the same four sorted, then the first two started, sorted

The result

AsyncLookups.ek9 now reads exactly as chapter 28 of EK9 in Depth shows it. async is a stage in the pipeline, not a thread you manage: the first block's order is whichever call finished first, sort puts an order back, and head 2 after async starts only two calls.

The EK9 IDE when the lesson has finished

Where this lesson comes from