• 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

64.62
/pipe/fork/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 fork
10

11
import (
12
        "context"
13

14
        "github.com/fogfish/golem/pipe/v2"
15
)
16

17
// Pure effect over category A ⟼ B
18
type E[A, B any] = func(A) B
19

20
// Either effect over category A ⟼ (B, error)
21
type EitherE[A, B any] = func(A) (B, error)
22

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

26
// Go channel morphism 𝑓: A ⟼ B
27
type F[A, B any] interface {
28
        apply(A) (B, error)
29
        errch(cap int) chan error
30
        catch(context.Context, error, chan<- error) bool
31
        pipef() pipe.F[A, B]
32
}
33

34
// Lift pure effect into morphism 𝑓: A ⟼ B
35
func Pure[A, B any](f E[A, B]) F[A, B] {
1✔
36
        ff := func(a A) (B, error) { return f(a), nil }
2✔
37
        return pure[A, B](ff)
1✔
38
}
39

40
// Lift either effect into morphism 𝑓: A ⟼ B
41
// The failure of morphism causes the failure of channel, aborts the computation.
42
func Lift[A, B any](f EitherE[A, B]) F[A, B] {
1✔
43
        return pure[A, B](f)
1✔
44
}
1✔
45

46
type pure[A, B any] EitherE[A, B]
47

48
//lint:ignore U1000 false positive
49
func (f pure[A, B]) apply(a A) (B, error) {
1✔
50
        return EitherE[A, B](f)(a)
1✔
51
}
1✔
52

53
//lint:ignore U1000 false positive
NEW
54
func (f pure[A, B]) errch(_ int) chan error {
×
NEW
55
        return make(chan error, 1)
×
NEW
56
}
×
57

58
//lint:ignore U1000 false positive
59
func (f pure[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
60
        exx <- err
1✔
61
        return false
1✔
62
}
1✔
63

64
//lint:ignore U1000 false positive
65
func (f pure[A, B]) pipef() pipe.F[A, B] {
1✔
66
        return pipe.Lift(f)
1✔
67
}
1✔
68

69
// Lift either effect into morphism 𝑓: A ⟼ B
70
// The failure of morphism causes the failure of step, continues the computation.
71
func Try[A, B any](f EitherE[A, B]) F[A, B] {
1✔
72
        return try[A, B](f)
1✔
73
}
1✔
74

75
type try[A, B any] EitherE[A, B]
76

77
//lint:ignore U1000 false positive
NEW
78
func (f try[A, B]) apply(a A) (B, error) {
×
NEW
79
        return EitherE[A, B](f)(a)
×
NEW
80
}
×
81

82
//lint:ignore U1000 false positive
NEW
83
func (f try[A, B]) errch(cap int) chan error {
×
NEW
84
        return make(chan error, cap)
×
NEW
85
}
×
86

87
//lint:ignore U1000 false positive
NEW
88
func (f try[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
×
NEW
89
        select {
×
NEW
90
        case exx <- err:
×
NEW
91
        case <-ctx.Done():
×
NEW
92
                return false
×
93
        }
NEW
94
        return true
×
95
}
96

97
//lint:ignore U1000 false positive
98
func (f try[A, B]) pipef() pipe.F[A, B] {
1✔
99
        return pipe.Try(f)
1✔
100
}
1✔
101

102
//------------------------------------------------------------------------------
103

104
// Go channel functor 𝓕: A ⟼ B
105
type FF[A, B any] interface {
106
        apply(context.Context, A, chan<- B) error
107
        errch(cap int) chan error
108
        catch(context.Context, error, chan<- error) bool
109
}
110

111
// Lift arrow into functor morphism 𝓕: A ⟼ B
112
// The failure of morphism causes the failure of channel, aborts the computation.
113
func LiftF[A, B any](f Arrow[A, B]) FF[A, B] {
1✔
114
        return puref[A, B](f)
1✔
115
}
1✔
116

117
type puref[A, B any] Arrow[A, B]
118

119
//lint:ignore U1000 false positive
120
func (f puref[A, B]) apply(ctx context.Context, a A, b chan<- B) error {
1✔
121
        return Arrow[A, B](f)(ctx, a, b)
1✔
122
}
1✔
123

124
//lint:ignore U1000 false positive
NEW
125
func (f puref[A, B]) errch(_ int) chan error {
×
NEW
126
        return make(chan error, 1)
×
NEW
127
}
×
128

129
//lint:ignore U1000 false positive
130
func (f puref[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
131
        exx <- err
1✔
132
        return false
1✔
133
}
1✔
134

135
// Lift arrow into functor morphism 𝓕: A ⟼ B
136
// The failure of morphism causes the failure of step, continues the computation.
137
func TryF[A, B any](f Arrow[A, B]) FF[A, B] {
1✔
138
        return tryf[A, B](f)
1✔
139
}
1✔
140

141
type tryf[A, B any] Arrow[A, B]
142

143
//lint:ignore U1000 false positive
144
func (f tryf[A, B]) apply(ctx context.Context, a A, b chan<- B) error {
1✔
145
        return Arrow[A, B](f)(ctx, a, b)
1✔
146
}
1✔
147

148
//lint:ignore U1000 false positive
NEW
149
func (f tryf[A, B]) errch(cap int) chan error {
×
NEW
150
        return make(chan error, cap)
×
NEW
151
}
×
152

153
//lint:ignore U1000 false positive
154
func (f tryf[A, B]) catch(ctx context.Context, err error, exx chan<- error) bool {
1✔
155
        select {
1✔
156
        case exx <- err:
1✔
NEW
157
        case <-ctx.Done():
×
NEW
158
                return false
×
159
        }
160
        return true
1✔
161
}
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

© 2026 Coveralls, Inc