Skip to content

54 Capturing values ​

The returned lambda keeps access to factor after multiplier finishes. Each call supplies a new number while using the captured factor.

norm
Function<Integer(Integer)> multiplier(Integer factor) {
  return (Integer number) {
    number * factor
  }
}

main() {
  Function<Integer(Integer)> triple = multiplier(factor: 3)
  printLine(triple(4))
  printLine(triple(7))
}

Expected output:

text
12
21

Try it: Create a second multiplier with factor 5.

Precise rules: Reference.

Norm 0.25