mirror of
https://github.com/rwxrob/bonzai
synced 2024-11-18 15:25:45 +00:00
Rename loop.Every to loop.All
This commit is contained in:
parent
38ec166c55
commit
97c4525d1a
19
loop/loop.go
19
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) })
|
||||
}
|
||||
|
@ -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"}
|
||||
|
Loading…
Reference in New Issue
Block a user