Help us shape the future of Go! Take the 2026 Go Developer Survey

generic

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At added in v2.2.71

func At[GA ~[]A, S, A, HKTS, HKTGA, HKTA any](
	fof pointed.OfType[GA, HKTGA],
	fmap functor.MapType[A, GA, HKTA, HKTGA],
) func(int) func(G.Traversal[S, GA, HKTS, HKTGA]) G.Traversal[S, A, HKTS, HKTA]

At creates a function that focuses a traversal on a specific array index.

This function takes an index and returns a function that transforms a traversal focusing on an array into a traversal focusing on the element at that index. It works by:

  1. Creating an Optional that focuses on the array element at the given index
  2. Converting that Optional into a Traversal
  3. Composing it with the original traversal

If the index is out of bounds, the traversal will focus on zero elements (no-op), following the Optional laws where operations on non-existent values have no effect.

This is particularly useful when you have a nested structure containing arrays and want to traverse to a specific element within those arrays.

Type Parameters:

  • GA: Array type constraint (e.g., []A)
  • S: The source type of the outer traversal
  • A: The element type within the array
  • HKTS: Higher-kinded type for S (functor/applicative context)
  • HKTGA: Higher-kinded type for GA (functor/applicative context)
  • HKTA: Higher-kinded type for A (functor/applicative context)

Parameters:

  • fof: Function to lift GA into the higher-kinded type HKTGA (pure/of operation)
  • fmap: Function to map over HKTA and produce HKTGA (functor map operation)

Returns:

  • A function that takes an index and returns a traversal transformer

Example:

import (
    F "github.com/IBM/fp-go/v2/function"
    "github.com/IBM/fp-go/v2/identity"
    T "github.com/IBM/fp-go/v2/optics/traversal"
    TA "github.com/IBM/fp-go/v2/optics/traversal/array/generic"
)

type Person struct {
    Name    string
    Hobbies []string
}

// Create a traversal focusing on the hobbies array
hobbiesTraversal := T.Id[Person, []string]()

// Focus on the first hobby (index 0)
firstHobby := F.Pipe1(
    hobbiesTraversal,
    TA.At[[]string, Person, string](
        identity.Of[[]string],
        identity.Map[string, []string],
    )(0),
)

// Modify the first hobby
person := Person{Name: "Alice", Hobbies: []string{"reading", "coding"}}
updated := firstHobby(func(s string) string {
    return s + "!"
})(person)
// updated.Hobbies: []string{"reading!", "coding"}

See Also:

  • OA.At: Creates an Optional focusing on an array element
  • optional.AsTraversal: Converts an Optional to a Traversal
  • G.Compose: Composes two traversals

func FromArray

func FromArray[GA ~[]A, GB ~[]B, A, B, HKTB, HKTAB, HKTRB any](
	fof pointed.OfType[GB, HKTRB],
	fmap functor.MapType[GB, func(B) GB, HKTRB, HKTAB],
	fap apply.ApType[HKTB, HKTRB, HKTAB],
) G.Traversal[GA, A, HKTRB, HKTB]

FromArray returns a traversal from an array

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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