Documentation
¶
Overview ¶
Package combo generates combinatorial structures over slices: Cartesian products, permutations, combinations (k-subsets), and power sets.
All functions return slice.Mapper for fluent chaining. Results grow factorially or exponentially — intended for small inputs or test generation, not production data volumes.
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]
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]
Types ¶
This section is empty.