迭代连续折叠结果的惯用和功能方法是什么?

问题描述 投票:0回答:1

我有一个序列(列表,迭代器)a0, a1, a2, ...,我使用函数f折叠。我想有一台发电机给我

a0, f(a0, a1), f(f(a0, a1), a2), ...

这类似于Mathematica中的FoldList。有fold_list功能吗?我找不到任何东西。

functional-programming rust fold
1个回答
2
投票

我会说最接近的抽象是Iterator::scan。它有点强大,因为它具有内部可变状态(即可以为得到的迭代器产生不同的值)并且可以提前中止。

您可以像这样使用它来构建迭代器扩展特征:

Playground

pub trait FoldListExt: Iterator {
    fn fold_list<'a, St: 'a, F: 'a>(self, initial_state: St, f: F) -> Box<Iterator<Item = St> + 'a>
    where
        St: Clone,
        F: FnMut(St, Self::Item) -> St,
        Self: 'a;
}

impl<I: Iterator> FoldListExt for I {
    fn fold_list<'a, St: 'a, F: 'a>(
        self,
        initial_state: St,
        mut f: F,
    ) -> Box<Iterator<Item = St> + 'a>
    where
        St: Clone,
        F: FnMut(St, Self::Item) -> St,
        Self: 'a,
    {
        Box::new(self.scan(Some(initial_state), move |state, item| {
            let old_state = state.take().unwrap();
            *state = Some(f(old_state.clone(), item));
            Some(old_state)
        }))
    }
}

pub fn main() {
    println!(
        "{:?}",
        (0..16)
            .into_iter()
            .fold_list(0, |a, b| a + b)
            .collect::<Vec<_>>()
    );
}

我使用Option<St>作为内部可变状态以避免另一个clone()调用。

您可以使用此代替:

Box::new(self.scan(initial_state, move |state, item| {
    let old_state = state.clone();
    *state = f(old_state.clone(), item);
    Some(old_state)
}))
© www.soinside.com 2019 - 2024. All rights reserved.