77
88prelude
99public import Init.Data.Iterators.Consumers.Partial
10+ public import Init.Data.Iterators.Consumers.Total
1011public import Init.Data.Iterators.Consumers.Monadic.Collect
1112
1213@[expose] public section
@@ -21,40 +22,113 @@ Concretely, the following operations are provided:
2122* `Iter.toListRev`, collecting the values in a list in reverse order but more efficiently
2223* `Iter.toArray`, collecting the values in an array
2324
24- Some operations are implemented using the `IteratorCollect` and `IteratorCollectPartial`
25- typeclasses.
25+ Some operations are implemented using the `IteratorCollect` type class.
2626-/
2727
2828namespace Std.Iterators
2929
30- @[always_inline, inline, inherit_doc IterM.toArray]
30+ /--
31+ Traverses the given iterator and stores the emitted values in an array.
32+
33+ If the iterator is not finite, this function might run forever. The variant
34+ `it.ensureTermination.toArray` always terminates after finitely many steps.
35+ -/
36+ @[always_inline, inline]
3137def Iter.toArray {α : Type w} {β : Type w}
32- [Iterator α Id β] [Finite α Id] [ IteratorCollect α Id Id] (it : Iter (α := α) β) : Array β :=
38+ [Iterator α Id β] [IteratorCollect α Id Id] (it : Iter (α := α) β) : Array β :=
3339 it.toIterM.toArray.run
3440
35- @[always_inline, inline, inherit_doc IterM.Partial.toArray]
41+ /--
42+ Traverses the given iterator and stores the emitted values in an array.
43+
44+ This function is deprecated. Instead of `it.allowNontermination.toArray`, use `it.toArray`.
45+ -/
46+ @[always_inline, inline, deprecated Iter.toArray (since := " 2025-12-04" )]
3647def Iter.Partial.toArray {α : Type w} {β : Type w}
37- [Iterator α Id β] [IteratorCollectPartial α Id Id] (it : Iter.Partial (α := α) β) : Array β :=
38- it.it.toIterM.allowNontermination.toArray.run
48+ [Iterator α Id β] [IteratorCollect α Id Id] (it : Iter.Partial (α := α) β) : Array β :=
49+ it.it.toArray
50+
51+ /--
52+ Traverses the given iterator and stores the emitted values in an array.
3953
40- @[always_inline, inline, inherit_doc IterM.toListRev]
54+ This variant terminates after finitely many steps and requires a proof that the iterator is
55+ finite. If such a proof is not available, consider using `Iter.toArray`.
56+ -/
57+ @[always_inline, inline]
58+ def Iter.Total.toArray {α : Type w} {β : Type w}
59+ [Iterator α Id β] [Finite α Id] [IteratorCollect α Id Id] (it : Iter.Total (α := α) β) :
60+ Array β :=
61+ it.it.toArray
62+
63+ /--
64+ Traverses the given iterator and stores the emitted values in reverse order in a list. Because
65+ lists are prepend-only, this `toListRev` is usually more efficient that `toList`.
66+
67+ If the iterator is not finite, this function might run forever. The variant
68+ `it.ensureTermination.toListRev` always terminates after finitely many steps.
69+ -/
70+ @[always_inline, inline]
4171def Iter.toListRev {α : Type w} {β : Type w}
42- [Iterator α Id β] [Finite α Id] (it : Iter (α := α) β) : List β :=
72+ [Iterator α Id β] (it : Iter (α := α) β) : List β :=
4373 it.toIterM.toListRev.run
4474
45- @[always_inline, inline, inherit_doc IterM.Partial.toListRev]
75+ /--
76+ Traverses the given iterator and stores the emitted values in reverse order in a list. Because
77+ lists are prepend-only, this `toListRev` is usually more efficient that `toList`.
78+
79+ This function is deprecated. Instead of `it.allowNontermination.toListRev`, use `it.toListRev`.
80+ -/
81+ @[always_inline, inline, deprecated Iter.toListRev (since := " 2025-12-04" )]
4682def Iter.Partial.toListRev {α : Type w} {β : Type w}
4783 [Iterator α Id β] (it : Iter.Partial (α := α) β) : List β :=
48- it.it.toIterM.allowNontermination. toListRev.run
84+ it.it.toListRev
4985
50- @[always_inline, inline, inherit_doc IterM.toList]
86+ /--
87+ Traverses the given iterator and stores the emitted values in reverse order in a list. Because
88+ lists are prepend-only, this `toListRev` is usually more efficient that `toList`.
89+
90+ This variant terminates after finitely many steps and requires a proof that the iterator is
91+ finite. If such a proof is not available, consider using `Iter.toListRev`.
92+ -/
93+ @[always_inline, inline]
94+ def Iter.Total.toListRev {α : Type w} {β : Type w}
95+ [Iterator α Id β] [Finite α Id] (it : Iter.Total (α := α) β) : List β :=
96+ it.it.toListRev
97+
98+ /--
99+ Traverses the given iterator and stores the emitted values in a list. Because
100+ lists are prepend-only, `toListRev` is usually more efficient that `toList`.
101+
102+ If the iterator is not finite, this function might run forever. The variant
103+ `it.ensureTermination.toList` always terminates after finitely many steps.
104+ -/
105+ @[always_inline, inline]
51106def Iter.toList {α : Type w} {β : Type w}
52- [Iterator α Id β] [Finite α Id] [ IteratorCollect α Id Id] (it : Iter (α := α) β) : List β :=
107+ [Iterator α Id β] [IteratorCollect α Id Id] (it : Iter (α := α) β) : List β :=
53108 it.toIterM.toList.run
54109
55- @[always_inline, inline, inherit_doc IterM.Partial.toList]
110+ /--
111+ Traverses the given iterator and stores the emitted values in a list. Because
112+ lists are prepend-only, `toListRev` is usually more efficient that `toList`.
113+
114+ This function is deprecated. Instead of `it.allowNontermination.toList`, use `it.toList`.
115+ -/
116+ @[always_inline, deprecated Iter.toList (since := " 2025-12-04" )]
56117def Iter.Partial.toList {α : Type w} {β : Type w}
57- [Iterator α Id β] [IteratorCollectPartial α Id Id] (it : Iter.Partial (α := α) β) : List β :=
58- it.it.toIterM.allowNontermination.toList.run
118+ [Iterator α Id β] [IteratorCollect α Id Id] (it : Iter.Partial (α := α) β) : List β :=
119+ it.it.toList
120+
121+ /--
122+ Traverses the given iterator and stores the emitted values in a list. Because
123+ lists are prepend-only, `toListRev` is usually more efficient that `toList`.
124+
125+ This variant terminates after finitely many steps and requires a proof that the iterator is
126+ finite. If such a proof is not available, consider using `Iter.toList`.
127+ -/
128+ @[always_inline, inline]
129+ def Iter.Total.toList {α : Type w} {β : Type w}
130+ [Iterator α Id β] [Finite α Id] [IteratorCollect α Id Id] (it : Iter.Total (α := α) β) :
131+ List β :=
132+ it.it.toList
59133
60134end Std.Iterators
0 commit comments