-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_fizzbuzz_test.go
63 lines (56 loc) · 1.05 KB
/
example_fizzbuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package lift_test
import (
"fmt"
"github.com/AndrewHarrisSPU/lift"
)
// This example demonstrates lifting a function.
// If the lifted function is passed a [Sym] that doesn't match the desired type,
// the return is zero valued.
func Example_a_fizzbuzz() {
sayFizz := liftFizzBuzz(func(_ fizz) string {
return "fizz"
})
sayBuzz := liftFizzBuzz(func(_ buzz) string {
return "buzz"
})
for i := 1; i < 31; i++ {
f, b := parseFizzBuzz(i)
if res := sayFizz(f) + sayBuzz(b); res != "" {
fmt.Println(i, res)
}
}
// Output:
// 3 fizz
// 5 buzz
// 6 fizz
// 9 fizz
// 10 buzz
// 12 fizz
// 15 fizzbuzz
// 18 fizz
// 20 buzz
// 21 fizz
// 24 fizz
// 25 buzz
// 27 fizz
// 30 fizzbuzz
}
type fizz struct{}
type buzz struct{}
func liftFizzBuzz[T any](fn func(T) string) func(lift.Sym) string {
return func(sym lift.Sym) string {
if t, ok := lift.Unwrap[T](sym); ok {
return fn(t)
}
return ""
}
}
func parseFizzBuzz(i int) (f, b lift.Sym) {
if i%3 == 0 {
f = lift.Wrap(fizz{})
}
if i%5 == 0 {
b = lift.Wrap(buzz{})
}
return
}