57 lines
1.7 KiB
Markdown
57 lines
1.7 KiB
Markdown
# sieve
|
|
|
|
Generic, dependency-free collection helpers that the standard library slices and maps packages lack: mapping, filtering, reducing, keying, grouping, deduplicating, partitioning and finding, plus a small set type.
|
|
|
|
`import "git.golem15.com/golem15/summercms/modules/sieve"`
|
|
|
|
**Name:** A sand sieve: you shake a collection through it and keep what you need; map, filter and reduce for slices and maps.
|
|
|
|
## Overview
|
|
|
|
`sieve` gives plain Go slices the collection operations that WinterCMS code reaches for on Laravel collections. Every function is eager and never modifies its input, results never share a backing array with the input, callbacks run once per element in input order, and nil or empty input yields a non-nil empty result, so `encoding/json` writes `[]` rather than `null`.
|
|
|
|
## Role in the framework
|
|
|
|
A dependency-free leaf with no imports, so any module, plugin or application can use it without import cycles.
|
|
|
|
## Features
|
|
|
|
- `sieve.Map` applies a function to every element and returns the results in order; with a field accessor it is the pluck operation.
|
|
|
|
## Usage
|
|
|
|
```go
|
|
package blog
|
|
|
|
import "git.golem15.com/golem15/summercms/modules/sieve"
|
|
|
|
type Post struct {
|
|
ID int
|
|
Title string
|
|
}
|
|
|
|
func titles(posts []Post) []string {
|
|
return sieve.Map(posts, func(p Post) string { return p.Title })
|
|
}
|
|
```
|
|
|
|
## API reference
|
|
|
|
| Identifier | Description |
|
|
|------------|-------------|
|
|
| `sieve.Map` | Returns f applied to each element, in input order. Nil input gives a non-nil empty slice. |
|
|
|
|
## Dependencies
|
|
|
|
- SummerCMS modules: none.
|
|
- Third-party: none.
|
|
- Standard library: none (the package has no imports).
|
|
|
|
## Testing
|
|
|
|
```sh
|
|
go test ./modules/sieve/...
|
|
```
|
|
|
|
The tests are in-memory and need no external services.
|