99public import Init.Data.Iterators.Consumers.Collect
1010public import Init.Data.Iterators.Consumers.Monadic.Loop
1111
12+ set_option linter.missingDocs true
13+
1214public section
1315
1416/-!
@@ -46,6 +48,9 @@ instance (α : Type w) (β : Type w) (n : Type x → Type x') [Monad n]
4648 haveI : ForIn' n (Iter (α := α) β) β _ := Iter.instForIn'
4749 instForInOfForIn'
4850
51+ /--
52+ An implementation of `for h : ... in ... do ...` notation for partial iterators.
53+ -/
4954@[always_inline, inline]
5055def Iter.Partial.instForIn' {α : Type w} {β : Type w} {n : Type x → Type x'} [Monad n]
5156 [Iterator α Id β] [IteratorLoopPartial α Id n] :
@@ -202,6 +207,11 @@ def Iter.all {α β : Type w}
202207 (p : β → Bool) (it : Iter (α := α) β) : Bool :=
203208 (it.allM (fun x => pure (f := Id) (p x))).run
204209
210+ /--
211+ Steps through the iterator until the monadic function `f` returns `some` for an element, at which
212+ point iteration stops and the result of `f` is returned. If the iterator is completely consumed
213+ without `f` returning `some`, then the result is `none`.
214+ -/
205215@[inline]
206216def Iter.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m] [Iterator α Id β]
207217 [IteratorLoop α Id m] [Finite α Id] (it : Iter (α := α) β) (f : β → m (Option γ)) :
@@ -211,7 +221,7 @@ def Iter.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Mon
211221 | none => return .yield none
212222 | some fx => return .done (some fx))
213223
214- @[inline]
224+ @[inline, inherit_doc Iter.findSomeM? ]
215225def Iter.Partial.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m]
216226 [Iterator α Id β] [IteratorLoopPartial α Id m] (it : Iter.Partial (α := α) β)
217227 (f : β → m (Option γ)) :
@@ -221,36 +231,50 @@ def Iter.Partial.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type
221231 | none => return .yield none
222232 | some fx => return .done (some fx))
223233
234+ /--
235+ Steps through the iterator until `f` returns `some` for an element, at which point iteration stops
236+ and the result of `f` is returned. If the iterator is completely consumed without `f` returning
237+ `some`, then the result is `none`.
238+ -/
224239@[inline]
225240def Iter.findSome? {α β : Type w} {γ : Type x} [Iterator α Id β]
226241 [IteratorLoop α Id Id] [Finite α Id] (it : Iter (α := α) β) (f : β → Option γ) :
227242 Option γ :=
228243 Id.run (it.findSomeM? (pure <| f ·))
229244
230- @[inline]
245+ @[inline, inherit_doc Iter.findSome? ]
231246def Iter.Partial.findSome? {α β : Type w} {γ : Type x} [Iterator α Id β]
232247 [IteratorLoopPartial α Id Id] (it : Iter.Partial (α := α) β) (f : β → Option γ) :
233248 Option γ :=
234249 Id.run (it.findSomeM? (pure <| f ·))
235250
251+ /--
252+ Steps through the iterator until an element satisfies the monadic predicate `f`, at which point
253+ iteration stops and the element is returned. If no element satisfies `f`, then the result is
254+ `none`.
255+ -/
236256@[inline]
237257def Iter.findM? {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α Id β]
238258 [IteratorLoop α Id m] [Finite α Id] (it : Iter (α := α) β) (f : β → m (ULift Bool)) :
239259 m (Option β) :=
240260 it.findSomeM? (fun x => return if (← f x).down then some x else none)
241261
242- @[inline]
262+ @[inline, inherit_doc Iter.findM? ]
243263def Iter.Partial.findM? {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α Id β]
244264 [IteratorLoopPartial α Id m] (it : Iter.Partial (α := α) β) (f : β → m (ULift Bool)) :
245265 m (Option β) :=
246266 it.findSomeM? (fun x => return if (← f x).down then some x else none)
247267
268+ /--
269+ Steps through the iterator until an element satisfies `f`, at which point iteration stops and the
270+ element is returned. If no element satisfies `f`, then the result is `none`.
271+ -/
248272@[inline]
249273def Iter.find? {α β : Type w} [Iterator α Id β] [IteratorLoop α Id Id]
250274 [Finite α Id] (it : Iter (α := α) β) (f : β → Bool) : Option β :=
251275 Id.run (it.findM? (pure <| .up <| f ·))
252276
253- @[inline]
277+ @[inline, inherit_doc Iter.find? ]
254278def Iter.Partial.find? {α β : Type w} [Iterator α Id β] [IteratorLoopPartial α Id Id]
255279 (it : Iter.Partial (α := α) β) (f : β → Bool) : Option β :=
256280 Id.run (it.findM? (pure <| .up <| f ·))
0 commit comments