Documentation
¶
Overview ¶
Package combo generates combinatorial structures over slices: Cartesian products, permutations, combinations (k-subsets), and power sets.
Eager functions return slice.Mapper for fluent chaining. Seq variants (SeqPermutations, SeqPowerSet, etc.) return seq.Seq for lazy evaluation with early termination — use these for large inputs where materializing the full result is impractical.
Index ¶
- func CartesianProduct[A, B any](a []A, b []B) slice.Mapper[pair.Pair[A, B]]
- func CartesianProductWith[A, B, R any](a []A, b []B, fn func(A, B) R) slice.Mapper[R]
- func Combinations[T any](items []T, k int) slice.Mapper[[]T]
- func Permutations[T any](items []T) slice.Mapper[[]T]
- func PowerSet[T any](items []T) slice.Mapper[[]T]
- func SeqCartesianProduct[A, B any](a []A, b []B) seq.Seq[pair.Pair[A, B]]
- func SeqCartesianProductWith[A, B, R any](a []A, b []B, fn func(A, B) R) seq.Seq[R]
- func SeqCombinations[T any](items []T, k int) seq.Seq[[]T]
- func SeqPermutations[T any](items []T) seq.Seq[[]T]
- func SeqPowerSet[T any](items []T) seq.Seq[[]T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CartesianProduct ¶
CartesianProduct returns all pairs from a and b.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/combo"
)
func main() {
// All pairs from two lists.
results := combo.CartesianProduct([]string{"a", "b"}, []int{1, 2})
for _, p := range results {
fmt.Printf("(%s, %d)\n", p.First, p.Second)
}
}
Output: (a, 1) (a, 2) (b, 1) (b, 2)
func CartesianProductWith ¶
CartesianProductWith applies fn to every (a, b) pair. Avoids intermediate Pair allocation when the caller transforms immediately. Panics if fn is nil.
func Combinations ¶
Combinations returns all k-element subsets of items, preserving order. Returns C(n,k) results.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/combo"
)
func main() {
// Choose 2 items from 4.
results := combo.Combinations([]string{"a", "b", "c", "d"}, 2)
for _, c := range results {
fmt.Println(c)
}
}
Output: [a b] [a c] [a d] [b c] [b d] [c d]
func Permutations ¶
Permutations returns all orderings of items. Returns n! results — use only for small inputs.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/combo"
)
func main() {
// All orderings of 3 items.
results := combo.Permutations([]int{1, 2, 3})
fmt.Println(results.Len(), "permutations")
fmt.Println(results[0])
}
Output: 6 permutations [1 2 3]
func PowerSet ¶
PowerSet returns all subsets of items. Returns 2^n results — use only for small inputs.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/combo"
)
func main() {
// All subsets of a set.
results := combo.PowerSet([]string{"x", "y"})
for _, s := range results {
fmt.Println(s)
}
}
Output: [] [y] [x] [x y]
func SeqCartesianProduct ¶ added in v0.114.0
SeqCartesianProduct returns a lazy sequence of all pairs from a and b.
func SeqCartesianProductWith ¶ added in v0.114.0
SeqCartesianProductWith returns a lazy sequence applying fn to every (a, b) pair. Avoids intermediate Pair allocation when the caller transforms immediately. Panics if fn is nil.
func SeqCombinations ¶ added in v0.114.0
SeqCombinations returns a lazy sequence of all k-element subsets of items, preserving order. Returns C(n,k) results.
func SeqPermutations ¶ added in v0.114.0
SeqPermutations returns a lazy sequence of all orderings of items. Returns n! results — use seq.Seq.Take for large inputs.
func SeqPowerSet ¶ added in v0.114.0
SeqPowerSet returns a lazy sequence of all subsets of items. Returns 2^n results — use seq.Seq.Take for large inputs.
Types ¶
This section is empty.