This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_map.go
More file actions
46 lines (41 loc) · 1.6 KB
/
4_map.go
File metadata and controls
46 lines (41 loc) · 1.6 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
package mu
// Map applies a function to the contained value, transforming T to T.
// If the option is [None], it returns `None[T]`.
// For type-changing transformations (T→U), use [pkg/github.com/appthrust/mu/muo.MapTo] instead. See package docs for details.
func (o Option[T]) Map(f func(T) T) Option[T] {
if val, ok := o.Unwrap(); ok {
return Some(f(val))
}
return None[T]()
}
// MapLeft applies a function to a [Left] value, leaving a [Right] value untouched.
// For type-changing transformations (L→L2), use [pkg/github.com/appthrust/mu/mue.MapLeftTo] instead. See package docs for details.
func (e Either[L, R]) MapLeft(f func(L) L) Either[L, R] {
if val, ok := e.UnwrapLeft(); ok {
return Left[L, R](f(val))
}
return e
}
// Map applies a function to a [Right] value, leaving a [Left] value untouched.
// For type-changing transformations (R→R2), use [pkg/github.com/appthrust/mu/mue.MapTo] instead. See package docs for details.
func (e Either[L, R]) Map(f func(R) R) Either[L, R] {
if val, ok := e.Unwrap(); ok {
return Right[L, R](f(val))
}
return e
}
// Map applies a function to a contained [Ok] value, leaving an [Err] value untouched.
// For type-changing transformations (T→U), use [pkg/github.com/appthrust/mu/mur.MapTo] instead. See package docs for details.
func (r Result[T]) Map(f func(T) T) Result[T] {
if val, ok := r.Unwrap(); ok {
return Ok(f(val))
}
return r
}
// MapErr applies a function to a contained [Err] value, leaving an [Ok] value untouched.
func (r Result[T]) MapErr(f func(error) error) Result[T] {
if err, ok := r.UnwrapErr(); ok {
return Err[T](f(err))
}
return r
}