muo

package
v0.0.0-...-f1dd236 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package muo provides type-changing utility functions for `Option[T]`. These functions enable transformations where the contained type changes (T→U), which cannot be expressed as methods due to Go's type system limitations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlatMapTo

func FlatMapTo[T any, U any](o mu.Option[T], f func(T) mu.Option[U]) mu.Option[U]

FlatMapTo applies the function f to the contained value of `Option[T]` if it exists, where f returns `Option[U]`. If the option is [None], it returns None[U]. This enables type-changing chain operations (T→U). See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/muo"
)

func main() {
	// FlatMapTo with Some value (int to Option[string])
	someInt := mu.Some(42)
	someStr := muo.FlatMapTo(someInt, func(x int) mu.Option[string] {
		if x > 0 {
			return mu.Some(strconv.Itoa(x))
		}
		return mu.None[string]()
	})
	fmt.Println("Some(42) flat mapped to string:", someStr.OrZero())

	// FlatMapTo returning None
	someNegative := mu.Some(-10)
	result := muo.FlatMapTo(someNegative, func(x int) mu.Option[string] {
		if x > 0 {
			return mu.Some(strconv.Itoa(x))
		}
		return mu.None[string]()
	})
	fmt.Println("Some(-10) flat mapped (negative):", result.IsNone())

	// FlatMapTo with None value
	noneInt := mu.None[int]()
	noneStr := muo.FlatMapTo(noneInt, func(x int) mu.Option[string] {
		return mu.Some(strconv.Itoa(x))
	})
	fmt.Println("None flat mapped to string:", noneStr.IsNone())

}
Output:

Some(42) flat mapped to string: 42
Some(-10) flat mapped (negative): true
None flat mapped to string: true

func Fold

func Fold[T any, U any](o mu.Option[T], onSome func(T) U, onNone func() U) U

Fold applies the function f to the contained value of `Option[T]` if it exists, returning U. If the option is [None], it returns the result of the onNone function. This enables type-changing pattern matching (T→U). See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/muo"
)

func main() {
	// Fold with Some value (int to string)
	someInt := mu.Some(42)
	result := muo.Fold(someInt,
		func(x int) string { return "value: " + strconv.Itoa(x) }, // onSome
		func() string { return "no value" },                       // onNone
	)
	fmt.Println("Some(42) folded:", result)

	// Fold with None value
	noneInt := mu.None[int]()
	result2 := muo.Fold(noneInt,
		func(x int) string { return "value: " + strconv.Itoa(x) }, // onSome
		func() string { return "no value" },                       // onNone
	)
	fmt.Println("None folded:", result2)

	// Fold to different type (int to bool)
	result3 := muo.Fold(someInt,
		func(x int) bool { return x > 0 }, // onSome
		func() bool { return false },      // onNone
	)
	fmt.Println("Some(42) folded to bool:", result3)

}
Output:

Some(42) folded: value: 42
None folded: no value
Some(42) folded to bool: true

func From

func From[T any](value T, some bool) mu.Option[T]

From converts a value to an `Option[T]`. If some is true, it returns `Some(value)`. If some is false, it returns [None].

Example
package main

import (
	"fmt"

	"github.com/appthrust/mu/muo"
)

func main() {
	// From with true condition
	someValue := muo.From(42, true)
	fmt.Println("From(42, true):", someValue.OrZero())

	// From with false condition
	noneValue := muo.From(42, false)
	fmt.Println("From(42, false):", noneValue.IsNone())

	// From with different types
	stringValue := muo.From("hello", true)
	fmt.Println("From(\"hello\", true):", stringValue.OrZero())

	emptyString := muo.From("hello", false)
	fmt.Println("From(\"hello\", false):", emptyString.IsNone())

	// Practical example with map lookup pattern
	m := map[string]int{"exists": 42}
	value, ok := m["exists"]
	optionExists := muo.From(value, ok)
	fmt.Println("From map lookup (exists):", optionExists.OrZero())

	value2, ok2 := m["missing"]
	optionMissing := muo.From(value2, ok2)
	fmt.Println("From map lookup (missing):", optionMissing.IsNone())

}
Output:

From(42, true): 42
From(42, false): true
From("hello", true): hello
From("hello", false): true
From map lookup (exists): 42
From map lookup (missing): true

func FromMap

func FromMap[K comparable, V any](m map[K]V, k K) mu.Option[V]

FromMap converts a value from a map to an `Option[V]`. If the key is not found, it returns [None]. If the key is found, it returns `Some(value)`.

Example
package main

import (
	"fmt"

	"github.com/appthrust/mu/muo"
)

func main() {
	// FromMap with existing key
	m := map[string]int{"a": 1, "b": 2, "c": 3}
	valueA := muo.FromMap(m, "a")
	fmt.Println("FromMap existing key 'a':", valueA.OrZero())

	// FromMap with non-existing key
	valueD := muo.FromMap(m, "d")
	fmt.Println("FromMap non-existing key 'd':", valueD.IsNone())

	// FromMap with different types
	users := map[int]string{1: "alice", 2: "bob"}
	user1 := muo.FromMap(users, 1)
	fmt.Println("FromMap user 1:", user1.OrZero())

	user99 := muo.FromMap(users, 99)
	fmt.Println("FromMap user 99:", user99.IsNone())

}
Output:

FromMap existing key 'a': 1
FromMap non-existing key 'd': true
FromMap user 1: alice
FromMap user 99: true

func FromSlice

func FromSlice[T any](s []T) mu.Option[T]

FromSlice converts a first element of a slice to an `Option[T]`. If the slice is empty, it returns [None]. If the slice is not empty, it returns `Some(value)`.

Example
package main

import (
	"fmt"

	"github.com/appthrust/mu/muo"
)

func main() {
	// FromSlice with non-empty slice
	slice1 := []string{"first", "second", "third"}
	first := muo.FromSlice(slice1)
	fmt.Println("FromSlice non-empty:", first.OrZero())

	// FromSlice with empty slice
	slice2 := []string{}
	empty := muo.FromSlice(slice2)
	fmt.Println("FromSlice empty:", empty.IsNone())

	// FromSlice with different types
	numbers := []int{10, 20, 30}
	firstNum := muo.FromSlice(numbers)
	fmt.Println("FromSlice numbers:", firstNum.OrZero())

}
Output:

FromSlice non-empty: first
FromSlice empty: true
FromSlice numbers: 10

func FromSliceN

func FromSliceN[T any](s []T, n int) mu.Option[T]

FromSliceN converts a n-th element of a slice to an `Option[T]`. If the slice is empty, it returns [None]. If the slice is not empty, it returns `Some(value)`.

Example
package main

import (
	"fmt"

	"github.com/appthrust/mu/muo"
)

func main() {
	// FromSliceN with valid index
	slice := []string{"zero", "one", "two", "three"}
	second := muo.FromSliceN(slice, 2)
	fmt.Println("FromSliceN(2):", second.OrZero())

	// FromSliceN with out of bounds index
	outOfBounds := muo.FromSliceN(slice, 10)
	fmt.Println("FromSliceN(10) out of bounds:", outOfBounds.IsNone())

	// FromSliceN with negative index
	negative := muo.FromSliceN(slice, -1)
	fmt.Println("FromSliceN(-1) negative:", negative.IsNone())

	// FromSliceN with empty slice
	empty := []string{}
	fromEmpty := muo.FromSliceN(empty, 0)
	fmt.Println("FromSliceN empty slice:", fromEmpty.IsNone())

}
Output:

FromSliceN(2): two
FromSliceN(10) out of bounds: true
FromSliceN(-1) negative: true
FromSliceN empty slice: true

func MapTo

func MapTo[T any, U any](o mu.Option[T], f func(T) U) mu.Option[U]

MapTo applies the function f to the contained value of `Option[T]` if it exists, returning `Option[U]`. If the option is [None], it returns None[U]. This enables type-changing transformations (T→U). See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/muo"
)

func main() {
	// MapTo with Some value (int to string)
	someInt := mu.Some(42)
	someStr := muo.MapTo(someInt, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("Some(42) mapped to string:", someStr.OrZero())

	// MapTo with None value
	noneInt := mu.None[int]()
	noneStr := muo.MapTo(noneInt, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("None mapped to string:", noneStr.IsNone())

	// MapTo with different types (int to bool)
	someBool := muo.MapTo(someInt, func(x int) bool {
		return x > 0
	})
	fmt.Println("Some(42) mapped to bool:", someBool.OrZero())

}
Output:

Some(42) mapped to string: 42
None mapped to string: true
Some(42) mapped to bool: true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL