Chapter 25: what pure promises, built

Chapter 25's PurityRules shows the pragmatic side of 'pure'. The rule is one-way: a pure function may call only pure code, but code that is not pure may call pure code freely - and a pure function may still print, because output is not the state purity protects. The IDE writes the four signatures with pure ticked where the book has it; you type the work. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-purity-rules.

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.purity

  defines function

    label() as pure
      -> amount as Float
      <- rtn as String: `GBP ${amount}`

    receipt() as pure
      -> amount as Float
      <- rtn as String: `Total ${label(amount)}`

    announce() as pure
      -> text as String
      Stdout().println(text)

    audit()
      -> text as String
      announce(`audit: ${text}`)

  defines program

    PurityRules()
      stdout <- Stdout()
      stdout.println(receipt(12.5))
      announce("a pure function may print")
      audit("a function that is not pure may call a pure one")

      first <- GUID()
      second <- GUID()
      stdout.println(`two calls to a pure constructor, same answer: ${first == second}`)

Step 1: label — a pure function

Choose Code > New Construct > Function…

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

label is the function's name. Add one parameter, amount of type Float, set Returns to String and tick pure. Choose Preview.

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

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

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

Select the label() as pure 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 label() as pure line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as String? down to its //AI line, and type label'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? down to its //AI line, and type label's own return and work, as the book writes them

Step 2: receipt — a pure function

Choose Code > New Construct > Function…

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

receipt is the function's name. Add one parameter, amount of type Float, set Returns to String and tick pure. Choose Preview.

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

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

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

Select the stub, from <- rtn as String? down to its //AI line, and type receipt'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? down to its //AI line, and type receipt's own return and work, as the book writes them

Step 3: announce — a pure function

Choose Code > New Construct > Function…

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

announce is the function's name. Add one parameter, text of type String and tick pure. Choose Preview.

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

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

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

Select the stub, from require text? down to its //AI line, and type announce's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require text? down to its //AI line, and type announce's own return and work, as the book writes them

Step 4: audit — a function

Choose Code > New Construct > Function…

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

audit is the function's name. Add one parameter, text of type String. Choose Preview.

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

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

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

Select the stub, from require text? down to its //AI line, and type audit's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require text? down to its //AI line, and type audit's own return and work, as the book writes them

Step 5: The program

Choose Code > New Construct > Program…

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

PurityRules is the program's name. Choose Preview.

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

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

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

Select the PurityRules() 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 PurityRules() 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 PurityRules'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 PurityRules's body exactly as the chapter shows it

Step 6: 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.

Total GBP 12.5, the two announcements, then false: the two GUIDs differ. Choose Close.

The EK9 IDE just before choosing Close: Total GBP 12.5, the two announcements, then false

The result

PurityRules.ek9 now reads exactly as chapter 25 of EK9 in Depth shows it. announce is pure and prints; audit is not pure and calls it; and GUID() is a pure constructor that still gives two different values - pure here means no side effects on your state, not 'the same answer every time'.

The EK9 IDE when the lesson has finished

Where this lesson comes from