Skip to main content
Version: v2.2.82 (latest)
Reference · Collections

Array Applicative

Applicative operations for arrays. Apply arrays of functions to arrays of values, producing all combinations.

01

Core API

FunctionSignatureDescription
Apfunc Ap[A, B any](fns []func(A) B) func([]A) []BApply functions to values
Flapfunc Flap[A, B any](value A) func([]func(A) B) []BApply value to functions
02

Usage Examples

Ap - Apply Functions

ap.go
import (
  A "github.com/IBM/fp-go/v2/array"
  F "github.com/IBM/fp-go/v2/function"
)

// Array of functions
fns := []func(int) int{
  func(n int) int { return n * 2 },
  func(n int) int { return n + 10 },
}

// Array of values
values := []int{1, 2, 3}

// Apply all functions to all values
result := F.Pipe2(
  values,
  A.Ap(fns),
)
// []int{2, 4, 6, 11, 12, 13}
// (1*2, 2*2, 3*2, 1+10, 2+10, 3+10)

Flap - Apply Value

flap.go
fns := []func(int) string{
  func(n int) string { return fmt.Sprintf("Double: %d", n*2) },
  func(n int) string { return fmt.Sprintf("Square: %d", n*n) },
}

result := F.Pipe2(
  fns,
  A.Flap(5),
)
// []string{"Double: 10", "Square: 25"}

Validation Example

validation.go
type Validator func(string) bool

validators := []Validator{
  func(s string) bool { return len(s) > 5 },
  func(s string) bool { return strings.Contains(s, "@") },
}

inputs := []string{"test", "user@example.com", "admin"}

// Apply all validators to all inputs
results := F.Pipe2(
  inputs,
  A.Ap(validators),
)
// []bool{false, true, false, false, true, false}