How can I compose iterator adapter without boilerplate each time like in node.js Readable.compose

I want to be able to compose iterators that even support genawaiter crate and avoid the boilerplate for every iterator adapter

I want to be able to do this (I know I can use the map operator but this is simplified example for when I need to have some state in the iterators):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>use genawaiter::{rc::gen, yield_};
use std::iter::Iterator;
fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> {
return gen!({
for item in iter {
yield_!(Some(item));
}
})
.into_iter();
}
fn run() {
let input: Vec<i32> = vec![1, 2, 3, 4, 5];
let output: Vec<Option<&i32>> = input
.iter()
.compose(wrap_with_option)
.collect();
let expected = vec![
Some(&1),
Some(&2),
Some(&3),
Some(&4),
Some(&5)
];
assert_eq!(output, expected);
}
</code>
<code>use genawaiter::{rc::gen, yield_}; use std::iter::Iterator; fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> { return gen!({ for item in iter { yield_!(Some(item)); } }) .into_iter(); } fn run() { let input: Vec<i32> = vec![1, 2, 3, 4, 5]; let output: Vec<Option<&i32>> = input .iter() .compose(wrap_with_option) .collect(); let expected = vec![ Some(&1), Some(&2), Some(&3), Some(&4), Some(&5) ]; assert_eq!(output, expected); } </code>
use genawaiter::{rc::gen, yield_};
use std::iter::Iterator;

fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> {
    return gen!({
            for item in iter {
                yield_!(Some(item));
            }
        })
    .into_iter();
}
fn run() {

    let input: Vec<i32> = vec![1, 2, 3, 4, 5];
    let output: Vec<Option<&i32>> = input
        .iter()
        .compose(wrap_with_option)
        .collect();

    let expected = vec![
        Some(&1),
        Some(&2),
        Some(&3),
        Some(&4),
        Some(&5)
    ];
    assert_eq!(output, expected);
}

You can do this:

it will work with any function that gets an iterator and returns an iterator,

It just implement the compose function on the global iterator that get a function and execute it on the current iterator (self) and return the function result – new iterator

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>use std::iter::Iterator;
pub trait ComposeByIterator: Iterator {
fn compose<F, OutputItem, OutputIterator>(self,f: F) -> OutputIterator
where
Self: Sized,
OutputIterator: Iterator<Item = OutputItem>,
F: Fn(Self) -> OutputIterator,
{
f(self)
}
}
impl<I: Iterator> ComposeByIterator for I {}
#[cfg(test)]
mod tests {
use genawaiter::{rc::gen, yield_};
use super::*;
#[test]
fn should_allow_changing_the_input_type() {
let input = vec![1, 2, 3, 4, 5];
let output: Vec<String> = input
.iter()
.compose(|iter| iter.map(|x| x.to_string()))
.collect();
let expected = vec![
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string(),
"5".to_string(),
];
assert_eq!(output, expected);
}
#[test]
fn should_allow_multiple_compositions() {
let input = vec![1, 2, 3, 4, 5];
let output: Vec<String> = input
.iter()
.compose(|iter| iter.map(|x| x.to_string()))
.compose(|iter| iter.map(|x| x + "!"))
.collect();
let expected = vec![
"1!".to_string(),
"2!".to_string(),
"3!".to_string(),
"4!".to_string(),
"5!".to_string(),
];
assert_eq!(output, expected);
}
#[test]
fn should_allow_multiple_compositions_that_change_type() {
#[derive(Debug, PartialEq)]
struct MyStruct {
value: String,
}
let input = vec![1, 2, 3, 4, 5];
let output: Vec<MyStruct> = input
.iter()
.compose(|iter| iter.map(|x| x.to_string()))
.compose(|iter| iter.map(|x| MyStruct { value: x }))
.collect();
let expected = vec![
MyStruct {
value: "1".to_string(),
},
MyStruct {
value: "2".to_string(),
},
MyStruct {
value: "3".to_string(),
},
MyStruct {
value: "4".to_string(),
},
MyStruct {
value: "5".to_string(),
},
];
assert_eq!(output, expected);
}
#[test]
fn should_support_gen() {
fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> {
return gen!({
for item in iter {
yield_!(Some(item));
}
})
.into_iter();
}
let input: Vec<i32> = vec![1, 2, 3, 4, 5];
let output: Vec<Option<&i32>> = input
.iter()
.compose(wrap_with_option)
.collect();
let expected = vec![
Some(&1),
Some(&2),
Some(&3),
Some(&4),
Some(&5)
];
assert_eq!(output, expected);
}
}
</code>
<code>use std::iter::Iterator; pub trait ComposeByIterator: Iterator { fn compose<F, OutputItem, OutputIterator>(self,f: F) -> OutputIterator where Self: Sized, OutputIterator: Iterator<Item = OutputItem>, F: Fn(Self) -> OutputIterator, { f(self) } } impl<I: Iterator> ComposeByIterator for I {} #[cfg(test)] mod tests { use genawaiter::{rc::gen, yield_}; use super::*; #[test] fn should_allow_changing_the_input_type() { let input = vec![1, 2, 3, 4, 5]; let output: Vec<String> = input .iter() .compose(|iter| iter.map(|x| x.to_string())) .collect(); let expected = vec![ "1".to_string(), "2".to_string(), "3".to_string(), "4".to_string(), "5".to_string(), ]; assert_eq!(output, expected); } #[test] fn should_allow_multiple_compositions() { let input = vec![1, 2, 3, 4, 5]; let output: Vec<String> = input .iter() .compose(|iter| iter.map(|x| x.to_string())) .compose(|iter| iter.map(|x| x + "!")) .collect(); let expected = vec![ "1!".to_string(), "2!".to_string(), "3!".to_string(), "4!".to_string(), "5!".to_string(), ]; assert_eq!(output, expected); } #[test] fn should_allow_multiple_compositions_that_change_type() { #[derive(Debug, PartialEq)] struct MyStruct { value: String, } let input = vec![1, 2, 3, 4, 5]; let output: Vec<MyStruct> = input .iter() .compose(|iter| iter.map(|x| x.to_string())) .compose(|iter| iter.map(|x| MyStruct { value: x })) .collect(); let expected = vec![ MyStruct { value: "1".to_string(), }, MyStruct { value: "2".to_string(), }, MyStruct { value: "3".to_string(), }, MyStruct { value: "4".to_string(), }, MyStruct { value: "5".to_string(), }, ]; assert_eq!(output, expected); } #[test] fn should_support_gen() { fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> { return gen!({ for item in iter { yield_!(Some(item)); } }) .into_iter(); } let input: Vec<i32> = vec![1, 2, 3, 4, 5]; let output: Vec<Option<&i32>> = input .iter() .compose(wrap_with_option) .collect(); let expected = vec![ Some(&1), Some(&2), Some(&3), Some(&4), Some(&5) ]; assert_eq!(output, expected); } } </code>
use std::iter::Iterator;

pub trait ComposeByIterator: Iterator {
    fn compose<F, OutputItem, OutputIterator>(self,f: F) -> OutputIterator
    where
        Self: Sized,
        OutputIterator: Iterator<Item = OutputItem>,
        F: Fn(Self) -> OutputIterator,
    {
        f(self)
    }
}
impl<I: Iterator> ComposeByIterator for I {}

#[cfg(test)]
mod tests {
    use genawaiter::{rc::gen, yield_};

    use super::*;

    #[test]
    fn should_allow_changing_the_input_type() {
        let input = vec![1, 2, 3, 4, 5];
        let output: Vec<String> = input
            .iter()
            .compose(|iter| iter.map(|x| x.to_string()))
            .collect();

        let expected = vec![
            "1".to_string(),
            "2".to_string(),
            "3".to_string(),
            "4".to_string(),
            "5".to_string(),
        ];
        assert_eq!(output, expected);
    }

    #[test]
    fn should_allow_multiple_compositions() {
        let input = vec![1, 2, 3, 4, 5];
        let output: Vec<String> = input
            .iter()
            .compose(|iter| iter.map(|x| x.to_string()))
            .compose(|iter| iter.map(|x| x + "!"))
            .collect();

        let expected = vec![
            "1!".to_string(),
            "2!".to_string(),
            "3!".to_string(),
            "4!".to_string(),
            "5!".to_string(),
        ];
        assert_eq!(output, expected);
    }

    #[test]
    fn should_allow_multiple_compositions_that_change_type() {
        #[derive(Debug, PartialEq)]
        struct MyStruct {
            value: String,
        }
        let input = vec![1, 2, 3, 4, 5];
        let output: Vec<MyStruct> = input
            .iter()
            .compose(|iter| iter.map(|x| x.to_string()))
            .compose(|iter| iter.map(|x| MyStruct { value: x }))
            .collect();

        let expected = vec![
            MyStruct {
                value: "1".to_string(),
            },
            MyStruct {
                value: "2".to_string(),
            },
            MyStruct {
                value: "3".to_string(),
            },
            MyStruct {
                value: "4".to_string(),
            },
            MyStruct {
                value: "5".to_string(),
            },
        ];
        assert_eq!(output, expected);
    }

    #[test]
    fn should_support_gen() {
        fn wrap_with_option<Input, I: Iterator<Item = Input>>(iter: I) -> impl Iterator<Item = Option<Input>> {
            return gen!({
                for item in iter {
                    yield_!(Some(item));
                }
            })
            .into_iter();
        }

        let input: Vec<i32> = vec![1, 2, 3, 4, 5];
        let output: Vec<Option<&i32>> = input
            .iter()
            .compose(wrap_with_option)
            .collect();

        let expected = vec![
            Some(&1),
            Some(&2),
            Some(&3),
            Some(&4),
            Some(&5)
        ];
        assert_eq!(output, expected);
        
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật