Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to concatenate iterators?

Tags:

iterator

rust

let vec = iter::repeat("don't satisfy condition 1") // iterator such as next() always "don't " satisfy condition 1"
    .take_while(|_| {
        satisfycondition1.satisfy() // true is condition 1 is satisfied else false
    })
    .collect();

This code creates a vector of n elements with n equal to the number of times condition 1 is not respected.

I would like now to create a vector of n + m elements with n equal to the number of times that condition 1 is not respected and m the number of times that condition 2 is not respected.

The code should resemble something like this:

let vec = iter::repeat("dont't satisfy condition 1")
    .take_while(|_| {
        satisfycondition1.satisfy() 
    })
    .union(
        iter::repeat("has satisfed condition 1 but not 2 yet")
        .take_while(|_| {
            satisfycondition2.satisfy() 
        })
    )
    .collect();

I know I could create two vectors and then concatenate them but it's less efficient.

You can use this code to understand what does repeat:

use  std::iter;

fn main() {
    let mut c = 0;
    let z: Vec<_> = iter::repeat("dont't satisfy condition 1")
        .take_while(|_| {
            c = c + 1;
            let rep = if c < 5 { true } else { false };
            rep
        })
        .collect();
    println!("------{:?}", z);
}
like image 707
Pierre-olivier Gendraud Avatar asked Apr 10 '19 22:04

Pierre-olivier Gendraud


People also ask

How do I combine two iterators in Java?

If you are given two Iterators in Java, how are you supposed to merge them into one list if both iterators are sorted? The Java's iterator has two important methods you can use: hasNext() and next(). The hasNext() returns a boolean telling if this iterator reaches the end.

Are iterators faster than for loops Python?

That being said, the iterators from itertools are often significantly faster than regular iteration from a standard Python for loop. This is certainly something to keep in mind, as it will likely impress a hiring manager when you are able to write a function to solve such a problem that is faster than everyone else's.

Which function is an example of an infinite iterator?

Iterator in Python is any python type that can be used with a ' for in loop '. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite.


1 Answers

It seems like std::iter::chain is what you're looking for.

use std::iter;

fn main() {
    let mut c = 0;
    let mut d = 5;
    let z: Vec<_> = iter::repeat("don't satisfy condition 1")
        .take_while(|_| {
            c = c + 1;
            let rep = if c < 5 { true } else { false };
            rep
            // this block can be simplified to
            // c += 1;
            // c < 5
            // Clippy warns about this
        })
        .chain(
            iter::repeat("satisfy condition 1 but not 2").take_while(|_| {
                d -= 1;
                d > 2
            }),
        )
        .collect();
    println!("------{:?}", z);
}

(playground link)

I can't comment on the semantics of your code, though. If you're trying to see which elements of an iterator "satisfy condition 1 but not 2", this wouldn't be how you do it. You would use std::iter::filter twice (once with condition 1 and once with not condition 2) to achieve that.

like image 87
SCappella Avatar answered Oct 11 '22 05:10

SCappella