luminyra

快速上手

从订单切片创建惰性序列,收集已付款订单的编号和总额。

运行订单示例

使用 Go 1.27 或更高版本,安装 coll,把完整示例复制到项目的 main.go,运行 go run .。在 coll 源码目录也可以直接运行 go run ./examples/slices。

go get github.com/luminyra/coll
完整订单示例
// 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)
	}
}
在 GitHub 查看

读懂前三行输出

From 从订单切片创建 Seq,Seq.Filter 保留已付款的订单。Seq.Map 取出编号,Seq.Collect 得到编号切片,Seq.Fold 累加以分计的金额。程序输出付款编号、金额合计和去重后的客户名。

接着阅读 何时开始消费序列。

本页目录