Functional programming
Recipe | Crates | Categories |
---|---|---|
Compose iterators |
| [Use `lens`][ex-rust-patterns-lens] | [![lens_rs][c-lens_rs-badge]][c-lens_rs] | [![cat-rust-patterns][cat-rust-patterns-badge]][cat-rust-patterns] |
Compose iterators
itertools
includes extra iterator adaptors, functions and macros.
fn main() { use itertools::Itertools; use itertools::assert_equal; use itertools::chain; // Assert that two iterables produce equal sequences assert_equal("hello world".split(' '), "hello world".split(' ')); // Chain let mut result: Vec<i32> = Vec::new(); for element in chain(&[1, 2, 3], &[4]) { result.push(*element); } assert_eq!(result, vec![1, 2, 3, 4]); // Cloned assert_eq!(itertools::cloned(b"abc").next(), Some(b'a')); // Deduplicate let data = vec![1., 1., 2., 3., 3., 2., 2.]; itertools::assert_equal(data.into_iter().dedup(), vec![1., 2., 3., 2.]); // `into_group_map` let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)]; let lookup = data.into_iter().into_group_map(); assert_eq!(lookup[&0], vec![10, 20]); assert_eq!(lookup.get(&1), None); assert_eq!(lookup[&2], vec![12, 42]); assert_eq!(lookup[&3], vec![13, 33]) }
[functional_programming: cover the most useful functions (P1)](https://github.com/john-cd/rust_howto/issues/467)
Use
functional_programming: review - lens is not used that often (P1)
Use lens
This Rust library provides support for lenses, which are a mechanism in functional programming for focusing on a part of a complex data structure.
fn main() { todo!(); }