.isnothing()

master
Charles Iliya Krempeaux 2023-09-24 05:24:56 +09:00
parent 58bf264aa9
commit b434dd95d3
3 changed files with 9 additions and 5 deletions

2
map.go
View File

@ -39,7 +39,7 @@ package opt
// //
// // result2 == opt.Nothing[int]() // // result2 == opt.Nothing[int]()
func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] { func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] {
if !op.something { if op.isnothing() {
return Nothing[T2]() return Nothing[T2]()
} }

View File

@ -107,7 +107,7 @@ func Something[T any](value T) Optional[T] {
// //
// op3 = op3.Filter(fn) // op3 = op3.Filter(fn)
func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] { func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
if !receiver.something { if receiver.isnothing() {
return Nothing[T]() return Nothing[T]()
} }
@ -118,6 +118,10 @@ func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
return receiver return receiver
} }
func (receiver Optional[T]) isnothing() bool {
return !receiver.something
}
// Get returns the value inside of the optional-type if it is holding something. // Get returns the value inside of the optional-type if it is holding something.
// //
// Example usage: // Example usage:
@ -162,7 +166,7 @@ func (receiver Optional[T]) Get() (T, bool) {
// // Output: // // Output:
// // op = opt.Nothing[uint8]() // // op = opt.Nothing[uint8]()
func (receiver Optional[T]) GoString() string { func (receiver Optional[T]) GoString() string {
if !receiver.something { if receiver.isnothing() {
return fmt.Sprintf("opt.Nothing[%T]()", receiver.value) return fmt.Sprintf("opt.Nothing[%T]()", receiver.value)
} }
@ -183,7 +187,7 @@ func (receiver Optional[T]) GoString() string {
// //@TODO // //@TODO
// }) // })
func (receiver Optional[T]) WhenNothing(fn func()) { func (receiver Optional[T]) WhenNothing(fn func()) {
if !receiver.something { if receiver.isnothing() {
fn() fn()
} }
} }

View File

@ -36,7 +36,7 @@ package opt
// //
// // result2 == opt.Nothing[byte]() // // result2 == opt.Nothing[byte]()
func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] { func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] {
if !op.something { if op.isnothing() {
return Nothing[T2]() return Nothing[T2]()
} }