From bae96f4ac3fc526272577c8fb11b41ba80404bfb Mon Sep 17 00:00:00 2001 From: julio4 <30329843+julio4@users.noreply.github.com> Date: Wed, 15 Jan 2025 12:42:27 +0100 Subject: [PATCH 1/4] draft: Iterator::collect --- corelib/src/iter/traits/collect.cairo | 4 ++-- corelib/src/iter/traits/iterator.cairo | 8 ++++++++ corelib/src/test/iter_test.cairo | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/corelib/src/iter/traits/collect.cairo b/corelib/src/iter/traits/collect.cairo index c6078c4ca7d..d57b9fd84fe 100644 --- a/corelib/src/iter/traits/collect.cairo +++ b/corelib/src/iter/traits/collect.cairo @@ -65,7 +65,7 @@ /// /// assert_eq!(c.arr, array![0, 1, 2, 3, 4]); /// ``` -pub trait FromIterator { +pub trait FromIterator { /// Creates a value from an iterator. /// /// See the [module-level documentation] for more. @@ -81,7 +81,7 @@ pub trait FromIterator { /// /// assert_eq!(v, array![0, 1, 2, 3, 4]); /// ``` - fn from_iter[Item: G], +Destruct>(iter: I) -> T; + fn from_iter[Item: A], +Drop>(iter: I) -> T; } /// Conversion into an [`Iterator`]. diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index b90a337154e..e3d48518d32 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -312,4 +312,12 @@ pub trait Iterator { ) -> Zip { zipped_iterator(self, other.into_iter()) } + + #[inline] + #[must_use] + fn collect, +Drop>( + self: T, + ) -> B { + FromIterator::from_iter(self) + } } diff --git a/corelib/src/test/iter_test.cairo b/corelib/src/test/iter_test.cairo index 605472e239c..2b020423a20 100644 --- a/corelib/src/test/iter_test.cairo +++ b/corelib/src/test/iter_test.cairo @@ -53,3 +53,12 @@ fn test_iter_adapter_fold() { assert_eq!(sum, 6); } + +#[test] +fn test_iter_adapter_collect() { + let arr: Array = (0..3_u32).into_iter().collect(); + + assert_eq!(*arr[0], 0); + assert_eq!(*arr[1], 1); + assert_eq!(*arr[2], 2); +} From 3ec3874245b8ece20a92d7a45207e9a76e226d65 Mon Sep 17 00:00:00 2001 From: julio4 <30329843+julio4@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:49:08 +0100 Subject: [PATCH 2/4] fix: iter collect (wip) --- corelib/src/iter/traits/iterator.cairo | 2 +- corelib/src/test/iter_test.cairo | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index e3d48518d32..a26028ed9dc 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -315,7 +315,7 @@ pub trait Iterator { #[inline] #[must_use] - fn collect, +Drop>( + fn collect, +Destruct>( self: T, ) -> B { FromIterator::from_iter(self) diff --git a/corelib/src/test/iter_test.cairo b/corelib/src/test/iter_test.cairo index 2b020423a20..3b774bc6223 100644 --- a/corelib/src/test/iter_test.cairo +++ b/corelib/src/test/iter_test.cairo @@ -56,9 +56,5 @@ fn test_iter_adapter_fold() { #[test] fn test_iter_adapter_collect() { - let arr: Array = (0..3_u32).into_iter().collect(); - - assert_eq!(*arr[0], 0); - assert_eq!(*arr[1], 1); - assert_eq!(*arr[2], 2); + assert_eq!((0..3_u32).into_iter().collect(), array![0, 1, 2]); } From 3b44f0ebd2407515969a7acff80a64ee8b738448 Mon Sep 17 00:00:00 2001 From: julio4 <30329843+julio4@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:05:58 +0100 Subject: [PATCH 3/4] fix: Iterator::collect explicit trait bounds --- corelib/src/iter/traits/collect.cairo | 2 +- corelib/src/iter/traits/iterator.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/corelib/src/iter/traits/collect.cairo b/corelib/src/iter/traits/collect.cairo index d57b9fd84fe..a8a1ef534b1 100644 --- a/corelib/src/iter/traits/collect.cairo +++ b/corelib/src/iter/traits/collect.cairo @@ -81,7 +81,7 @@ pub trait FromIterator { /// /// assert_eq!(v, array![0, 1, 2, 3, 4]); /// ``` - fn from_iter[Item: A], +Drop>(iter: I) -> T; + fn from_iter[Item: A], +Destruct>(iter: I) -> T; } /// Conversion into an [`Iterator`]. diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index a26028ed9dc..f13f3664d8f 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -318,6 +318,6 @@ pub trait Iterator { fn collect, +Destruct>( self: T, ) -> B { - FromIterator::from_iter(self) + FromIterator::::from_iter::(self) } } From a0a82a81a5dacb4c4e399cc58b7745abe456065e Mon Sep 17 00:00:00 2001 From: julio4 <30329843+julio4@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:53:00 +0100 Subject: [PATCH 4/4] doc: Iterator::collect --- corelib/src/iter/traits/iterator.cairo | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index f13f3664d8f..b260ef2edfe 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -313,6 +313,53 @@ pub trait Iterator { zipped_iterator(self, other.into_iter()) } + /// Transforms an iterator into a collection. + /// + /// `collect()` can take anything iterable, and turn it into a relevant + /// collection. This is one of the more powerful methods in the core + /// library, used in a variety of contexts. + /// + /// The most basic pattern in which `collect()` is used is to turn one + /// collection into another. You take a collection, call [`iter`] on it, + /// do a bunch of transformations, and then `collect()` at the end. + /// + /// `collect()` can also create instances of types that are not typical + /// collections. + /// + /// Because `collect()` is so general, it can cause problems with type + /// inference. As such, `collect()` is one of the few times you'll see + /// the syntax affectionately known as the 'turbofish': `::<>`. This + /// helps the inference algorithm understand specifically which collection + /// you're trying to collect into. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let doubled: Array = array![1, 2, 3].into_iter().map(|x| x * 2).collect(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` + /// + /// Note that we needed the `: Array` on the left-hand side. + /// + /// Using the 'turbofish' instead of annotating `doubled`: + /// + /// ``` + /// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::>(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` + /// + /// Because `collect()` only cares about what you're collecting into, you can + /// still use a partial type hint, `_`, with the turbofish: + /// + /// ``` + /// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::>(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` #[inline] #[must_use] fn collect, +Destruct>(