nonempty

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ap

func Ap[B, A any](fa NonEmptyArray[A]) func(NonEmptyArray[func(A) B]) NonEmptyArray[B]

func First

func First[A any](as NonEmptyArray[A]) A

func Fold

func Fold[A any](s S.Semigroup[A]) func(NonEmptyArray[A]) A

Fold folds the NonEmptyArray using the provided [Semigroup].

func FoldMap

func FoldMap[A, B any](s S.Semigroup[B]) func(func(A) B) func(NonEmptyArray[A]) B

FoldMap maps and folds a NonEmptyArray. Map the NonEmptyArray passing each value to the iterating function. Then fold the results using the provided [Semigroup].

func Head[A any](as NonEmptyArray[A]) A

func IsEmpty

func IsEmpty[A any](_ NonEmptyArray[A]) bool

func IsNonEmpty

func IsNonEmpty[A any](_ NonEmptyArray[A]) bool

func Last

func Last[A any](as NonEmptyArray[A]) A

func Prepend

func Prepend[A any](head A) EM.Endomorphism[NonEmptyArray[A]]

Prepend prepends a single value to an array

func Reduce

func Reduce[A, B any](f func(B, A) B, initial B) func(NonEmptyArray[A]) B

func ReduceRight

func ReduceRight[A, B any](f func(A, B) B, initial B) func(NonEmptyArray[A]) B

func Size

func Size[A any](as NonEmptyArray[A]) int

func Tail

func Tail[A any](as NonEmptyArray[A]) []A

Types

type Kleisli

type Kleisli[A, B any] = func(A) NonEmptyArray[B]

type NonEmptyArray

type NonEmptyArray[A any] []A

NonEmptyArray represents an array with at least one element

func Flatten

func Flatten[A any](mma NonEmptyArray[NonEmptyArray[A]]) NonEmptyArray[A]

func From

func From[A any](first A, data ...A) NonEmptyArray[A]

From constructs a NonEmptyArray from a set of variadic arguments

func MonadAp

func MonadAp[B, A any](fab NonEmptyArray[func(A) B], fa NonEmptyArray[A]) NonEmptyArray[B]

func MonadChain

func MonadChain[A, B any](fa NonEmptyArray[A], f Kleisli[A, B]) NonEmptyArray[B]

func MonadMap

func MonadMap[A, B any](as NonEmptyArray[A], f func(a A) B) NonEmptyArray[B]

func Of

func Of[A any](first A) NonEmptyArray[A]

Of constructs a single element array

type Operator

type Operator[A, B any] = Kleisli[NonEmptyArray[A], B]

func Chain

func Chain[A, B any](f func(A) NonEmptyArray[B]) Operator[A, B]

func Map

func Map[A, B any](f func(a A) B) Operator[A, B]

type Option

type Option[A any] = option.Option[A]

func ToNonEmptyArray

func ToNonEmptyArray[A any](as []A) Option[NonEmptyArray[A]]

ToNonEmptyArray attempts to convert a regular slice into a NonEmptyArray. This function provides a safe way to create a NonEmptyArray from a slice that might be empty, returning an Option type to handle the case where the input slice is empty.

Type Parameters:

  • A: The element type of the array

Parameters:

  • as: A regular slice that may or may not be empty

Returns:

  • Option[NonEmptyArray[A]]: Some(NonEmptyArray) if the input slice is non-empty, None if empty

Behavior:

  • If the input slice is empty, returns None
  • If the input slice has at least one element, wraps it in Some and returns it as a NonEmptyArray
  • The conversion is a type cast, so no data is copied

Example:

// Convert non-empty slice
numbers := []int{1, 2, 3}
result := ToNonEmptyArray(numbers)  // Some(NonEmptyArray[1, 2, 3])

// Convert empty slice
empty := []int{}
result := ToNonEmptyArray(empty)  // None

// Use with Option methods
numbers := []int{1, 2, 3}
result := ToNonEmptyArray(numbers)
if O.IsSome(result) {
    nea := O.GetOrElse(F.Constant(From(0)))(result)
    head := Head(nea)  // 1
}

Use cases:

  • Safely converting user input or external data to NonEmptyArray
  • Validating that a collection has at least one element before processing
  • Converting results from functions that return regular slices
  • Ensuring type safety when working with collections that must not be empty

Example with validation:

func processItems(items []string) Option[string] {
    return F.Pipe2(
        items,
        ToNonEmptyArray[string],
        O.Map(func(nea NonEmptyArray[string]) string {
            return Head(nea)  // Safe to get head since we know it's non-empty
        }),
    )
}

Example with error handling:

items := []int{1, 2, 3}
result := ToNonEmptyArray(items)
switch {
case O.IsSome(result):
    nea := O.GetOrElse(F.Constant(From(0)))(result)
    fmt.Println("First item:", Head(nea))
case O.IsNone(result):
    fmt.Println("Array is empty")
}

Example with chaining:

// Process only if non-empty
result := F.Pipe3(
    []int{1, 2, 3},
    ToNonEmptyArray[int],
    O.Map(Map(func(x int) int { return x * 2 })),
    O.Map(Head[int]),
)  // Some(2)

Note: This function is particularly useful when working with APIs or functions that return regular slices but you need the type-level guarantee that the collection is non-empty for subsequent operations.

Jump to

Keyboard shortcuts

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