mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
34 lines
1.0 KiB
Rust
34 lines
1.0 KiB
Rust
pub(crate) use _functools::make_module;
|
|
|
|
#[pymodule]
|
|
mod _functools {
|
|
use crate::{function::OptionalArg, protocol::PyIter, PyObjectRef, PyResult, VirtualMachine};
|
|
|
|
#[pyfunction]
|
|
fn reduce(
|
|
function: PyObjectRef,
|
|
iterator: PyIter,
|
|
start_value: OptionalArg<PyObjectRef>,
|
|
vm: &VirtualMachine,
|
|
) -> PyResult {
|
|
let mut iter = iterator.iter_without_hint(vm)?;
|
|
let start_value = if let OptionalArg::Present(val) = start_value {
|
|
val
|
|
} else {
|
|
iter.next().transpose()?.ok_or_else(|| {
|
|
let exc_type = vm.ctx.exceptions.type_error.clone();
|
|
vm.new_exception_msg(
|
|
exc_type,
|
|
"reduce() of empty sequence with no initial value".to_owned(),
|
|
)
|
|
})?
|
|
};
|
|
|
|
let mut accumulator = start_value;
|
|
for next_obj in iter {
|
|
accumulator = vm.invoke(&function, (accumulator, next_obj?))?
|
|
}
|
|
Ok(accumulator)
|
|
}
|
|
}
|