Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

rust - Can you create a function that takes another function and a parameter and returns a lazy stream of nested function calls?

In Clojure, I use a function called iterate that:

Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects

Is there something similar in Rust?

For example, I have this function that I'd like to pass to a function with a number and then iterate over a stream/range/vector until I find Some(1):

fn coltz(n: u64) -> Option<u64> {
    match n % 2 {
        0 => Some(n / 2), 
        _ => Some(3 * n + 1)
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can repeatedly call a closure using std::iter::repeat_with():

repeat_with(move || {
    let result = x;
    x = f(x);
    result
})

The return value is an iterator over the successive return values of the closure.

We use move to move x into the closure, as the current state of our iteration. Inside the closure we update x with f(x) and return the old value (so in the first iteration we return the original x).

Here is a complete working example:

use std::iter::repeat_with;

fn collatz(n: u64) -> u64 {
    match n % 2 {
        0 => n / 2,
        _ => 3 * n + 1,
    }
}

fn iterate<F, X>(f: F, mut x: X) -> impl Iterator<Item = X>
where
    F: Fn(X) -> X,
    X: Copy,
{
    repeat_with(move || {
        let result = x;
        x = f(x);
        result
    })
}

fn main() {
    for i in iterate(collatz, 12).take_while(|&x| x != 1) {
        println!("{}", i);
    }
}

Playground


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...