• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fogfish / golem / 14206101271

01 Apr 2025 09:10PM UTC coverage: 81.312% (-0.4%) from 81.746%
14206101271

Pull #68

github

fogfish
increase test coverage
Pull Request #68: abstract pipe api via F[A, B]

159 of 189 new or added lines in 4 files covered. (84.13%)

2 existing lines in 1 file now uncovered.

1549 of 1905 relevant lines covered (81.31%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.22
/pipe/function.go
1
//
2
// Copyright (C) 2022 - 2025 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/fogfish/golem
7
//
8

9
package pipe
10

11
import "context"
12

13
// Pure effect over category A ⟼ B
14
type E[A, B any] = func(A) B
15

16
// Either effect over category A ⟼ (B, error)
17
type EitherE[A, B any] = func(A) (B, error)
18

19
// Arrow over functor 𝓕: A ⟼ B
20
type Arrow[A, B any] = func(context.Context, A, chan<- B) error
21

22
// Go channel morphism 𝑓: A ⟼ B
23
type F[A, B any] interface {
24
        apply(A) (B, error)
25
        errch(cap int) chan error
26
        catch(context.Context, error, chan<- error) bool
27
}
28

29
// Lift pure effect into morphism 𝑓: A ⟼ B
30
func Pure[A, B any](f E[A, B]) F[A, B] {
1✔
31
        ff := func(a A) (B, error) { return f(a), nil }
2✔
32
        return pure[A, B](ff)
1✔
33
}
34

35
// Lift either effect into morphism 𝑓: A ⟼ B
36
// The failure of morphism causes the failure of channel, aborts the computation.
37
func Lift[A, B any](f EitherE[A, B]) F[A, B] {
1✔
38
        return pure[A, B](f)
1✔
39
}
1✔
40

41
type pure[A, B any] EitherE[A, B]
42

43
//lint:ignore U1000 false positive
44
func (f pure[A, B]) apply(a A) (B, error) {
1✔
45
        return EitherE[A, B](f)(a)
1✔
46
}
1✔
47

48
//lint:ignore U1000 false positive
49
func (f pure[A, B]) errch(_ int) chan error {
1✔
50
        return make(chan error, 1)
1✔
51
}
1✔
52

53
//lint:ignore U1000 false positive
54
func (f pure[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
55
        exx <- err
1✔
56
        return false
1✔
57
}
1✔
58

59
// Lift either effect into morphism 𝑓: A ⟼ B
60
// The failure of morphism causes the failure of step, continues the computation.
61
func Try[A, B any](f EitherE[A, B]) F[A, B] {
1✔
62
        return try[A, B](f)
1✔
63
}
1✔
64

65
type try[A, B any] EitherE[A, B]
66

67
//lint:ignore U1000 false positive
68
func (f try[A, B]) apply(a A) (B, error) {
1✔
69
        return EitherE[A, B](f)(a)
1✔
70
}
1✔
71

72
//lint:ignore U1000 false positive
73
func (f try[A, B]) errch(cap int) chan error {
1✔
74
        return make(chan error, cap)
1✔
75
}
1✔
76

77
//lint:ignore U1000 false positive
78
func (f try[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
79
        select {
1✔
80
        case exx <- err:
1✔
NEW
81
        case <-ctx.Done():
×
NEW
82
                return false
×
83
        }
84
        return true
1✔
85
}
86

87
//------------------------------------------------------------------------------
88

89
// Go channel functor 𝓕: A ⟼ B
90
type FF[A, B any] interface {
91
        apply(context.Context, A, chan<- B) error
92
        errch(cap int) chan error
93
        catch(context.Context, error, chan<- error) bool
94
}
95

96
// Lift arrow into functor morphism 𝓕: A ⟼ B
97
// The failure of morphism causes the failure of channel, aborts the computation.
98
func LiftF[A, B any](f Arrow[A, B]) FF[A, B] {
1✔
99
        return puref[A, B](f)
1✔
100
}
1✔
101

102
type puref[A, B any] Arrow[A, B]
103

104
//lint:ignore U1000 false positive
105
func (f puref[A, B]) apply(ctx context.Context, a A, b chan<- B) error {
1✔
106
        return Arrow[A, B](f)(ctx, a, b)
1✔
107
}
1✔
108

109
//lint:ignore U1000 false positive
110
func (f puref[A, B]) errch(_ int) chan error {
1✔
111
        return make(chan error, 1)
1✔
112
}
1✔
113

114
//lint:ignore U1000 false positive
115
func (f puref[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
116
        exx <- err
1✔
117
        return false
1✔
118
}
1✔
119

120
// Lift arrow into functor morphism 𝓕: A ⟼ B
121
// The failure of morphism causes the failure of step, continues the computation.
122
func TryF[A, B any](f Arrow[A, B]) FF[A, B] {
1✔
123
        return tryf[A, B](f)
1✔
124
}
1✔
125

126
type tryf[A, B any] Arrow[A, B]
127

128
//lint:ignore U1000 false positive
129
func (f tryf[A, B]) apply(ctx context.Context, a A, b chan<- B) error {
1✔
130
        return Arrow[A, B](f)(ctx, a, b)
1✔
131
}
1✔
132

133
//lint:ignore U1000 false positive
134
func (f tryf[A, B]) errch(cap int) chan error {
1✔
135
        return make(chan error, cap)
1✔
136
}
1✔
137

138
//lint:ignore U1000 false positive
139
func (f tryf[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
140
        select {
1✔
141
        case exx <- err:
1✔
NEW
142
        case <-ctx.Done():
×
NEW
143
                return false
×
144
        }
145
        return true
1✔
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc