mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 10:36:43 +00:00
Document that Last panics on empty slices with a clear error message. Add examples for single element and empty slice cases. Related to Issue 13 (PR #41) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
901 B
901 B
| title | date |
|---|---|
| Scan | 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.
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!"]
}