-
Notifications
You must be signed in to change notification settings - Fork 1
/
map2.go
66 lines (55 loc) · 1.61 KB
/
map2.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
64
65
66
package incr
import (
"context"
"fmt"
)
// Map2 applies a function to a given input incremental and returns
// a new incremental of the output type of that function.
func Map2[A, B, C any](scope Scope, a Incr[A], b Incr[B], fn func(A, B) C) Incr[C] {
return Map2Context(scope, a, b, func(_ context.Context, a A, b B) (C, error) {
return fn(a, b), nil
})
}
// Map2Context applies a function that accepts a context and returns an error,
// to a given input incremental and returns a new incremental of
// the output type of that function.
func Map2Context[A, B, C any](scope Scope, a Incr[A], b Incr[B], fn func(context.Context, A, B) (C, error)) Incr[C] {
return WithinScope(scope, &map2Incr[A, B, C]{
n: NewNode("map2"),
a: a,
b: b,
fn: fn,
parents: []INode{a, b},
})
}
var (
_ Incr[string] = (*map2Incr[int, int, string])(nil)
_ INode = (*map2Incr[int, int, string])(nil)
_ IStabilize = (*map2Incr[int, int, string])(nil)
_ fmt.Stringer = (*map2Incr[int, int, string])(nil)
)
type map2Incr[A, B, C any] struct {
n *Node
a Incr[A]
b Incr[B]
fn func(context.Context, A, B) (C, error)
val C
parents []INode
}
func (m2n *map2Incr[A, B, C]) Parents() []INode {
return m2n.parents
}
func (m2n *map2Incr[A, B, C]) Node() *Node { return m2n.n }
func (m2n *map2Incr[A, B, C]) Value() C { return m2n.val }
func (m2n *map2Incr[A, B, C]) Stabilize(ctx context.Context) (err error) {
var val C
val, err = m2n.fn(ctx, m2n.a.Value(), m2n.b.Value())
if err != nil {
return
}
m2n.val = val
return nil
}
func (m2n *map2Incr[A, B, C]) String() string {
return m2n.n.String()
}