Skip to content

59 Chaining block calls ​

produce { 7 } supplies a callback that creates a value; map { item * 2 } transforms it. Each block's expected function type guides its parameter and result.

norm
class Box<T> {
  T value

  Box<R> map<R>(R transform(T item)) {
    return Box<R>(value: transform(value))
  }
}

Box<T> produce<T>(T work()) {
  return Box<T>(value: work())
}

main() {
  Box<Integer> answer = produce { 7 } map { item * 2 }
  printLine(answer.value)
}

Expected output:

text
14

Try it: Change the first block to return 9 and trace the result.

Precise rules: Reference.

Norm 0.25