From e739523ac1f926007fcfd8d82e5559df37cc6c6b Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 11 Jan 2025 16:13:00 +0100 Subject: add options.Map() --- options/options.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'options/options.go') diff --git a/options/options.go b/options/options.go index 9754e2d..bfae710 100644 --- a/options/options.go +++ b/options/options.go @@ -21,10 +21,19 @@ func FromPointer[T any](value *T) Option[T] { } } -// IsNoneOrZero returns whether the Option is either empty, or contains a zero value. +// IsNoneOrZero returns whether o is either empty, or contains a zero value. func IsNoneOrZero[T comparable](o Option[T]) bool { return o.IsNoneOr(func(value T) bool { var zero T return zero == value }) } + +// Map applies the given function to the value contained in o, if there is one. +func Map[T, U any](o Option[T], mapping func(T) U) Option[U] { + if t, ok := o.Unpack(); ok { + return Some(mapping(t)) + } else { + return None[U]() + } +} -- cgit v1.2.3