-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_fish_test.go
54 lines (45 loc) · 1.11 KB
/
example_fish_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
package lift_test
import (
"fmt"
"github.com/AndrewHarrisSPU/lift"
)
// This example demonstrates a [Map] with [Sym] values.
// The [Map] is a dispatch table; a [Sym] may be looked up, and a related function returned.
func Example_b_fish() {
// "recievers"
type fish string
type octopus struct {
arms int
}
// "methods"
fishSlap := func(sym lift.Sym) string {
f := lift.MustUnwrap[fish](sym)
return fmt.Sprintf("%sslap", f)
}
octopusSlap := func(sym lift.Sym) (slap string) {
o := lift.MustUnwrap[octopus](sym)
for i := 0; i < o.arms; i++ {
slap += "octoslap"
}
return
}
// a dispatch map
marineLifeSlap := lift.NewMap[func(lift.Sym) string](
lift.Def[fish](fishSlap),
lift.Def[octopus](octopusSlap),
)
// the demonstration:
symbols := []lift.Sym{
lift.Wrap(fish("trout")),
lift.Wrap(fish("salmon")),
lift.Wrap(octopus{8}),
}
for _, sym := range symbols {
slap, _ := lift.LoadSym(marineLifeSlap, sym)
fmt.Printf("boom! %s!\n", slap(sym))
}
// Output:
// boom! troutslap!
// boom! salmonslap!
// boom! octoslapoctoslapoctoslapoctoslapoctoslapoctoslapoctoslapoctoslap!
}