lof

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 3 Imported by: 0

README

lof

Callback helpers for fluent chains — adapters for builtins, predicates, comparators, identity functions, and stream helpers.

Go builtins like len aren't first-class function values, and standard library functions like fmt.Println often have signatures that don't match callback shapes expected by collection APIs. lof provides small helpers with passable function types.

// Sort strings descending
sorted := slice.From(names).SortBy(lof.StringDesc)

What It Looks Like

// Length of each inner slice
pageCounts := slice.From(reports).ToInt(lof.Len)
// Filter blank strings (whitespace-only counts as blank)
meaningful := slice.From(lines).KeepIf(lof.IsNonBlank)
// Generate a sequence: 0, 1, 2, 3, ...
nats := stream.Generate(0, lof.Inc)
// Comma-ok for option interop
if name, ok := lof.IfNonEmpty(os.Getenv("USER")); ok {
    fmt.Println(name)
}

Generic vs Concrete Helpers

Some helpers have both a generic version and concrete type-specific variants. The generic versions (Asc, Desc, Identity) require explicit type instantiation (lof.Identity[string]). The concrete variants (StringAsc, IntDesc, StringIdentity) avoid this — use them when working with common types.

Operations

Projection

  • Len[T any]([]T) intlen for slices
  • StringLen(string) intlen for strings
  • Identity[T any](T) T — returns argument unchanged (may require explicit instantiation: lof.Identity[string])
  • StringIdentity(string) string — string-specific identity
  • IntIdentity(int) int — int-specific identity

Predicates

  • IsNonEmpty(string) bool — true if s != ""
  • IsNonBlank(string) bool — true if strings.TrimSpace(s) != "" (whitespace-only strings return false)
  • IfNonEmpty(string) (string, bool) — comma-ok for option.New and similar

Comparators (return int for use with slices.SortFunc and slice.SortBy)

  • Asc[T cmp.Ordered](a, b T) int — ascending
  • Desc[T cmp.Ordered](a, b T) int — descending
  • StringAsc(a, b string) int — ascending strings
  • StringDesc(a, b string) int — descending strings
  • IntAsc(a, b int) int — ascending ints
  • IntDesc(a, b int) int — descending ints

Generation

  • Inc(int) int — successor function (n + 1) for stream.Generate and similar

Side Effects

  • Println(string) — string-specific wrapper for fmt.Println (drops return values)

See pkg.go.dev for complete API documentation, the main README for installation, and slice for the collection methods that consume these helpers.

Documentation

Overview

Package lof provides utility functions for functional programming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Asc added in v0.41.0

func Asc[T cmp.Ordered](a, b T) int

Asc is an ascending comparator for ordered types.

func Desc added in v0.41.0

func Desc[T cmp.Ordered](a, b T) int

Desc is a descending comparator for ordered types.

func Identity added in v0.41.0

func Identity[T any](t T) T

Identity returns its argument unchanged. Use as a function value via type instantiation: lof.Identity[string]

func IfNonEmpty added in v0.30.0

func IfNonEmpty(s string) (string, bool)

IfNonEmpty returns s and whether s is non-empty. Converts "empty string = absent" returns to Go's comma-ok idiom.

result := cmp.Diff(want, got)
if diff, ok := lof.IfNonEmpty(result); ok {
    t.Errorf("mismatch:\n%s", diff)
}

func Inc added in v0.41.0

func Inc(n int) int

Inc returns n + 1. Successor function for use with stream.Generate and similar.

func IntAsc added in v0.41.0

func IntAsc(a, b int) int

IntAsc is an ascending comparator for ints.

func IntDesc added in v0.41.0

func IntDesc(a, b int) int

IntDesc is a descending comparator for ints.

func IntIdentity added in v0.41.0

func IntIdentity(n int) int

IntIdentity returns its int argument unchanged.

func IsNonBlank added in v0.30.0

func IsNonBlank(s string) bool

IsNonBlank returns true if s contains non-whitespace characters.

func IsNonEmpty added in v0.30.0

func IsNonEmpty(s string) bool

IsNonEmpty returns true if s is non-empty.

func Len

func Len[T any](ts []T) int

Len wraps the len builtin for slices.

func Println

func Println(s string)

Println wraps fmt.Println for strings.

func StringAsc added in v0.41.0

func StringAsc(a, b string) int

StringAsc is an ascending comparator for strings.

func StringDesc added in v0.41.0

func StringDesc(a, b string) int

StringDesc is a descending comparator for strings.

func StringIdentity added in v0.41.0

func StringIdentity(s string) string

StringIdentity returns its string argument unchanged.

func StringLen

func StringLen(s string) int

StringLen wraps the len builtin for strings.

Types

This section is empty.

Jump to

Keyboard shortcuts

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