### chans.Map for fetching accounts Source: https://antonz.org/chans/index Example of using chans.Map to fetch user accounts from a database based on a channel of IDs. The results are sent to an output channel, and any errors are collected. ```go errs := make([]error, 2) accounts := make(chan *Account) go func() { getAcc := func(id string) (*Account, error) { return db.GetAccount(ctx, id) } err := chans.Map(ctx, accounts, ids, getAcc) errs[0] = err close(accounts) }() ``` -------------------------------- ### Example Go Pipeline with chans Package Source: https://antonz.org/chans/index Demonstrates a typical use case of the chans package, involving flattening a channel of string slices, closing the output channel, and then reducing the flattened words to a byte count. It utilizes `chans.Flatten` and `chans.Reduce`. ```go package main import ( "context" "fmt" "github.com/your_username/chans" ) func main() { ctx := context.Background() // Given a channel of documents. docs := make(chan []string, 10) docs <- []string{"go", "is", "awesome"} docs <- []string{"cats", "are", "cute"} close(docs) // Extract all words from the documents. words := make(chan string, 10) chans.Flatten(ctx, words, docs) close(words) // Calculate the total byte count of all words. step := func(acc int, word string) int { return acc + len(word) } count := chans.Reduce(ctx, words, 0, step) fmt.Println("byte count =", count) } ``` -------------------------------- ### Error handling after chans operations Source: https://antonz.org/chans/index Example of consolidating errors from multiple chans operations and context cancellation to determine the overall success or failure of the pipeline. ```go err := errors.Join(errs[0], errs[1], ctx.Err()) if err != nil { return 0, err } return total, nil ``` -------------------------------- ### chans.Reduce for summing account balances Source: https://antonz.org/chans/index Shows how to use chans.Reduce to calculate the total balance of accounts from a channel. It takes an initial value and a function to accumulate the results. ```go sumBalance := func(total float64, acc *Account) float64 { return total + acc.Balance } total := chans.Reduce(ctx, vips, 0, sumBalance) ``` -------------------------------- ### chans.Filter for selecting VIP accounts Source: https://antonz.org/chans/index Demonstrates using chans.Filter to select only VIP accounts from a channel of accounts. The function takes an input channel, an output channel, and a predicate function to determine filtering. ```go vips := make(chan *Account) go func() { onlyVIP := func(acc *Account) (bool, error) { return acc.VIP, nil } err := chans.Filter(ctx, vips, accounts, onlyVIP) errs[1] = err close(vips) }() ``` -------------------------------- ### Map function from chans package Source: https://antonz.org/chans/index Illustrates the usage of the Map function from the chans package. This function provides core mapping logic, reading from an input channel, applying a function, and writing to an output channel, while respecting context cancellation and errors. ```go err := chans.Map(ctx, users, ids, func(id int) (*User, error) { return db.GetUser(ctx, id) }) ``` -------------------------------- ### chans.Reduce Functionality Source: https://antonz.org/chans/index The Reduce function in the chans package combines all values from an input channel into a single result using a provided function. It takes a context, an input channel, an initial accumulator value, and a step function. ```go // Calculate the total byte count of all words. step := func(acc int, word string) int { return acc + len(word) } count := chans.Reduce(ctx, words, 0, step) fmt.Println("byte count =", count) ``` -------------------------------- ### Map function from rill package Source: https://antonz.org/chans/index Demonstrates the usage of the Map function from the rill package for concurrent processing. This implementation is opinionated, spawning goroutines, not exiting early on error, and managing output channels internally. ```go // Concurrency = 3 users := rill.Map(ids, 3, func(id int) (*User, error) { return db.GetUser(ctx, id) }) ``` -------------------------------- ### chans.Flatten Functionality Source: https://antonz.org/chans/index The Flatten function reads slices from an input channel and sends their individual elements to an output channel in order. This is useful for de-nesting data structures within a pipeline. ```go // Extract all words from the documents. words := make(chan string, 10) chans.Flatten(ctx, words, docs) close(words) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.