generic

package
v2.2.71 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At[GA ~[]A, A any](idx int) OP.Optional[GA, A]

At creates an Optional that focuses on the element at a specific index in an array.

This function returns an Optional that can get and set the element at the given index. If the index is out of bounds, GetOption returns None and Set operations are no-ops (the array is returned unchanged). This follows the Optional laws where operations on non-existent values have no effect.

The Optional provides safe array access without panicking on invalid indices, making it ideal for functional transformations where you want to modify array elements only when they exist.

Type Parameters:

  • A: The type of elements in the array

Parameters:

  • idx: The zero-based index to focus on

Returns:

  • An Optional that focuses on the element at the specified index

Example:

import (
    AR "github.com/IBM/fp-go/v2/array"
    OP "github.com/IBM/fp-go/v2/optics/optional"
    OA "github.com/IBM/fp-go/v2/optics/optional/array"
)

numbers := []int{10, 20, 30, 40}

// Create an optional focusing on index 1
second := OA.At[int](1)

// Get the element at index 1
value := second.GetOption(numbers)
// value: option.Some(20)

// Set the element at index 1
updated := second.Set(25)(numbers)
// updated: []int{10, 25, 30, 40}

// Out of bounds access returns None
outOfBounds := OA.At[int](10)
value = outOfBounds.GetOption(numbers)
// value: option.None[int]()

// Out of bounds set is a no-op
unchanged := outOfBounds.Set(99)(numbers)
// unchanged: []int{10, 20, 30, 40} (original array)

See Also:

  • AR.Lookup: Gets an element at an index, returning an Option
  • AR.UpdateAt: Updates an element at an index, returning an Option
  • OP.Optional: The Optional optic type

Types

This section is empty.

Jump to

Keyboard shortcuts

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