mirror of
https://github.com/rwxrob/bonzai
synced 2024-11-04 18:00:18 +00:00
Add each package to replace loop
This commit is contained in:
parent
b62baa6995
commit
6cd4df1060
53
each/each.go
Normal file
53
each/each.go
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
Package each shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go.
|
||||
*/
|
||||
package each
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Do executes the given function for every item in the slice.
|
||||
func Do[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
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Println prints ever element of the set.
|
||||
func Println[T any](set []T) {
|
||||
Do(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) { fmt.Print(i) })
|
||||
}
|
||||
|
||||
// Printf calls fmt.Printf on itself with the given form. For more
|
||||
// substantial printing consider calling each.Do instead.
|
||||
func Printf[T any](set []T, form string) {
|
||||
Do(set, func(i T) { fmt.Printf(form, i) })
|
||||
}
|
||||
|
||||
// Log calls log.Print on ever element of the set.
|
||||
func Log[T any](set []T) {
|
||||
Do(set, func(i T) { log.Print(i) })
|
||||
}
|
||||
|
||||
// Logf calls log.Printf on ever element of the set.
|
||||
func Logf[T any](set []T, form string) {
|
||||
Do(set, func(i T) { log.Printf(form, i) })
|
||||
}
|
77
each/each_test.go
Normal file
77
each/each_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
package each_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/rwxrob/bonzai/each"
|
||||
)
|
||||
|
||||
func ExampleDo() {
|
||||
set1 := []string{"doe", "ray", "mi"}
|
||||
each.Do(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}
|
||||
each.Do(set2, func(f func()) { f() })
|
||||
// Output:
|
||||
// doeraymi
|
||||
// onetwothree
|
||||
}
|
||||
|
||||
func ExamplePrintln() {
|
||||
set := []string{"doe", "ray", "mi"}
|
||||
each.Println(set)
|
||||
bools := []bool{false, true, true}
|
||||
each.Println(bools)
|
||||
// Output:
|
||||
// doe
|
||||
// ray
|
||||
// mi
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
func ExamplePrint() {
|
||||
set := []string{"doe", "ray", "mi"}
|
||||
each.Print(set)
|
||||
bools := []bool{false, true, true}
|
||||
each.Print(bools)
|
||||
// Output:
|
||||
// doeraymifalsetruetrue
|
||||
}
|
||||
|
||||
func ExamplePrintf() {
|
||||
set := []string{"doe", "ray", "mi"}
|
||||
each.Printf(set, "sing %v\n")
|
||||
// Output:
|
||||
// sing doe
|
||||
// sing ray
|
||||
// sing mi
|
||||
}
|
||||
|
||||
func ExampleLogf() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(0)
|
||||
set := []string{"doe", "ray", "mi"}
|
||||
each.Logf(set, "sing %v\n")
|
||||
// Output:
|
||||
// sing doe
|
||||
// sing ray
|
||||
// sing mi
|
||||
}
|
||||
|
||||
func ExampleLog() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(0)
|
||||
set := []string{"doe", "ray", "mi"}
|
||||
each.Log(set)
|
||||
// Output:
|
||||
// doe
|
||||
// ray
|
||||
// mi
|
||||
}
|
Loading…
Reference in New Issue
Block a user