-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_test.go
More file actions
56 lines (48 loc) · 1.1 KB
/
example_test.go
File metadata and controls
56 lines (48 loc) · 1.1 KB
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
package taskgraph_test
import (
"context"
"fmt"
"log"
tg "github.com/thought-machine/taskgraph"
)
var (
keyInput = tg.NewKey[string]("input")
keyReversed = tg.NewNamespacedKey[string]("input", "reversed")
keyResult = tg.NewKey[bool]("result")
)
var taskReverseInput = tg.Reflect[string]{
Name: "reverseInput",
ResultKey: keyReversed,
Fn: func(input string) (string, error) {
var res string
for _, v := range input {
res = string(v) + res
}
return res, nil
},
Depends: []any{keyInput},
}.Locate()
var taskIsPalindrome = tg.Reflect[bool]{
Name: "isPalindrome",
ResultKey: keyResult,
Fn: func(input, reversed string) (bool, error) {
return input == reversed, nil
},
Depends: []any{keyInput, keyReversed},
}.Locate()
var graphIsPalindrome = tg.Must(
tg.New("example_graph", tg.WithTasks(taskReverseInput, taskIsPalindrome)),
)
func Example() {
res, err := graphIsPalindrome.Run(context.Background(), keyInput.Bind("racecar"))
if err != nil {
log.Fatal(err)
}
val, err := keyResult.Get(res)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%t\n", val)
// Output:
// true
}