From 386f341f39dd6c18049343f04ba3c61b006d2c9d Mon Sep 17 00:00:00 2001 From: Joseph Fourny Date: Thu, 21 Mar 2024 20:40:22 -0400 Subject: [PATCH] Split optional.go into more files. --- .idea/.gitignore | 2 + pkg/opt/none.go | 47 +++++++++++++ pkg/opt/optional.go | 157 ++++++++++---------------------------------- pkg/opt/some.go | 52 +++++++++++++++ 4 files changed, 135 insertions(+), 123 deletions(-) create mode 100644 pkg/opt/none.go create mode 100644 pkg/opt/some.go diff --git a/.idea/.gitignore b/.idea/.gitignore index 5b38e1f..ad71afe 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -7,3 +7,5 @@ /modules.xml /papaya.iml /vcs.xml +# GitHub Copilot persisted chat sessions +/copilot/chatSessions diff --git a/pkg/opt/none.go b/pkg/opt/none.go new file mode 100644 index 0000000..94be664 --- /dev/null +++ b/pkg/opt/none.go @@ -0,0 +1,47 @@ +package opt + +// None represents the absence of a value. +type None[V any] struct { +} + +// Assert that None[V] implements Optional[V] +var _ Optional[any] = None[any]{} + +func (n None[V]) Present() bool { + return false +} + +func (n None[V]) Get() (V, bool) { + var zero V + return zero, false +} + +func (n None[V]) GetOrZero() V { + var zero V + return zero +} + +func (n None[V]) GetOrDefault(defaultValue V) V { + return defaultValue +} + +func (n None[V]) GetOrFunc(f func() V) V { + return f() +} + +func (n None[V]) Filter(_ func(V) bool) Optional[V] { + return n +} + +func (n None[V]) IfPresent(_ func(V)) bool { + return false +} + +func (n None[V]) IfPresentElse(_ func(V), f func()) bool { + f() + return false +} + +func (n None[V]) String() string { + return "None" +} diff --git a/pkg/opt/optional.go b/pkg/opt/optional.go index 36419ee..77c5a3f 100644 --- a/pkg/opt/optional.go +++ b/pkg/opt/optional.go @@ -2,6 +2,40 @@ package opt import "fmt" +// Optional is a generic type that takes one type parameter V and represents a value that may or may not be present. +// It is similar to Java's Optional type. +type Optional[V any] interface { + fmt.Stringer + + // Present returns true if the Optional contains a value, false otherwise. + Present() bool + + // Get returns the value contained in the Optional and an indicator of whether the Optional is empty. + // If the Optional is empty, the value returned is the zero value of type V. + Get() (V, bool) + + // GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty. + GetOrZero() V + + // GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty. + GetOrDefault(defaultValue V) V + + // GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty. + GetOrFunc(func() V) V + + // IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty. + // Returns true if the function was called, false otherwise. + IfPresent(func(V)) bool + + // IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise. + // Returns true if the first function was called, false otherwise. + IfPresentElse(func(V), func()) bool + + // Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value. + // If the Optional is empty, an empty Optional is returned. + Filter(func(V) bool) Optional[V] +} + // Empty returns an empty Optional (None). func Empty[V any]() Optional[V] { return None[V]{} @@ -103,126 +137,3 @@ func MaybeMap[V, U any](o Optional[V], mapper func(V) (U, bool)) Optional[U] { } return Empty[U]() } - -// Optional is a generic type that takes one type parameter V and represents a value that may or may not be present. -// It is similar to Java's Optional type. -type Optional[V any] interface { - fmt.Stringer - - // Present returns true if the Optional contains a value, false otherwise. - Present() bool - - // Get returns the value contained in the Optional and an indicator of whether the Optional is empty. - // If the Optional is empty, the value returned is the zero value of type V. - Get() (V, bool) - - // GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty. - GetOrZero() V - - // GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty. - GetOrDefault(defaultValue V) V - - // GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty. - GetOrFunc(func() V) V - - // IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty. - // Returns true if the function was called, false otherwise. - IfPresent(func(V)) bool - - // IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise. - // Returns true if the first function was called, false otherwise. - IfPresentElse(func(V), func()) bool - - // Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value. - // If the Optional is empty, an empty Optional is returned. - Filter(func(V) bool) Optional[V] -} - -// None is an Optional that represents the absence of a value. -type None[V any] struct { -} - -func (n None[V]) Present() bool { - return false -} - -func (n None[V]) Get() (V, bool) { - var zero V - return zero, false -} - -func (n None[V]) GetOrZero() V { - var zero V - return zero -} - -func (n None[V]) GetOrDefault(defaultValue V) V { - return defaultValue -} - -func (n None[V]) GetOrFunc(f func() V) V { - return f() -} - -func (n None[V]) Filter(_ func(V) bool) Optional[V] { - return n -} - -func (n None[V]) IfPresent(_ func(V)) bool { - return false -} - -func (n None[V]) IfPresentElse(_ func(V), f func()) bool { - f() - return false -} - -func (n None[V]) String() string { - return "None" -} - -// Some is an Optional that represents the presence of a value. -type Some[V any] struct { - Value V -} - -func (s Some[V]) Present() bool { - return true -} - -func (s Some[V]) Get() (V, bool) { - return s.Value, true -} - -func (s Some[V]) GetOrZero() V { - return s.Value -} - -func (s Some[V]) GetOrDefault(_ V) V { - return s.Value -} - -func (s Some[V]) GetOrFunc(_ func() V) V { - return s.Value -} - -func (s Some[V]) Filter(f func(V) bool) Optional[V] { - if f(s.Value) { - return s - } - return Empty[V]() -} - -func (s Some[V]) IfPresent(f func(V)) bool { - f(s.Value) - return true -} - -func (s Some[V]) IfPresentElse(f func(V), _ func()) bool { - f(s.Value) - return true -} - -func (s Some[V]) String() string { - return fmt.Sprintf("Some(%#v)", s.Value) -} diff --git a/pkg/opt/some.go b/pkg/opt/some.go new file mode 100644 index 0000000..82af209 --- /dev/null +++ b/pkg/opt/some.go @@ -0,0 +1,52 @@ +package opt + +import "fmt" + +// Some represents the presence of a value. +type Some[V any] struct { + Value V +} + +// Assert that Some[V] implements Optional[V] +var _ Optional[any] = Some[any]{} + +func (s Some[V]) Present() bool { + return true +} + +func (s Some[V]) Get() (V, bool) { + return s.Value, true +} + +func (s Some[V]) GetOrZero() V { + return s.Value +} + +func (s Some[V]) GetOrDefault(_ V) V { + return s.Value +} + +func (s Some[V]) GetOrFunc(_ func() V) V { + return s.Value +} + +func (s Some[V]) Filter(f func(V) bool) Optional[V] { + if f(s.Value) { + return s + } + return Empty[V]() +} + +func (s Some[V]) IfPresent(f func(V)) bool { + f(s.Value) + return true +} + +func (s Some[V]) IfPresentElse(f func(V), _ func()) bool { + f(s.Value) + return true +} + +func (s Some[V]) String() string { + return fmt.Sprintf("Some(%#v)", s.Value) +}