-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity_test.go
59 lines (45 loc) · 1.25 KB
/
identity_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
package monad
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestIdentityLeftIdentityLaw(t *testing.T) {
t.Parallel()
is := require.New(t)
f := func(x int) Identity[int] {
return NewIdentity(x + 1)
}
a := 3
// Test left identity: return a >>= f is the same as f applied to a
leftHandSide := NewIdentity(a).FlatMap(f).Value()
rightHandSide := f(a).Value()
is.Equal(leftHandSide, rightHandSide)
}
func TestIdentityRightIdentityLaw(t *testing.T) {
t.Parallel()
is := require.New(t)
m := NewIdentity(3)
// Test right identity: m >>= return is no different than just m
leftHandSide := m.FlatMap(func(x int) Identity[int] {
return NewIdentity(x)
}).Value()
rightHandSide := m.Value()
is.Equal(leftHandSide, rightHandSide)
}
func TestIdentityAssociativityLaw(t *testing.T) {
t.Parallel()
is := require.New(t)
m := NewIdentity(3)
f := func(x int) Identity[int] {
return NewIdentity(x + 1)
}
g := func(x int) Identity[int] {
return NewIdentity(x * 2)
}
// Test associativity: (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)
leftHandSide := m.FlatMap(f).FlatMap(g).Value()
rightHandSide := m.FlatMap(func(x int) Identity[int] {
return f(x).FlatMap(g)
}).Value()
is.Equal(leftHandSide, rightHandSide)
}