Code-Memo

Itertools-style patterns

Python’s itertools maps to small loops plus slices and maps (Go 1.21+), sometimes container/ring.

Chain slices
a := []int{1, 2}
b := []int{3, 4}
out := append(append([]int{}, a...), b...)
Unique / dedupe (sorted input)
func uniqSorted(xs []int) []int {
	if len(xs) == 0 {
		return xs
	}
	out := xs[:1]
	for _, v := range xs[1:] {
		if v != out[len(out)-1] {
			out = append(out, v)
		}
	}
	return out
}
Cartesian product

Nested for loops; count known dimensions explicitly.

Permutations / combinations

Implement recursively or use a focused library if combinatorics grow complex.

slices.Chunk (Go 1.23+)

Batch elements for processing windows.

Zipping two slices
for i := 0; i < len(a) && i < len(b); i++ {
	_, _ = a[i], b[i]
}

Different lengths need a policy (truncate, error, pad).

Infinite iterators

Channels or stateful iterator structs; avoid goroutines unless you need concurrency.