Skip to content

Commit 0213563

Browse files
committed
fn: update helper funcs for option and either
1 parent 8eff420 commit 0213563

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

fn/either.go

+22
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,25 @@ func (e Either[L, R]) IsLeft() bool {
3535
func (e Either[L, R]) IsRight() bool {
3636
return e.right.IsSome()
3737
}
38+
39+
// MapLeft maps the left value of the Either to a new value.
40+
func MapLeft[L any, R any, O any](f func(L) O) func(Either[L, R]) Option[O] {
41+
return func(e Either[L, R]) Option[O] {
42+
if e.IsLeft() {
43+
return MapOption(f)(e.left)
44+
}
45+
46+
return None[O]()
47+
}
48+
}
49+
50+
// MapRight maps the right value of the Either to a new value.
51+
func MapRight[L any, R any, O any](f func(R) O) func(Either[L, R]) Option[O] {
52+
return func(e Either[L, R]) Option[O] {
53+
if e.IsRight() {
54+
return MapOption(f)(e.right)
55+
}
56+
57+
return None[O]()
58+
}
59+
}

fn/option.go

+52-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@ func Some[A any](a A) Option[A] {
1717
}
1818
}
1919

20-
// None trivially constructs an empty option
20+
// None trivially constructs an empty option.
2121
//
2222
// None : Option[A].
2323
func None[A any]() Option[A] {
2424
return Option[A]{}
2525
}
2626

27+
// MaybeSome constructs an option from a pointer.
28+
//
29+
// MaybeSome : *A -> Option[A].
30+
func MaybeSome[A any](a *A) Option[A] {
31+
if a == nil {
32+
return None[A]()
33+
}
34+
35+
return Some[A](*a)
36+
}
37+
2738
// ElimOption is the universal Option eliminator. It can be used to safely
2839
// handle all possible values inside the Option by supplying two continuations.
2940
//
@@ -48,6 +59,33 @@ func (o Option[A]) UnwrapOr(a A) A {
4859
return a
4960
}
5061

62+
// UnwrapPtr is used to extract a reference to a value from an option, and we
63+
// supply an empty pointer in the case when the Option is empty.
64+
func (o Option[A]) UnwrapToPtr() *A {
65+
var v *A
66+
o.WhenSome(func(a A) {
67+
v = &a
68+
})
69+
70+
return v
71+
}
72+
73+
// UnwrapOrFunc is used to extract a value from an option, and we supply a
74+
// thunk to be evaluated in the case when the Option is empty.
75+
func (o Option[A]) UnwrapOrFunc(f func() A) A {
76+
return ElimOption(o, f, func(a A) A { return a })
77+
}
78+
79+
// UnwrapOrFuncErr is used to extract a value from an option, and we supply a
80+
// thunk to be evaluated in the case when the Option is empty.
81+
func (o Option[A]) UnwrapOrFuncErr(f func() (A, error)) (A, error) {
82+
if o.isSome {
83+
return o.some, nil
84+
}
85+
86+
return f()
87+
}
88+
5189
// WhenSome is used to conditionally perform a side-effecting function that
5290
// accepts a value of the type that parameterizes the option. If this function
5391
// performs no side effects, WhenSome is useless.
@@ -117,6 +155,19 @@ func MapOption[A, B any](f func(A) B) func(Option[A]) Option[B] {
117155
}
118156
}
119157

158+
// MapOptionZ transforms a pure function A -> B into one that will operate
159+
// inside the Option context. Unlike MapOption, this function will return the
160+
// default/zero argument of the return type if the Option is empty.
161+
func MapOptionZ[A, B any](o Option[A], f func(A) B) B {
162+
var zero B
163+
164+
if o.IsNone() {
165+
return zero
166+
}
167+
168+
return f(o.some)
169+
}
170+
120171
// LiftA2Option transforms a pure function (A, B) -> C into one that will
121172
// operate in an Option context. For the returned function, if either of its
122173
// arguments are None, then the result will be None.

0 commit comments

Comments
 (0)