15 Functions as Values
A function in Zeno is a value, like a number or a list. It can be given a name, passed to another function, kept in a list or a dict, and given back by a function. This chapter is about what that makes possible.
15.1 Passing a function
A function’s name, without brackets after it, is the function itself. Passing it hands the function over, to be called later:
function twice(f: function(int) -> int, x: int) -> int {
return f(f(x))
}
function add_one(n: int) -> int {
return n + 1
}
print(twice(add_one, 5))
print(add_one)7
<function add_one>
twice calls whatever function it was given, twice. You
have already seen this with map, filter and
reduce in Lists.
15.2 The type of a function
The type of a function is written as its heading with the names and the body taken out:
| Function | Its type |
|---|---|
function add_one(n: int) -> int |
function(int) -> int |
function add(a: int, b: int) -> int |
function(int, int) -> int |
function report(text: str) |
function(str) |
function now() -> int |
function() -> int |
A function fits a type when it can be called with those arguments and gives back what the type says. One that does not fit is refused at the call, with the reason:
function twice(f: function(int) -> int, x: int) -> int {
return f(f(x))
}
function shout(text: str) -> str {
return text.upper()
}
print(twice(shout, 1))error: `f` of `twice` should be function(int) -> int, but the parameter `text` of this function is str
--> main.zeno:7:13
|
7 | print(twice(shout, 1))
| ^
1 error found. Nothing was run.
There is nothing new to learn: the same keyword, the same brackets,
the same -> as a heading. A form such as
(int) -> int, as in TypeScript and Swift, would be
shorter, but it gives brackets a second meaning in types. (Decision
1.11a.)
15.3 A function with no name
A function that is used only once can be written where it is used, with the name left out:
let double = function(n: int) -> int { return n * 2 }
print(double(21))
let readings = [15.0, 25.0, 30.0]
print(readings.map(function(c) { return c * 1.8 + 32.0 }))
print(readings.filter(function(c) { return c > 20.0 }))42
[59.0, 77.0, 86.0]
[25.0, 30.0]
When such a function is passed where a function type is expected, as
with map here, its types can be left out: Zeno works them
out from the type it is given.
It is exactly a function declaration with the name left out, so there
is only one way to write a function. Python’s
lambda n: n * 2 is a second, smaller syntax that cannot
hold statements, and JavaScript’s n => n * 2 would need
the arrow that Zeno left out of match. (Decision 3.6a.)
For the commonest case, making a new list from an old one, a
comprehension is shorter still:
[c * 1.8 + 32.0 for c in readings].
15.4 Functions in lists and dicts
Because a function is a value, it can be kept anywhere a value can. A dict from names to functions is a simple way to run a command by name:
function add(a: int, b: int) -> int {
return a + b
}
function multiply(a: int, b: int) -> int {
return a * b
}
let operations: dict[str, function(int, int) -> int] = [:]
operations["add"] = add
operations["times"] = multiply
operations["minus"] = function(a: int, b: int) -> int { return a - b }
for name in ["add", "times", "minus"] {
print(name, operations[name](6, 3))
}add 9
times 18
minus 3
15.5 Functions that make functions
A function can give back a function. The function it gives back can use the variables of the function that made it, even after that function has returned:
function scaler(factor: float) -> function(float) -> float {
return function(x: float) -> float { return x * factor }
}
let to_millivolts = scaler(1000.0)
let halve = scaler(0.5)
print(to_millivolts(3.3), halve(10.0))3300.0 5.0
to_millivolts and halve are two functions
made by one scaler, each with its own factor.
A function together with the variables it keeps is called a
closure.
It keeps the variable, not a copy
A closure keeps the variable itself. If it changes the variable, the change lasts until it is called again:
function counter() -> function() -> int {
var count = 0
return function() -> int {
count += 1
return count
}
}
let next_id = counter()
print(next_id(), next_id(), next_id())
let other = counter()
print(other())1 2 3
1
Each call of counter makes a new count, so
other starts again from 1.
This is what the code says when it is read: there is one
count. If the inner function had a copy,
next_id() would give 1 every time, and nothing would say
why. (Decision 3.6c.)
Each turn of a loop has its own name
The name in a for loop is new on each turn, so a
function made inside a loop keeps the value of that turn:
var handlers: list[function()] = []
for at in range(3) {
handlers.append(function() { print("handler", at) })
}
for handle in handlers {
handle()
}handler 0
handler 1
handler 2
In JavaScript with var, and in Python, all three would
print the last value, 2: a famous puzzle that Zeno does not have.
What it can see is fixed where it is written
A function can see the variables declared above it, in the blocks around it, and never one declared further down, even one with the same name:
let level = 1
function outer() -> int {
let read = function() -> int { return level }
let level = 2
return read()
}
print(outer())1
read was written where the only level in
sight was the one at the top, so that is the one it uses, and
outer gives 1.
The meaning of a name should be settled by where it is written, not by what happens to be declared later. This rule, called lexical scope, is the one nearly every modern language follows, and it lets the compiler decide once which variables a function must take with it. (Decisions 3.6c and 3.6d.)
15.6 Calling something that is not a function
Only a function can be called, and the checker knows which names hold one:
let speed = 5
speed()error: int cannot be called
--> main.zeno:2:1
|
2 | speed()
| ^
1 error found. Nothing was run.
A function is a value: its name without brackets is the function. Its
type is written function(int, str) -> bool. A function
with no name is written function(n) { return n * 2 }.
Functions can be passed, stored in lists and dicts, and given back by
other functions. A function given back keeps the variables around it,
the variables themselves and not copies, as they were in sight where it
was written.
Write a function
apply_allthat takesfs: list[function(int) -> int]andx: int, and gives each function’s answer forxin alist[int].Write a function
composethat takes two functions of typefunction(int) -> int,fandg, and gives a function of that type which doesgand thenf.Make a counter that starts from a number and counts up by a step, both given to the function that makes it.
Build a small calculator: read lines such as
add 2 3withinput(), split them, and look the word up in a dict of functions. What should happen with a word that is not in the dict?