class_name ReduceIterator extends Iterator

var iterator: Iterator
var reducer: Callable
var accumulator: Variant

func _init(iterator: Iterator, reducer: Callable, initial_value: Variant) -> void:
    self.iterator = iterator
    self.reducer = reducer
    accumulator = initial_value

func next() -> Option:
    var value = iterator.next()
    if value.is_none(): return value

    if accumulator == null:
        accumulator = value
    else:
        accumulator = reducer.call(accumulator, value)

    return Option.some(accumulator)
