mirror of
https://github.com/rjNemo/underscore
synced 2026-06-09 20:16:44 +00:00
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>
37 lines
901 B
Markdown
37 lines
901 B
Markdown
---
|
|
title: "Scan"
|
|
date: 2025-01-16T00:00:00-00:00
|
|
---
|
|
|
|
`Scan` is like Reduce but returns all intermediate accumulator values. Also known as prefix scan or cumulative fold. Useful for tracking running totals, running maximums, or other cumulative operations.
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
u "github.com/rjNemo/underscore"
|
|
)
|
|
|
|
func main() {
|
|
// Running sum
|
|
nums := []int{1, 2, 3, 4}
|
|
add := func(acc, n int) int { return acc + n }
|
|
fmt.Println(u.Scan(nums, 0, add)) // [1, 3, 6, 10]
|
|
|
|
// Running maximum
|
|
values := []int{3, 1, 4, 1, 5, 9, 2}
|
|
max := func(acc, n int) int {
|
|
if n > acc {
|
|
return n
|
|
}
|
|
return acc
|
|
}
|
|
fmt.Println(u.Scan(values, 0, max)) // [3, 3, 4, 4, 5, 9, 9]
|
|
|
|
// String concatenation
|
|
words := []string{"hello", "world", "!"}
|
|
concat := func(acc, s string) string { return acc + s }
|
|
fmt.Println(u.Scan(words, "", concat)) // ["hello", "helloworld", "helloworld!"]
|
|
}
|
|
```
|