Add loop.Print and loop.Printf

pull/53/head
rwxrob 2 years ago
parent 01a3a765e3
commit 38ec166c55
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -20,3 +20,13 @@ func Do[T any](set []T, p func(i T) error) error {
func Println[T any](set []T) {
Do(set, func(i T) error { fmt.Println(i); return nil })
}
// Print prints ever element of the set.
func Print[T any](set []T) {
Do(set, func(i T) error { fmt.Print(i); return nil })
}
// 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 })
}

@ -15,3 +15,21 @@ func ExamplePrintln() {
// true
// true
}
func ExamplePrint() {
set := []string{"doe", "ray", "mi"}
loop.Print(set)
bools := []bool{false, true, true}
loop.Print(bools)
// Output:
// doeraymifalsetruetrue
}
func ExamplePrintf() {
set := []string{"doe", "ray", "mi"}
loop.Printf(set, "sing %v\n")
// Output:
// sing doe
// sing ray
// sing mi
}

Loading…
Cancel
Save