github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/collection/collection_iterating_test.go (about)

     1  package collection
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/m4gshm/gollections/collection/immutable/set"
     7  	"github.com/m4gshm/gollections/loop"
     8  	"github.com/m4gshm/gollections/loop/range_"
     9  )
    10  
    11  func Test_Iterating_Loop(t *testing.T) {
    12  
    13  	uniques := set.From(range_.Of(0, 100))
    14  	next := uniques.Loop()
    15  	for i, ok := next(); ok; i, ok = next() {
    16  		doOp(i)
    17  	}
    18  
    19  }
    20  
    21  func Test_Iterating_Iter(t *testing.T) {
    22  
    23  	uniques := set.From(range_.Of(0, 100))
    24  	for iter, i, ok := uniques.First(); ok; i, ok = iter.Next() {
    25  		doOp(i)
    26  	}
    27  
    28  }
    29  
    30  func Test_Iterating_ForEach(t *testing.T) {
    31  
    32  	uniques := set.From(range_.Of(0, 100))
    33  	uniques.ForEach(doOp)
    34  
    35  }
    36  
    37  func Test_Iterating_For(t *testing.T) {
    38  
    39  	uniques := set.From(range_.Of(0, 100))
    40  	uniques.For(func(i int) error {
    41  		if i > 22 {
    42  			return loop.Break
    43  		}
    44  		doOp(i)
    45  		return loop.Continue
    46  	})
    47  
    48  }
    49  
    50  func doOp(i int) {
    51  
    52  }