generic

package
v2.2.78 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose[
	TAB ~func(func(B) HKTB) func(A) HKTA,
	TSA ~func(func(A) HKTA) func(S) HKTS,
	TSB ~func(func(B) HKTB) func(S) HKTS,
	S, A, B, HKTS, HKTA, HKTB any](ab TAB) func(TSA) TSB

func Filter added in v2.2.71

func Filter[
	S, HKTS, A, HKTA any](
	fof pointed.OfType[A, HKTA],
	fmap functor.MapType[A, A, HKTA, HKTA],
) func(predicate.Predicate[A]) endomorphism.Endomorphism[Traversal[S, A, HKTS, HKTA]]

Filter creates a function that filters the targets of a traversal based on a predicate.

This function allows you to refine a traversal to only focus on values that satisfy a given predicate. It works by converting the predicate into a prism, then converting that prism into a traversal, and finally composing it with the original traversal.

The filtering is selective: when modifying values through the filtered traversal, only values that satisfy the predicate will be transformed. Values that don't satisfy the predicate remain unchanged.

Type Parameters:

  • S: The source type
  • A: The focus type (the values being filtered)
  • HKTS: Higher-kinded type for S (functor/applicative context)
  • HKTA: Higher-kinded type for A (functor/applicative context)

Parameters:

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

Returns:

  • A function that takes a predicate and returns an endomorphism on traversals

Example:

import (
    AR "github.com/IBM/fp-go/v2/array"
    F "github.com/IBM/fp-go/v2/function"
    "github.com/IBM/fp-go/v2/identity"
    N "github.com/IBM/fp-go/v2/number"
    AI "github.com/IBM/fp-go/v2/optics/traversal/array/identity"
)

// Create a traversal for array elements
arrayTraversal := AI.FromArray[int]()
baseTraversal := F.Pipe1(
    Id[[]int, []int](),
    Compose[[]int, []int, []int, int](arrayTraversal),
)

// Filter to only positive numbers
isPositive := N.MoreThan(0)
filteredTraversal := F.Pipe1(
    baseTraversal,
    Filter[[]int, int](identity.Of[int], identity.Map[int, int])(isPositive),
)

// Double only positive numbers
numbers := []int{-2, -1, 0, 1, 2, 3}
result := filteredTraversal(func(n int) int { return n * 2 })(numbers)
// result: [-2, -1, 0, 2, 4, 6]

See Also:

  • prism.FromPredicate: Creates a prism from a predicate
  • prism.AsTraversal: Converts a prism to a traversal
  • Compose: Composes two traversals

func Fold

func Fold[S, A any](sa Traversal[S, A, C.Const[A, S], C.Const[A, A]]) func(S) A

Fold maps each target to a `Monoid` and combines the result

func FoldMap

func FoldMap[S, M, A any](f func(A) M) func(sa Traversal[S, A, C.Const[M, S], C.Const[M, A]]) func(S) M

FoldMap maps each target to a `Monoid` and combines the result

func FromTraversable

func FromTraversable[
	TAB ~func(func(A) HKTFA) func(HKTTA) HKTAA,
	A,
	HKTTA,
	HKTFA,
	HKTAA any](
	traverseF func(HKTTA, func(A) HKTFA) HKTAA,
) TAB

func GetAll

func GetAll[GA ~[]A, S, A any](s S) func(sa Traversal[S, A, C.Const[GA, S], C.Const[GA, A]]) GA

GetAll gets all the targets of a traversal

Types

type Traversal

type Traversal[S, A, HKTS, HKTA any] = func(func(A) HKTA) func(S) HKTS

Jump to

Keyboard shortcuts

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