# Title: Functions that remember
# About: Closures: little functions that keep their own memory, like a step counter.

fn make_counter(label) {
  let count = 0
  return fn() {
    count = count + 1
    say(label + ": " + str(count))
  }
}

let cups = make_counter("Cups of tea today")
let pages = make_counter("Pages read today")

cups()
cups()
pages()
cups()

# A function can also be kept in a 'let' and passed around like any value.
let shout = fn(text) {
  return text + "!!!"
}

fn twice(f, value) {
  return f(f(value))
}

say(twice(shout, "Hello there"))
