underscore/docs/content/collections/tap.md
Ruidy bcb4dd1e9d
docs: add documentation for new collection functions
Add comprehensive documentation for all new functions:
- TakeWhile: take elements while predicate is true
- DropWhile: drop elements while predicate is true
- Scan: running accumulator (prefix scan)
- First/FirstN: get first element(s) safely
- Init: all but last element
- Intersperse: insert separator between elements
- Sliding: sliding window views
- FoldRight: right-to-left fold/reduce
- Tap: side effects without mutation
- Transpose: flip matrix rows/columns
- Unzip: split tuples into separate slices
- ParallelReduce: parallel reduction (experimental)
- Replicate: create n copies of a value

Each doc includes:
- Clear description
- Code examples with output
- Common use cases
- Edge case handling

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-16 08:48:38 +01:00

1,002 B

title date
Tap 2025-01-16T00:00:00-00:00

Tap applies a function to each element for side effects (like debugging or logging) and returns the original slice unchanged. Useful for debugging pipelines without breaking the flow.

package main

import (
	"fmt"
	u "github.com/rjNemo/underscore"
)

func main() {
	// Debugging a pipeline
	nums := []int{1, 2, 3, 4, 5}

	result := u.Tap(
		u.Map(
			u.Filter(nums, func(n int) bool { return n%2 == 0 }),
			func(n int) int { return n * 2 },
		),
		func(n int) {
			fmt.Printf("Debug: %d\n", n) // Prints each value
		},
	)

	fmt.Println(result) // [4, 8]

	// Counting elements that pass through
	count := 0
	filtered := u.Tap(
		u.Filter(nums, func(n int) bool { return n > 2 }),
		func(n int) { count++ },
	)
	fmt.Printf("Found %d elements: %v\n", count, filtered)
	// Found 3 elements: [3 4 5]

	// Logging transformations
	data := []string{"hello", "world"}
	u.Tap(data, func(s string) {
		fmt.Printf("Processing: %s\n", s)
	})
}