reflect

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package reflect provides functional programming utilities for working with Go's reflect.Value type. It offers higher-order functions like Map, Reduce, and ReduceWithIndex that operate on reflective values representing slices or arrays.

These utilities are particularly useful when working with dynamic types or when implementing generic algorithms that need to operate on collections discovered at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[A any](f func(R.Value) A) func(R.Value) []A

Map transforms each element of a reflect.Value (representing a slice or array) using the provided function, returning a new slice containing the transformed values.

This is a curried function that first takes the transformation function, then returns a function that accepts the reflect.Value to map over.

Parameters:

  • f: A transformation function that takes a reflect.Value and returns a value of type A

Returns:

  • A function that takes a reflect.Value and returns a slice of transformed values

Example:

// Extract integers from a reflected slice and double them
doubleInts := Map(func(v reflect.Value) int {
    return int(v.Int()) * 2
})
result := doubleInts(reflect.ValueOf([]int{1, 2, 3}))
// result = []int{2, 4, 6}

func Reduce

func Reduce[A any](f func(A, R.Value) A, initial A) func(R.Value) A

Reduce applies a reducer function to each element of a reflect.Value (representing a slice or array), accumulating a result value. Unlike ReduceWithIndex, the reducer function does not receive the index.

This is a curried function that first takes the reducer function and initial value, then returns a function that accepts the reflect.Value to reduce.

Parameters:

  • f: A reducer function that takes (accumulator A, element reflect.Value) and returns the new accumulator
  • initial: The initial value for the accumulation

Returns:

  • A function that takes a reflect.Value and returns the final accumulated value

Example:

// Sum all integers in a reflected slice
sum := Reduce(func(acc int, v reflect.Value) int {
    return acc + int(v.Int())
}, 0)
result := sum(reflect.ValueOf([]int{10, 20, 30}))
// result = 60

func ReduceWithIndex

func ReduceWithIndex[A any](f func(int, A, R.Value) A, initial A) func(R.Value) A

ReduceWithIndex applies a reducer function to each element of a reflect.Value (representing a slice or array), accumulating a result value. The reducer function receives the current index, the accumulated value, and the current element as a reflect.Value.

This is a curried function that first takes the reducer function and initial value, then returns a function that accepts the reflect.Value to reduce.

Parameters:

  • f: A reducer function that takes (index int, accumulator A, element reflect.Value) and returns the new accumulator
  • initial: The initial value for the accumulation

Returns:

  • A function that takes a reflect.Value and returns the final accumulated value

Example:

// Sum all integers in a reflected slice with their indices
sumWithIndex := ReduceWithIndex(func(i int, acc int, v reflect.Value) int {
    return acc + i + int(v.Int())
}, 0)
result := sumWithIndex(reflect.ValueOf([]int{10, 20, 30}))
// result = 0 + (0+10) + (1+20) + (2+30) = 63

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