Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 12, 2024
1 parent f00e03c commit 78c8024
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ data.group_by(|x| x % 2); // HashMap::from([(0, vec![2]), (1, vec![1, 3]
| *flat* | :heavy_check_mark: | | :heavy_check_mark: | |
| *fold* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| *fold_to* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
| *for_each* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| *frequencies* | :heavy_check_mark: | :heavy_check_mark: | | |
| *frequencies_by* | :heavy_check_mark: | :heavy_check_mark: | | |
| *group_by* | :heavy_check_mark: | | :heavy_check_mark: | |
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl<Item> Traversable<Item> for BinaryHeap<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,9 @@ impl<Key: Ord, Value> Map<Key, Value> for BTreeMap<Key, Value> {
{
superset(self.keys(), elements)
}

#[inline]
fn for_each(&self, function: impl FnMut((&Key, &Value))) {
self.iter().for_each(function)
}
}
5 changes: 5 additions & 0 deletions src/extensions/collections/btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl<Item> Traversable<Item> for BTreeSet<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl<Key, Value> Map<Key, Value> for HashMap<Key, Value> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut((&Key, &Value))) {
self.iter().for_each(function)
}

#[inline]
fn map<L, W>(&self, function: impl FnMut((&Key, &Value)) -> (L, W)) -> Self::This<L, W>
where
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ impl<Item> Traversable<Item> for HashSet<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl<Item> Traversable<Item> for LinkedList<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl<Item> Traversable<Item> for [Item] {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl<Item> Traversable<Item> for Vec<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl<Item> Traversable<Item> for VecDeque<Item> {
self.iter().fold(initial_value, function)
}

#[inline]
fn for_each(&self, function: impl FnMut(&Item)) {
self.iter().for_each(function)
}

#[inline]
fn group_fold<K, B>(
&self, to_key: impl FnMut(&Item) -> K, initial_value: B, function: impl FnMut(B, &Item) -> B,
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/// Methods have the following properties:
///
/// - Requires the collection to represent a list
/// - May consume the collection and its elements
/// - May create a new collection
/// - May consume the list and its elements
/// - May create a new list
///
pub trait List<Item> {
/// Returns the first element of this sequence, or `None` if it is empty.
Expand Down
39 changes: 37 additions & 2 deletions src/extensions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::iter::{Product, Sum};
/// Methods have the following properties:
///
/// - Requires the collection to represent a map
/// - May consume the collection and its elements
/// - May create a new collection
/// - May consume the map and its entries
/// - May create a new map
///
pub trait Map<Key, Value> {
type This<K, V>;
Expand Down Expand Up @@ -868,6 +868,41 @@ pub trait Map<Key, Value> {
{
self.into_iter().fold(initial_value, function)
}

/// Calls a closure on each entry of this map.
///
/// This is equivalent to using a [`for`] loop on the map, although
/// `break` and `continue` are not possible from a closure. It's generally
/// more idiomatic to use a `for` loop, but `for_each` may be more legible
/// when processing items at the end of longer map chains.
///
/// [`for`]: ../../book/ch03-05-control-flow.html#looping-through-a-map-with-for
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use crate::cantrip::*;
/// use std::sync::mpsc::channel;
///
/// let (tx, rx) = channel();
/// (0..3).for_each(move |x| tx.send(x).unwrap());
///
/// let v: Vec<_> = rx.iter().collect();
/// assert_eq!(v, vec![0, 1, 2]);
/// ```
///
/// For such a small example, a `for` loop may be cleaner, but `for_each`
/// might be preferable to keep a functional style with longer maps:
///
/// ```
/// (0..5).flat_map(|x| x * 100 .. x * 110)
/// .enumerate()
/// .filter(|&(i, x)| (i + x) % 3 == 0)
/// .for_each(|(i, x)| println!("{i}:{x}"));
/// ```
fn for_each(&self, function: impl FnMut((&Key, &Value)));

/// Creates a new map by retaining the values representing the intersection
/// of the original map with another map i.e., the values that are
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ops::RangeBounds;
/// Methods have the following properties:
///
/// - Requires the collection to represent a sequence
/// - May consume the collection and its elements
/// - May consume the sequence and its elements
/// - May create a new sequence
///
pub trait Sequence<Item> {
Expand Down
35 changes: 35 additions & 0 deletions src/extensions/traversable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,41 @@ pub trait Traversable<Item> {
/// ```
fn fold<B>(&self, initial_value: B, function: impl FnMut(B, &Item) -> B) -> B;

/// Calls a closure on each element of this collection.
///
/// This is equivalent to using a [`for`] loop on the collection, although
/// `break` and `continue` are not possible from a closure. It's generally
/// more idiomatic to use a `for` loop, but `for_each` may be more legible
/// when processing items at the end of longer collection chains.
///
/// [`for`]: ../../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use crate::cantrip::*;
/// use std::sync::mpsc::channel;
///
/// let (tx, rx) = channel();
/// (0..3).for_each(move |x| tx.send(x).unwrap());
///
/// let v: Vec<_> = rx.iter().collect();
/// assert_eq!(v, vec![0, 1, 2]);
/// ```
///
/// For such a small example, a `for` loop may be cleaner, but `for_each`
/// might be preferable to keep a functional style with longer collections:
///
/// ```
/// (0..5).flat_map(|x| x * 100 .. x * 110)
/// .enumerate()
/// .filter(|&(i, x)| (i + x) % 3 == 0)
/// .for_each(|(i, x)| println!("{i}:{x}"));
/// ```
fn for_each(&self, function: impl FnMut(&Item));

/// Creates `HashMap` of keys mapped and folded to values according to
/// specified discriminator and folding operation functions.
///
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
/// | *flat* | :heavy_check_mark: | | :heavy_check_mark: | |
/// | *fold* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
/// | *fold_to* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
/// | *for_each* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
/// | *frequencies* | :heavy_check_mark: | :heavy_check_mark: | | |
/// | *frequencies_by* | :heavy_check_mark: | :heavy_check_mark: | | |
/// | *group_by* | :heavy_check_mark: | | :heavy_check_mark: | |
Expand Down

0 comments on commit 78c8024

Please sign in to comment.