From 97c4525d1ab25defdb1e19235b98d2ea0130e475 Mon Sep 17 00:00:00 2001 From: rwxrob Date: Sat, 26 Feb 2022 01:01:21 -0500 Subject: [PATCH] Rename loop.Every to loop.All --- loop/loop.go | 19 +++++++++++++------ loop/loop_test.go | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/loop/loop.go b/loop/loop.go index b06de7a..2325873 100644 --- a/loop/loop.go +++ b/loop/loop.go @@ -5,9 +5,16 @@ package loop import "fmt" -// Do executes the given function for each item in the slice. If any -// error is encountered processing stops and error returned. -func Do[T any](set []T, p func(i T) error) error { +// All executes the given function for every item in the slice. +func All[T any](set []T, p func(i T)) { + for _, i := range set { + p(i) + } +} + +// UntilError executes the give function for every item in the slice +// until it encounters and error and returns the error, if any. +func UntilError[T any](set []T, p func(i T) error) error { for _, i := range set { if err := p(i); err != nil { return err @@ -18,15 +25,15 @@ func Do[T any](set []T, p func(i T) error) error { // Println prints ever element of the set. func Println[T any](set []T) { - Do(set, func(i T) error { fmt.Println(i); return nil }) + All(set, func(i T) { fmt.Println(i) }) } // Print prints ever element of the set. func Print[T any](set []T) { - Do(set, func(i T) error { fmt.Print(i); return nil }) + All(set, func(i T) { fmt.Print(i) }) } // Printf prints ever element of the set using format string. func Printf[T any](set []T, form string) { - Do(set, func(i T) error { fmt.Printf(form, i); return nil }) + All(set, func(i T) { fmt.Printf(form, i) }) } diff --git a/loop/loop_test.go b/loop/loop_test.go index 8460ab9..f7689c1 100644 --- a/loop/loop_test.go +++ b/loop/loop_test.go @@ -1,6 +1,24 @@ package loop_test -import "github.com/rwxrob/bonzai/loop" +import ( + "fmt" + + "github.com/rwxrob/bonzai/loop" +) + +func ExampleAll() { + set1 := []string{"doe", "ray", "mi"} + loop.All(set1, func(s string) { fmt.Print(s) }) + fmt.Println() + f1 := func() { fmt.Print("one") } + f2 := func() { fmt.Print("two") } + f3 := func() { fmt.Print("three") } + set2 := []func(){f1, f2, f3} + loop.All(set2, func(f func()) { f() }) + // Output: + // doeraymi + // onetwothree +} func ExamplePrintln() { set := []string{"doe", "ray", "mi"}