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

fogfish / golem / 14211705386

02 Apr 2025 04:44AM UTC coverage: 81.207% (-0.1%) from 81.312%
14211705386

push

github

web-flow
make Apply public in F[A, B] (#70)

20 of 21 new or added lines in 4 files covered. (95.24%)

2 existing lines in 1 file now uncovered.

1547 of 1905 relevant lines covered (81.21%)

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
func (f pure[A, B]) Apply(a A) (B, error) {
1✔
49
        return EitherE[A, B](f)(a)
1✔
50
}
1✔
51

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

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

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

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

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

NEW
76
func (f try[A, B]) Apply(a A) (B, error) {
×
77
        return EitherE[A, B](f)(a)
×
78
}
×
79

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

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

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

100
//------------------------------------------------------------------------------
101

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

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

115
type puref[A, B any] Arrow[A, B]
116

117
func (f puref[A, B]) Apply(ctx context.Context, a A, b chan<- B) error {
1✔
118
        return Arrow[A, B](f)(ctx, a, b)
1✔
119
}
1✔
120

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

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

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

138
type tryf[A, B any] Arrow[A, B]
139

140
func (f tryf[A, B]) Apply(ctx context.Context, a A, b chan<- B) error {
1✔
141
        return Arrow[A, B](f)(ctx, a, b)
1✔
142
}
1✔
143

144
//lint:ignore U1000 false positive
145
func (f tryf[A, B]) errch(cap int) chan error {
×
146
        return make(chan error, cap)
×
147
}
×
148

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