lof

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 1 Imported by: 0

README

lof: lower-order function wrappers

Utility functions for functional programming. Wraps Go builtins for HOF use, plus helpers for common patterns.

A lower-order function is the flip side of a higher-order function—the function being passed, not the one receiving it.

Go builtins like len are operators, not functions—you can't pass len to ToInt. lof wraps them as passable functions.

names.Each(lof.Println)  // print each name

See pkg.go.dev for complete API documentation.

Quick Start

import (
    "github.com/binaryphile/fluentfp/lof"
    "github.com/binaryphile/fluentfp/slice"
)

names := slice.From(users).ToString(User.Name)
names.Each(lof.Println)  // print each name

// When type has no Len() method, lof.Len bridges the gap
type Report struct { Pages []Page }

// pageCount returns the number of pages in a report.
pageCount := func(r Report) int { return lof.Len(r.Pages) }
pageCounts := slice.From(reports).ToInt(pageCount)

API Reference

Function Signature Purpose Example
Len Len[T]([]T) int Wrap len for slices lengths = items.ToInt(lof.Len)
StringLen StringLen(string) int Wrap len for strings lens = names.ToInt(lof.StringLen)
Println Println(string) Wrap fmt.Println names.Each(lof.Println)
IfNotEmpty IfNotEmpty(string) (string, bool) Comma-ok for strings diff, ok := lof.IfNotEmpty(result)

IfNotEmpty: Comma-ok for Empty Strings

Some functions use empty string as "absent" (e.g., cmp.Diff returns "" when equal). IfNotEmpty converts this to Go's comma-ok idiom.

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

When NOT to Use lof

  • Method expressions existUser.Name beats wrapping a getter
  • Direct calls work — If you're not in a HOF chain, just call len() or fmt.Println()

See Also

For the HOF methods that consume these wrappers, see slice.

Documentation

Overview

Package lof provides utility functions for functional programming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IfNotEmpty added in v0.12.0

func IfNotEmpty(s string) (string, bool)

IfNotEmpty 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.IfNotEmpty(result); ok {
    t.Errorf("mismatch:\n%s", diff)
}

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 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