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], A]
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)