Documentation

Init.Data.Iterators.Consumers.Loop

Loop consumers #

This module provides consumers that iterate over a given iterator, applying a certain user-supplied function in every iteration. Concretely, the following operations are provided:

These operations are implemented using the IteratorLoop and IteratorLoopPartial typeclasses.

@[inline]
def Std.Iterators.Iter.instForIn' {α β : Type w} {n : Type x → Type x'} [Monad n] [Iterator α Id β] [Finite α Id] [IteratorLoop α Id n] :
ForIn' n (Iter β) β { mem := fun (it : Iter β) (out : β) => it.IsPlausibleIndirectOutput out }

A ForIn' instance for iterators. Its generic membership relation is not easy to use, so this is not marked as instance. This way, more convenient instances can be built on top of it or future library improvements will make it more comfortable.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    @[inline]
    def Std.Iterators.Iter.Partial.instForIn' {α β : Type w} {n : Type x → Type x'} [Monad n] [Iterator α Id β] [IteratorLoopPartial α Id n] :
    ForIn' n (Partial β) β { mem := fun (it : Partial β) (out : β) => it.it.IsPlausibleIndirectOutput out }
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      instance Std.Iterators.instForMIterOfFiniteOfIteratorLoopId {m : Type x → Type x'} {α β : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id m] :
      ForM m (Iter β) β
      Equations
      • One or more equations did not get rendered due to their size.
      Equations
      • One or more equations did not get rendered due to their size.
      @[inline]
      def Std.Iterators.Iter.foldM {m : Type x → Type x'} [Monad m] {α β : Type w} {γ : Type x} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id m] (f : γβm γ) (init : γ) (it : Iter β) :
      m γ

      Folds a monadic function over an iterator from the left, accumulating a value starting with init. The accumulated value is combined with the each element of the list in order, using f.

      It is equivalent to it.toList.foldlM.

      This function requires a Finite instance proving that the iterator will finish after a finite number of steps. If the iterator is not finite or such an instance is not available, consider using it.allowNontermination.foldM instead of it.foldM. However, it is not possible to formally verify the behavior of the partial variant.

      Equations
      Instances For
        @[inline]
        def Std.Iterators.Iter.Partial.foldM {m : Type x → Type x'} [Monad m] {α β : Type w} {γ : Type x} [Iterator α Id β] [IteratorLoopPartial α Id m] (f : γβm γ) (init : γ) (it : Partial β) :
        m γ

        Folds a monadic function over an iterator from the left, accumulating a value starting with init. The accumulated value is combined with the each element of the list in order, using f.

        It is equivalent to it.toList.foldlM.

        This is a partial, potentially nonterminating, function. It is not possible to formally verify its behavior. If the iterator has a Finite instance, consider using IterM.foldM instead.

        Equations
        Instances For
          @[inline]
          def Std.Iterators.Iter.fold {α β : Type w} {γ : Type x} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] (f : γβγ) (init : γ) (it : Iter β) :
          γ

          Folds a function over an iterator from the left, accumulating a value starting with init. The accumulated value is combined with the each element of the list in order, using f.

          It is equivalent to it.toList.foldl.

          This function requires a Finite instance proving that the iterator will finish after a finite number of steps. If the iterator is not finite or such an instance is not available, consider using it.allowNontermination.fold instead of it.fold. However, it is not possible to formally verify the behavior of the partial variant.

          Equations
          Instances For
            @[inline]
            def Std.Iterators.Iter.Partial.fold {α β : Type w} {γ : Type x} [Iterator α Id β] [IteratorLoopPartial α Id Id] (f : γβγ) (init : γ) (it : Partial β) :
            γ

            Folds a function over an iterator from the left, accumulating a value starting with init. The accumulated value is combined with the each element of the list in order, using f.

            It is equivalent to it.toList.foldl.

            This is a partial, potentially nonterminating, function. It is not possible to formally verify its behavior. If the iterator has a Finite instance, consider using IterM.fold instead.

            Equations
            Instances For
              @[specialize #[]]
              def Std.Iterators.Iter.anyM {α β : Type w} {m : TypeType w'} [Monad m] [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id] (p : βm Bool) (it : Iter β) :

              Returns true if the monadic predicate p returns true for any element emitted by the iterator it.

              O(|xs|). Short-circuits upon encountering the first match. The elements in it are examined in order of iteration.

              Equations
              Instances For
                @[inline]
                def Std.Iterators.Iter.any {α β : Type w} [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id] (p : βBool) (it : Iter β) :

                Returns true if the pure predicate p returns true for any element emitted by the iterator it.

                O(|xs|). Short-circuits upon encountering the first match. The elements in it are examined in order of iteration.

                Equations
                Instances For
                  @[specialize #[]]
                  def Std.Iterators.Iter.allM {α β : Type w} {m : TypeType w'} [Monad m] [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id] (p : βm Bool) (it : Iter β) :

                  Returns true if the monadic predicate p returns true for all elements emitted by the iterator it.

                  O(|xs|). Short-circuits upon encountering the first mismatch. The elements in it are examined in order of iteration.

                  Equations
                  Instances For
                    @[inline]
                    def Std.Iterators.Iter.all {α β : Type w} [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id] (p : βBool) (it : Iter β) :

                    Returns true if the pure predicate p returns true for all elements emitted by the iterator it.

                    O(|xs|). Short-circuits upon encountering the first mismatch. The elements in it are examined in order of iteration.

                    Equations
                    Instances For
                      @[inline]
                      def Std.Iterators.Iter.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m] [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id] (it : Iter β) (f : βm (Option γ)) :
                      m (Option γ)
                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[inline]
                        def Std.Iterators.Iter.Partial.findSomeM? {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m] [Iterator α Id β] [IteratorLoopPartial α Id m] (it : Partial β) (f : βm (Option γ)) :
                        m (Option γ)
                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[inline]
                          def Std.Iterators.Iter.findSome? {α β : Type w} {γ : Type x} [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id] (it : Iter β) (f : βOption γ) :
                          Equations
                          Instances For
                            @[inline]
                            def Std.Iterators.Iter.Partial.findSome? {α β : Type w} {γ : Type x} [Iterator α Id β] [IteratorLoopPartial α Id Id] (it : Partial β) (f : βOption γ) :
                            Equations
                            Instances For
                              @[inline]
                              def Std.Iterators.Iter.findM? {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id] (it : Iter β) (f : βm (ULift Bool)) :
                              m (Option β)
                              Equations
                              Instances For
                                @[inline]
                                def Std.Iterators.Iter.Partial.findM? {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α Id β] [IteratorLoopPartial α Id m] (it : Partial β) (f : βm (ULift Bool)) :
                                m (Option β)
                                Equations
                                Instances For
                                  @[inline]
                                  def Std.Iterators.Iter.find? {α β : Type w} [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id] (it : Iter β) (f : βBool) :
                                  Equations
                                  Instances For
                                    @[inline]
                                    def Std.Iterators.Iter.Partial.find? {α β : Type w} [Iterator α Id β] [IteratorLoopPartial α Id Id] (it : Partial β) (f : βBool) :
                                    Equations
                                    Instances For
                                      @[inline]
                                      def Std.Iterators.Iter.count {α β : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] (it : Iter β) :

                                      Steps through the whole iterator, counting the number of outputs emitted.

                                      Performance:

                                      This function's runtime is linear in the number of steps taken by the iterator.

                                      Equations
                                      Instances For
                                        @[inline, deprecated Std.Iterators.Iter.count (since := "2025-10-29")]
                                        def Std.Iterators.Iter.size {α β : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] (it : Iter β) :

                                        Steps through the whole iterator, counting the number of outputs emitted.

                                        Performance:

                                        This function's runtime is linear in the number of steps taken by the iterator.

                                        Equations
                                        Instances For
                                          @[inline]

                                          Steps through the whole iterator, counting the number of outputs emitted.

                                          Performance:

                                          This function's runtime is linear in the number of steps taken by the iterator.

                                          Equations
                                          Instances For
                                            @[inline, deprecated Std.Iterators.Iter.Partial.count (since := "2025-10-29")]

                                            Steps through the whole iterator, counting the number of outputs emitted.

                                            Performance:

                                            This function's runtime is linear in the number of steps taken by the iterator.

                                            Equations
                                            Instances For