-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinators.go
More file actions
76 lines (67 loc) · 1.82 KB
/
combinators.go
File metadata and controls
76 lines (67 loc) · 1.82 KB
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
67
68
69
70
71
72
73
74
75
76
package result
// AndThen chains operations that return Results (monadic bind/flatMap)
// This is the core operation for railway-oriented programming.
//
// If the result is Ok, the function f is applied to the value and its result is returned.
// If the result is Err, the error is propagated without calling f.
//
// Context (op, meta) is preserved and propagated through the chain.
func (r Result[T]) AndThen(f func(T) Result[T]) Result[T] {
if r.IsErr() {
return r
}
next := f(r.value)
// Preserve context if next doesn't have it
if next.op == "" && r.op != "" {
next.op = r.op
}
if next.meta == nil && r.meta != nil {
next.meta = r.meta
}
return next
}
// AndThenMap chains operations that return Results of different types.
// This is AndThen but with a type transformation.
//
// Example:
//
// result.Ok(42).AndThenMap(func(n int) result.Result[string] {
// return result.Ok(fmt.Sprintf("number: %d", n))
// })
func AndThenMap[T, U any](r Result[T], f func(T) Result[U]) Result[U] {
if r.IsErr() {
return Result[U]{
err: r.err,
op: r.op,
meta: r.meta,
}
}
next := f(r.value)
// Preserve context
if next.op == "" && r.op != "" {
next.op = r.op
}
if next.meta == nil && r.meta != nil {
next.meta = r.meta
}
return next
}
// OrElse provides an alternative Result if this one is Err.
// If the result is Ok, it is returned unchanged.
// If the result is Err, the function f is called with the error to produce an alternative.
//
// This is useful for fallback chains:
//
// cache.Get(id).
// OrElse(func(err error) result.Result[User] {
// return db.FindByID(id)
// }).
// OrElse(func(err error) result.Result[User] {
// return result.Ok(defaultUser)
// })
func (r Result[T]) OrElse(f func(error) Result[T]) Result[T] {
if r.IsOk() {
return r
}
return f(r.err)
}