diff --git a/src/extensions/collectible.rs b/src/extensions/collectible.rs index 6ea67e5..25132fa 100644 --- a/src/extensions/collectible.rs +++ b/src/extensions/collectible.rs @@ -68,7 +68,7 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.delete(&2), vec![1, 2, 3]); /// @@ -105,7 +105,7 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.delete_multi(&vec![1, 2]), vec![2, 3]); /// @@ -146,14 +146,14 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.combinations(0), vec![vec![]]); /// assert_eq!(a.combinations(1), vec![vec![1], vec![2], vec![3]]); /// assert_eq!(a.combinations(2), vec![vec![1, 2], vec![1, 3], vec![2, 3]]); /// assert_eq!(a.combinations(3), vec![vec![1, 2, 3]]); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(a.combinations(4), empty_result); /// assert_eq!(e.combinations(2), empty_result); /// ``` @@ -792,7 +792,7 @@ pub trait Collectible: IntoIterator { /// use std::collections::HashSet; /// /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// let intersection = a.intersect(&vec![4, 2, 2, 3, 4]); /// @@ -910,7 +910,6 @@ pub trait Collectible: IntoIterator { self.into_iter().map(function).collect() } - // FIXME - fix the failing test case /// Creates a new collection containing the n largest elements of /// this collection in descending order. /// @@ -919,10 +918,13 @@ pub trait Collectible: IntoIterator { /// ``` /// use cantrip::*; /// - /// let a = vec![5, 1, 3, 2, 4]; - /// let e: Vec = Vec::new(); + /// # let source = vec![2, 1, 3]; + /// let a = vec![2, 1, 3]; + /// let e: Vec = vec![]; /// - /// // assert_eq!(largest(3), vec![5, 4, 3]); + /// assert_eq!(a.largest(2), vec![3, 2]); + /// # let a = source.clone(); + /// assert_eq!(a.largest(4), vec![3, 2, 1]); /// assert_eq!(e.largest(3), vec![]); /// ``` fn largest(self, n: usize) -> Self @@ -933,11 +935,14 @@ pub trait Collectible: IntoIterator { let mut iterator = self.into_iter(); let mut heap = iterator.by_ref().map(|x| Reverse(x)).take(n).collect::>(); for item in iterator { - if heap.peek().unwrap().0 < item { - *heap.peek_mut().unwrap() = Reverse(item); + if let Some(mut top) = heap.peek_mut() { + if item > top.0 { + *top = Reverse(item); + } } } - heap.into_iter().rev().map(|x| x.0).collect() + let result: Vec = unfold(|| heap.pop()).map(|x| x.0).collect(); + result.into_iter().rev().collect() } /// Creates two new collections from this collection by applying @@ -1047,7 +1052,7 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.powerset(), vec![ /// vec![], @@ -1082,7 +1087,7 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![2, 3, 4]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// let product = a.product(); /// @@ -1141,7 +1146,6 @@ pub trait Collectible: IntoIterator { iterator.next().map(|result| iterator.fold(result, function)) } - // FIXME - fix the failing test case /// Creates a new collection containing the n smallest elements of /// this collection in descending order. /// @@ -1150,10 +1154,13 @@ pub trait Collectible: IntoIterator { /// ``` /// use cantrip::*; /// - /// let a = vec![5, 1, 3, 2, 4]; - /// let e: Vec = Vec::new(); + /// # let source = vec![2, 3, 1]; + /// let a = vec![2, 3, 1]; + /// let e: Vec = vec![]; /// - /// // assert_eq!(smallest(3), vec![1, 2, 3]); + /// assert_eq!(a.smallest(2), vec![1, 2]); + /// # let a = source.clone(); + /// assert_eq!(a.smallest(4), vec![1, 2, 3]); /// assert_eq!(e.smallest(3), vec![]); /// ``` fn smallest(self, n: usize) -> Self @@ -1164,11 +1171,14 @@ pub trait Collectible: IntoIterator { let mut iterator = self.into_iter(); let mut heap = iterator.by_ref().take(n).collect::>(); for item in iterator { - if *heap.peek().unwrap() > item { - *heap.peek_mut().unwrap() = item; + if let Some(mut top) = heap.peek_mut() { + if item < *top { + *top = item; + } } } - heap.into_iter().collect() + let result: Vec = unfold(|| heap.pop()).collect(); + result.into_iter().rev().collect() } /// Creates a new collection from this collection by replacing the @@ -1183,7 +1193,7 @@ pub trait Collectible: IntoIterator { /// /// # let source = vec![1, 2, 2, 3]; /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.substitute(&2, 4), vec![1, 4, 2, 3]); /// @@ -1214,7 +1224,7 @@ pub trait Collectible: IntoIterator { /// /// # let source = vec![1, 2, 2, 3]; /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.substitute_multi(&vec![2, 3], vec![4, 5]), vec![1, 4, 2, 5]); /// # let a = source.clone(); @@ -1268,7 +1278,7 @@ pub trait Collectible: IntoIterator { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// let sum = a.sum(); /// diff --git a/src/extensions/ordered.rs b/src/extensions/ordered.rs index 8718723..45a639e 100644 --- a/src/extensions/ordered.rs +++ b/src/extensions/ordered.rs @@ -60,7 +60,7 @@ pub trait Ordered { /// /// let a = vec![1, 2, 2, 3]; /// let b = vec![1, 1]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.count_unique(), 3); /// assert_eq!(b.count_unique(), 1); @@ -156,7 +156,7 @@ pub trait Ordered { /// use cantrip::*; /// /// let a = [1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.joined(", "), "1, 2, 3"); /// assert_eq!(e.joined(", "), ""); diff --git a/src/extensions/sequence.rs b/src/extensions/sequence.rs index 6a5be22..eec8b7e 100644 --- a/src/extensions/sequence.rs +++ b/src/extensions/sequence.rs @@ -28,7 +28,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2]; /// let a = vec![1, 2]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.add_at_multi(0, vec![3, 4]), vec![3, 4, 1, 2]); /// # let a = source.clone(); @@ -74,7 +74,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2]; /// let a = vec![1, 2]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.add_at(0, 3), vec![3, 1, 2]); /// # let a = source.clone(); @@ -109,7 +109,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.cartesian_product(0), vec![vec![]]); /// assert_eq!(a.cartesian_product(1), vec![vec![1], vec![2], vec![3]]); @@ -121,7 +121,7 @@ pub trait Sequence { /// ] /// ); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(e.cartesian_product(2), empty_result); /// ``` fn cartesian_product(&self, k: usize) -> Vec @@ -184,12 +184,12 @@ pub trait Sequence { /// /// let a = vec![2, 3, 1, 1, 2, 3]; /// let b = vec![1]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// let chunked = a.chunked_by(|&p, &n| p > 0 && n < 3); /// assert_eq!(chunked, vec![vec![2, 3], vec![1], vec![1], vec![2, 3]]); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(b.chunked_by(|_, _| true), vec![vec![1]]); /// assert_eq!(e.chunked_by(|_, _| true), empty_result); /// ``` @@ -333,7 +333,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.combinations_multi(0), vec![vec![]]); /// assert_eq!(a.combinations_multi(1), vec![vec![1], vec![2], vec![3]]); @@ -345,7 +345,7 @@ pub trait Sequence { /// vec![1, 3, 3], vec![2, 2, 2], vec![2, 2, 3], vec![2, 3, 3], vec![3, 3, 3], /// ]); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(e.combinations_multi(2), empty_result); /// ``` fn combinations_multi(&self, k: usize) -> Vec @@ -365,7 +365,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.delete_at(0), vec![2, 3]); /// # let a = source.clone(); @@ -397,7 +397,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.delete_at_multi(vec![0, 2]), vec![2]); /// # let a = source.clone(); @@ -564,7 +564,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.init(), vec![1, 2]); /// @@ -1206,7 +1206,7 @@ pub trait Sequence { /// use crate::cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.skip(2), vec![3]); /// @@ -1237,7 +1237,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![-1, 0, 1]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.skip_while(|&x| x < 0), vec![0, 1]); /// @@ -1262,7 +1262,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.slice(0..2), vec![1, 2]); /// # let a = source.clone(); @@ -1651,7 +1651,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.substitute_at(1, 4), vec![1, 4, 3]); /// @@ -1679,7 +1679,7 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.substitute_at_multi(vec![0, 2], vec![4, 5]), vec![4, 2, 5]); /// # let a = source.clone(); @@ -1722,7 +1722,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.tail(), vec![2, 3]); /// @@ -1764,7 +1764,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.take(5), vec![1, 2]); /// @@ -1793,7 +1793,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![-1, 0, 1]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.take_while(|&x| x <= 0), vec![-1, 0]); /// @@ -1925,7 +1925,7 @@ pub trait Sequence { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.variations(0), vec![vec![]]); /// assert_eq!(a.variations(1), vec![vec![1], vec![2], vec![3]]); @@ -1937,7 +1937,7 @@ pub trait Sequence { /// vec![1, 2, 3], vec![1, 3, 2], vec![2, 1, 3], vec![2, 3, 1], vec![3, 1, 2], vec![3, 2, 1], /// ]); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(e.variations(2), empty_result); /// ``` fn variations(&self, k: usize) -> Vec @@ -1969,13 +1969,13 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3, 4, 5]; /// let a = vec![1, 2, 3, 4, 5]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.windowed(2, 1), vec![vec![1, 2], vec![2, 3], vec![3, 4], vec![4, 5]]); /// # let a = source.clone(); /// assert_eq!(a.windowed(2, 2), vec![vec![1, 2], vec![3, 4]]); /// - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(e.windowed(1, 1), empty_result); /// ``` fn windowed(&self, size: usize, step: usize) -> Self::This @@ -2005,12 +2005,12 @@ pub trait Sequence { /// /// # let source = vec![1, 2, 3, 4, 5]; /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.windowed_circular(2, 1), vec![vec![1, 2], vec![2, 3], vec![3, 1]]); /// # let a = source.clone(); /// assert_eq!(a.windowed_circular(2, 2), vec![vec![1, 2], vec![3, 4], vec![5, 1]]); - /// let empty_result: Vec> = Vec::new(); + /// let empty_result: Vec> = vec![]; /// assert_eq!(e.windowed(1, 1), empty_result); /// ``` fn windowed_circular(&self, size: usize, step: usize) -> Self::This diff --git a/src/extensions/traversable.rs b/src/extensions/traversable.rs index bf849f7..c023b27 100644 --- a/src/extensions/traversable.rs +++ b/src/extensions/traversable.rs @@ -31,7 +31,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert!(a.all(|&x| x > 0)); /// assert!(e.all(|&x| x > 0)); @@ -59,7 +59,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert!(a.any(|&x| x > 0)); /// @@ -346,7 +346,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.max_by(|x, y| x.cmp(y)), Some(&5)); /// @@ -366,7 +366,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3_i32, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.max_by_key(|x| x.abs()), Some(&-10)); /// @@ -398,7 +398,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.max_of(), Some(&3)); /// @@ -424,7 +424,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.min_by(|x, y| x.cmp(y)), Some(&-10)); /// @@ -444,7 +444,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3_i32, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.min_by_key(|x| x.abs()), Some(&0)); /// @@ -477,7 +477,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.min_of(), Some(&1)); /// @@ -504,7 +504,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.minmax_by(|x, y| x.cmp(y)), Some((&-10, &5))); /// assert_eq!(e.minmax_by(|x, y| x.cmp(y)), None); @@ -524,7 +524,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3_i32, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.minmax_by_key(|x| x.abs()), Some((&0, &-10))); /// assert_eq!(e.minmax_by_key(|x| x.abs()), None); @@ -543,7 +543,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![-3_i32, 0, 1, 5, -10]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert_eq!(a.minmax_of(), Some((&-10, &5))); /// assert_eq!(e.minmax_of(), None); @@ -605,7 +605,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert!(a.subset(&vec![4, 3, 2, 2, 1])); /// assert!(e.subset(&vec![1])); @@ -632,7 +632,7 @@ pub trait Traversable { /// use cantrip::*; /// /// let a = vec![1, 2, 2, 3]; - /// let e: Vec = Vec::new(); + /// let e: Vec = vec![]; /// /// assert!(a.superset(&vec![3, 1])); /// assert!(a.superset(&vec![2, 2, 1]));