generic

package
v2.3.11 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromArrayLens

func FromArrayLens[GA ~[]A, S, A, HKTS, HKTA, HKTAA, HKTRA any](
	fof pointed.OfType[GA, HKTRA],
	fmap functor.MapType[GA, func(A) GA, HKTRA, HKTAA],
	fmapEndo functor.MapType[GA, Endomorphism[S], HKTRA, HKTS],
	fap apply.ApType[HKTA, HKTRA, HKTAA],
) func(Lens[S, GA]) Traversal[S, A, HKTS, HKTA]

FromArrayLens creates a traversal endomorphism from a lens that focuses on an array.

This function converts a lens that accesses an array field into a traversal endomorphism that can traverse all elements within that array. The resulting traversal works with Endomorphism[S] as its effect type, making it composable with other traversal endomorphisms using monoid operations.

The function works by:

  1. Using the lens getter to extract the array from the source structure
  2. Traversing all elements in the array using the provided transformation function
  3. Using the lens setter (mapped to work with endomorphisms) to update the structure

This is particularly useful when building complex traversals that need to access and modify array fields within a larger structure. The endomorphism-based approach allows you to combine multiple such traversals using Concat and Empty operations.

Type Parameters:

  • GA: Array type constraint (e.g., []A)
  • S: The source structure type containing the array
  • A: The element type within the array
  • HKTS: Higher-kinded type for Endomorphism[S]
  • HKTA: Higher-kinded type for A in the effect context
  • HKTAA: Higher-kinded type for the array transformation function
  • HKTRA: Higher-kinded type for the array in the effect context

Parameters:

  • fof: Function to lift GA into HKTRA (pure/of operation)
  • fmap: Function to map over the array transformation
  • fmapEndo: Function to map the lens setter into the endomorphism context
  • fap: Applicative apply operation for combining effects

Returns:

  • A function that takes a lens and returns a traversal endomorphism

Example:

import (
    F "github.com/IBM/fp-go/v2/function"
    "github.com/IBM/fp-go/v2/monoid"
    thunk "github.com/IBM/fp-go/v2/context/readerioresult"
    TLA "github.com/IBM/fp-go/v2/optics/traversalendo/array/generic"
)

type Person struct {
    Name    string
    Hobbies []string
}

// Create a lens for the Hobbies field
hobbiesLens := lens.Lens[Person, []string]{
    Get: func(p Person) []string { return p.Hobbies },
    Set: func(p Person, h []string) Person {
        p.Hobbies = h
        return p
    },
}

// Convert to a traversal endomorphism
hobbiesTrav := TLA.FromArrayLens[[]string, Person, string](
    thunk.Of[[]string],
    thunk.Map[[]string, func(string) []string],
    thunk.Map[Person, endomorphism.Endomorphism[Person]],
    thunk.Ap[string, []string],
)(hobbiesLens)

// Use in a monoid fold to combine with other traversals
m := MakeMonoid[string, thunk.Thunk[string], Person](
    thunk.Of,
    thunk.Map,
    thunk.Ap,
)
combined := monoid.Fold(m)([]Traversal[Person, string]{
    hobbiesTrav,
    // ... other traversals
})

See Also:

  • ToTraversal: Convert a traversal endomorphism to a regular traversal
  • MakeMonoid: Create a monoid for combining traversal endomorphisms
  • Concat: Combine two traversal endomorphisms

Types

type Endomorphism

type Endomorphism[A any] = endomorphism.Endomorphism[A]

type Lens

type Lens[S, A any] = lens.Lens[S, A]

type Optional

type Optional[S, A any] = optional.Optional[S, A]

type Traversal

type Traversal[S, A, HKTS, HKTA any] = TG.Traversal[S, A, HKTS, HKTA]

HKTS = HKT[Endomorphism[S]]

Jump to

Keyboard shortcuts

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