Skip to content

Conversation

@juliazadorozhnaya
Copy link
Contributor

Add five new slice manipulation functions with tests, examples, and documentation.

New Functions

Take

Safely takes the first n elements from a slice. Handles edge cases and creates a copy to avoid mutating the original.

Production use cases:

  • Pagination: Get first page of results
  • Top N items: Extract top 10 users, products, or metrics
  • Batch processing: Process first 100 tasks from queue

TakeWhile

Takes elements from the beginning while the predicate returns true. Useful for processing ordered data until a condition is met.

Production use cases:

  • Process successful transactions until first failure
  • Parse valid tokens until first invalid one

FilterTake

Filters elements and takes the first n matches. More efficient than chaining Filter and Take, as it stops after finding n matches.

Production use cases:

  • Search: Find first 5 active users (stops early, doesn't scan entire list)

Window

Creates overlapping sliding windows of a given size (step=1). Each window overlaps with the previous one by size-1 elements.

Production use cases:

  • Time series analysis: Calculate 7-day moving averages
  • Anomaly detection: Compare adjacent time periods

Sliding

Creates sliding windows with configurable step size. More flexible than Window - can create overlapping or non-overlapping windows.

Production use cases:

  • Batch processing: Process data in chunks (step=size, like Chunk)
  • Aggregation: Calculate averages over 5-minute windows with 1-minute steps
  • Sampling: Extract every 10th element in batches

Add five new slice manipulation functions with tests, examples, and documentation.
@juliazadorozhnaya juliazadorozhnaya marked this pull request as draft January 6, 2026 17:40
Copy link
Owner

@samber samber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this first contribution.

Please add the description of the helper to the README. The documentation website is still young. I'm going to remove this legacy doc as soon as the SEO improves.

I suppose we can create similar helpers in subpackages and especially it, but we can do it in a follow-up PR.

@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

❌ Patch coverage is 96.29630% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.05%. Comparing base (96bff97) to head (a1c353f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
slice.go 96.29% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #760   +/-   ##
=======================================
  Coverage   94.04%   94.05%           
=======================================
  Files          18       18           
  Lines        2840     2894   +54     
=======================================
+ Hits         2671     2722   +51     
- Misses        152      154    +2     
- Partials       17       18    +1     
Flag Coverage Δ
unittests 94.05% <96.29%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Owner

@samber samber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except 1 comment, it seems good to me.

Do you plan to add more things on this PR ? Would you like to remove the draft tag ?

Edit: Please fix the linter error

@samber
Copy link
Owner

samber commented Jan 7, 2026

Please note that some of your helpers are already implemented in https://github.com/samber/ro, with a streaming paradigm.

@juliazadorozhnaya juliazadorozhnaya marked this pull request as ready for review January 7, 2026 15:53
@samber samber merged commit b6154d0 into samber:master Jan 7, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants