1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Callable
def reduce(function, accumulator, items):
if len(items) == 0:
return accumulator
return reduce(function, function(accumulator, items[0]), items[1:])
def reduce(function, accumulator, items):
match items:
case []:
return accumulator
case [head, *tail]:
return reduce(function, function(accumulator, head), tail)
type Reducer = Callable[[A], B]
def reduce[A, B](function: Reducer, accumulator: A, items: list[B]) -> A:
match items:
case []:
return accumulator
case [head, *tail]:
return reduce(function, function(accumulator, head), tail)