luminyra

Quickstart

Create a lazy sequence from a slice of orders and collect paid order IDs and their total.

Run the orders example

Use Go 1.27 or later. Install coll, copy the complete example into your project's main.go, and run go run .. From the coll source directory, run go run ./examples/slices.

go get github.com/luminyra/coll
Complete orders example
// Command slices filters, transforms, groups and batches a slice of orders,
// first with a lazy coll.Seq chain and then with eager helpers.
package main

import (
	"fmt"
	"maps"
	"slices"

	"github.com/luminyra/coll"
)

type order struct {
	ID       int
	Customer string
	Amount   int // in cents
	Paid     bool
}

func main() {
	orders := []order{
		{ID: 1, Customer: "alice", Amount: 1200, Paid: true},
		{ID: 2, Customer: "bob", Amount: 800, Paid: false},
		{ID: 3, Customer: "alice", Amount: 300, Paid: true},
		{ID: 4, Customer: "carol", Amount: 4500, Paid: true},
		{ID: 5, Customer: "bob", Amount: 2600, Paid: true},
	}
	isPaid := func(o order) bool { return o.Paid }

	// A Seq is lazy: each step runs only when Collect or a range loop pulls
	// values. Map is a Go 1.27 generic method, so the chain can turn orders
	// into ints or strings.
	paid := coll.From(orders).Filter(isPaid)

	ids := paid.Map(func(o order) int { return o.ID }).Collect()
	total := paid.Fold(0, func(sum int, o order) int { return sum + o.Amount })
	customers := paid.UniqBy(func(o order) string { return o.Customer }).
		Map(func(o order) string { return o.Customer }).
		Collect()

	fmt.Println("paid orders:", ids)
	fmt.Println("total (cents):", total)
	fmt.Println("customers:", customers)

	// GroupBy returns a map, whose iteration order is unspecified; sort the
	// keys for stable output.
	byCustomer := coll.GroupBy(orders, func(o order) string { return o.Customer })
	for _, name := range slices.Sorted(maps.Keys(byCustomer)) {
		fmt.Printf("%s: %d orders\n", name, len(byCustomer[name]))
	}

	settled, pending := coll.Partition(orders, isPaid)
	fmt.Printf("%d paid, %d pending\n", len(settled), len(pending))

	// For a slice that is already in memory, the standard slices.Chunk
	// batches it without copying.
	for batch := range slices.Chunk(ids, 2) {
		fmt.Println("batch:", batch)
	}
}
View on GitHub

Read the first three lines of output

From creates a Seq from the orders slice, and Seq.Filter keeps paid orders. Seq.Map extracts the IDs, Seq.Collect returns a slice of IDs, and Seq.Fold sums the amounts in cents. The program prints the paid order IDs, the total amount, and the unique customer names.

Read when the sequence is consumed next.

On this page