5 Strictness and laziness

This chapter covers

  • Strict vs. non-strict functions
  • Implementing a lazy list data type
  • Memoizing streams to avoid recomputation
  • Inspecting streams to visualize and test
  • Separating program description from evaluation
  • Infinite streams and corecursion

Kotlin, like most modern programming languages, uses strict evaluation by default. That is, it allows only functions whose parameters must be entirely evaluated before they may be called. In all our examples so far, we have focused on this evaluation strategy, also know as eager or greedy evaluation. In fact, this is what we have been using while deriving data types such as List, Option, and Either. We will look at a more formal definition of strictness later, but what ...

Get Functional Programming in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.