combo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 3 Imported by: 0

README

Combo - combinatorial operations for Go

Go Reference Go Report Card Tests Coverage Status

This is combo, a library of combinatorial operations for Go. Given a slice, it can compute its permutations, its n-element combinations, and its n-element combinations-with-replacement.

There is also a command-line tool for invoking these operations on a list of strings.

Installation and usage

For library usage, please see the documentation at pkg.go.dev.

Installing the command-line tool:

go install github.com/bobg/combo/cmd/combo@latest

Usage:

combo perm [ARG ARG ...]
combo comb N [ARG ARG ...]
combo rcomb N [ARG ARG ...]

The first argument is the operation to run: perm for permutations; comb for combinations; and rcomb for combinations-with-replacement.

The comb and rcomb subcommands require a numeric argument, N, the number of strings to choose in each combination.

The input is specified as additional arguments on the command line. If no such arguments are supplied, then the input is read from standard input, one line per string.

Examples:

$ combo perm 1 2 3
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
$ combo comb 2 a b c
a b
a c
b c
$ combo rcomb 2 a b c
a a
a b
a c
b b
b c
c c

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combinations

func Combinations[T any, S ~[]T](s S, n int) iter.Seq[S]

Combinations produces a sequence containing all n-length combinations of distinct elements from s.

If s is [1 2 3] and n is 2, this function will produce:

[1 2] [1 3] [2 3]
Example
package main

import (
	"fmt"

	"github.com/bobg/combo"
)

func main() {
	slice := []int{1, 2, 3, 4}
	combs := combo.Combinations(slice, 2)
	for c := range combs {
		fmt.Println(c)
	}
}
Output:

[1 2]
[1 3]
[1 4]
[2 3]
[2 4]
[3 4]

func CombinationsWithReplacement

func CombinationsWithReplacement[T any, S ~[]T](s S, n int) iter.Seq[S]

CombinationsWithReplacement produces a sequence containing all n-length combinations of possibly repeated elements from s.

If s is [1 2 3] and n is 2, this function will produce:

[1 1] [1 2] [1 3] [2 2] [2 3] [3 3]
Example
package main

import (
	"fmt"

	"github.com/bobg/combo"
)

func main() {
	slice := []int{1, 2, 3}
	rcombs := combo.CombinationsWithReplacement(slice, 2)
	for c := range rcombs {
		fmt.Println(c)
	}
}
Output:

[1 1]
[1 2]
[1 3]
[2 2]
[2 3]
[3 3]

func Permutations

func Permutations[T any, S ~[]T](s S) iter.Seq[S]

Permutations produces a sequence containing all permutations of s. It uses Heap's Algorithm. See https://en.wikipedia.org/wiki/Heap%27s_algorithm.

If s is [1 2 3], this function will produce:

[1 2 3] [2 1 3] [3 1 2] [1 3 2] [2 3 1] [3 2 1]
Example
package main

import (
	"fmt"

	"github.com/bobg/combo"
)

func main() {
	slice := []int{1, 2, 3}
	perms := combo.Permutations(slice)
	for p := range perms {
		fmt.Println(p)
	}
}
Output:

[1 2 3]
[2 1 3]
[3 1 2]
[1 3 2]
[2 3 1]
[3 2 1]

Types

This section is empty.

Directories

Path Synopsis
cmd
combo command

Jump to

Keyboard shortcuts

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