136 Commits

Author SHA1 Message Date
Samuel Berthe b11e461ff4 perf: preallocate maps and slices in CountValues, UniqKeys, UniqValues, FilterKeys, FilterValues, FilterKeysErr, FilterValuesErr (#833)
* perf: preallocate maps and slices in CountValues, UniqKeys, UniqValues, FilterKeys, FilterValues, FilterKeysErr, FilterValuesErr

Add size hints to map and slice allocations to avoid repeated
grow-and-copy reallocations:
- CountValues: make(map[T]int, len(collection))
- UniqKeys/UniqValues: make([]K, 0, size)
- FilterKeys/FilterValues/FilterKeysErr/FilterValuesErr: make([]K, 0, len(in))

* oops
2026-03-06 00:35:45 +01:00
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00
Samuel Berthe dc372c082b perf: use direct index assignment in RepeatBy, Fill, Repeat (#834)
Replace make([]T, 0, count) + append with make([]T, count) + result[i] = ...
since the final size is known upfront. Eliminates per-element append overhead.

Also adds comprehensive benchmark coverage for all helpers across
core, it, mutable, and parallel packages (282 benchmark functions).
2026-03-06 00:04:16 +01:00
Samuel Berthe e82e7f5d7d perf: avoid double iteration in UniqMap (#829)
UniqMap previously built a map then called Keys(seen) which:
1. Iterated the map a second time to extract keys
2. Returned keys in non-deterministic map iteration order
3. Allocated a separate slice inside Keys()

Now collects results directly during the single pass, preserving
insertion order and eliminating the redundant iteration.
2026-03-05 18:53:43 +01:00
Samuel Berthe f7891eb41c perf: preallocate result slice in Reject, RejectErr, RejectMap (#827)
* perf(Reject): preallocate result slice capacity

Reject was using `Slice{}` (zero capacity) causing multiple
grow-and-copy allocations as elements were appended. Preallocate
with `make(Slice, 0, len(collection))` to eliminate regrowth.

benchstat (strings_1000): -42.96% time, -53.37% memory, -88.89% allocs

* perf(Reject): preallocate result slice in Reject, RejectErr, RejectMap

All three Reject variants were using zero-capacity slices (`Slice{}`
or `[]R{}`), causing multiple grow-and-copy allocations as elements
were appended. Preallocate with `make(..., 0, len(collection))` to
eliminate slice regrowth.

Reject benchstat (strings_1000): -42.96% time, -53.37% memory, -88.89% allocs
RejectErr benchstat (strings_1000): -45.11% time, -53.37% memory, -88.89% allocs
RejectMap benchstat (strings_1000): -47.63% time, -53.37% memory, -88.89% allocs
2026-03-05 18:12:24 +01:00
Samuel Berthe af46a13bfc feat: adding RejectErr helpers 2026-03-02 16:06:51 +01:00
Samuel Berthe ff0e293ce3 feat: adding FilterErr helpers 2026-03-02 16:06:51 +01:00
Samuel Berthe 4bb58fd2c6 feat: adding RepeatByErr helpers 2026-03-02 16:06:51 +01:00
Samuel Berthe 64b0378b52 fix: fix inconsistent behavior on early return (return zero value instead of partial result) 2026-03-02 16:06:51 +01:00
Samuel Berthe 1db8f69a48 feat: adding KeyByErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 1342d124d5 feat: adding PartitionByErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 0950850fe2 feat: adding GroupByMapErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 75474ff444 feat: adding GroupByErr+CountByErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe b0b34ffc94 feat: adding UniqByErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 361866b4d4 feat: adding ReduceRightErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 20d76196f2 feat: adding ReduceErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe 75d813d292 feat: adding FlatMapErr helper 2026-03-02 16:06:51 +01:00
Samuel Berthe ec67399cd4 feat: adding MapErr helper 2026-03-02 16:06:51 +01:00
d-enk c70160efcd fix: correct DropByIndex handling of negative indices out of bounds (#778)
Fixed DropByIndex function for the case when a negative index exceeds
the slice size. This caused subsequent indices to have incorrect
offsets, leading to incorrect results.

Example of the bug:
  DropByIndex([]int{0, 1, 2, 3, 4}, -100, 4)
  returned []int{0, 1, 2, 4} instead of []int{0, 1, 2, 3}

Improvements:
- Removed map-based uniqueness check, now uses sorted iteration with O(1) memory
- Added copy of indexes to prevent mutation of input
- Fixed negative index handling logic
- Improved range check using uint for proper bounds validation

Performance:
- Time: -75.61% (geomean)
- Memory: -62.65% (geomean)
- Allocations: -43.67% (geomean)

Additional:
- Optimized Filter and FilterI in mutable/slice.go
2026-02-22 04:27:07 +01:00
d-enk ac3221dd22 refactor: improve Slice logic and fix docstring (#785)
- Fix docstring: clarify Slice returns a slice view, not a copy
- Reorder boundary checks to use else-if
- Add test case for negative start and end indices
2026-02-08 02:31:32 +01:00
d-enk b6d1bf3749 refactor: simplify slice cut/trim prefix/suffix functions (#787)
- CutPrefix now uses HasPrefix instead of manual comparison loop
- CutSuffix now uses HasSuffix instead of manual comparison loop
- TrimPrefix simplified with HasPrefix condition in for loop
- TrimSuffix simplified with HasSuffix condition in for loop

This reduces code duplication and improves readability.
2026-01-27 18:57:30 +01:00
d-enk d8c723125a perf: optimize Sliding by pre-allocating result capacity (#783)
Reducing repeated calculations
2026-01-22 17:49:30 +01:00
d-enk 7a0a18b3a4 perf: optimize PartitionBy by eliminating redundant append (#765)
New groups are now initialized with the first element directly,
avoiding an extra append operation in the hot loop.

Use a one style for lo/it/parallel
2026-01-12 20:58:04 +01:00
Yuliya b6154d0f8d feat: add Take, TakeWhile, FilterTake, Window, and Sliding functions (#760)
* feat: add Take, TakeWhile, FilterTake, Window, and Sliding functions

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

* improve Take function safety and add benchmarks

* rename FilterTake to TakeFilter and add README docs

* remove Window and Sliding benchmarks

* apply gofmt and fix linter errors

* apply gofmt and fix linter errors
2026-01-07 17:28:08 +01:00
quexer dc14b52cfb fix: improve Clone function to preserve nilness and avoid liveness issues (#740) 2025-11-07 15:59:21 +01:00
Nathan Baulch e7386d9246 lint: fix inconsistent callback function parameter names (#730)
* Fix linting

* Use is.ElementsMatch

This will ignore the ordering of the final intersection. Especially
important when checking old versions of go that do not guarantee an order
when iterating through maps.

* lint: fix inconsistent callback function parameter names

* lint: rename "iteratee" to "transform" for *Map helpers

* lint: rename "project" parameters to "transform"

* lint: rename "cb" parameters to "callback"

* lint: rename "iteratee" to "callback" for ForEach helpers

---------

Co-authored-by: Franky W. <frankywahl@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 18:05:11 +01:00
Nathan Baulch 48d8fe40e5 fix: rename IsSortedByKey to IsSortedBy (#735)
* Fix linting

* Use is.ElementsMatch

This will ignore the ordering of the final intersection. Especially
important when checking old versions of go that do not guarantee an order
when iterating through maps.

* fix: rename IsSortedByKey to IsSortedBy

---------

Co-authored-by: Franky W. <frankywahl@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 17:54:13 +01:00
quexer 41f7af2114 Add Clone function to return shallow copy of slice collections (#732)
* Initial plan

* Add Clone function to return shallow copy of slice collection

Co-authored-by: quexer <92157+quexer@users.noreply.github.com>

* Update slice.go

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>

* Update slice_test.go

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>

* doc and test

* Apply suggestions from code review

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: quexer <92157+quexer@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 17:47:04 +01:00
Samuel Berthe 734a6b156c feat(it): adding loit.Concat (#722) 2025-10-25 21:11:08 +02:00
Felipe Gasper f0d321a941 Add a Concat slice function. (#714)
* Add a Concat slice function.

This is a variadic wrapper around Flatten that allows callers
to assemble one slice from many slices without first joining
them into a slice-of-slices.

* Update slice_test.go

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-10-25 20:43:15 +02:00
Ivan Volkov 3a67a3fdd2 feat: Optimize UniqMap to reduce unnecessary slice preallocation (#710)
* feat: Optimize UniqMap to reduce unnecessary slice preallocation

* Update slice_example_test.go

* Apply suggestions from code review

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-10-12 12:56:52 +02:00
Marat Abrarov 7d7222b803 feat: from slice/iterator to map with index passed (#698)
* feat: implementation of conversion from slice to map without index passed to transformation function through the similar conversion but with index passed to transformation function.

Signed-off-by: Marat Abrarov <abrarov@gmail.com>

* feat: implementation of conversion from iterator to map without index passed to transformation function through the similar conversion but with index passed to transformation function.

Signed-off-by: Marat Abrarov <abrarov@gmail.com>

---------

Signed-off-by: Marat Abrarov <abrarov@gmail.com>
2025-10-03 02:05:55 +02:00
Marat Abrarov 71b5295007 feat: conversion from slice to map with index passed to transformation function (https://github.com/samber/lo/issues/601). (#697)
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
2025-10-03 00:43:06 +02:00
Nathan Baulch f5aac81cc0 lint: enable gofumpt extra rules (#690)
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-09-26 13:32:59 +02:00
Nathan Baulch 1df48c4a2a perf: avoid Keyify twice in Trim (#689)
* perf: avoid Keyify twice in Trim

* pref: avoid two slice allocations in Trim
2025-09-26 13:27:23 +02:00
Nathan Baulch 2911f93956 fix: panic when passing -1 to Drop (#688) 2025-09-25 13:11:52 +02:00
Samuel Berthe ec86b574ed feat: adding TrimXXX methods (#683) 2025-09-25 04:15:30 +02:00
Nathan Baulch 52d31f788f lint: fix CI warnings and avoid named return parameters (#682) 2025-09-25 03:44:33 +02:00
Alex Klepov 21a523ddc0 Added Cut, CutPrefix, CutSuffix (#666)
* Added Cut, CutPrefix, CutSuffix

* fix readme

* Clarify Cut, CutPrefix, and CutSuffix documentation

Updated descriptions and examples for Cut, CutPrefix, and CutSuffix functions to clarify usage with slices.

* Refactor Cut, CutPrefix, and CutSuffix functions

* Update slice_test.go

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-09-25 01:52:48 +02:00
Nathan Baulch 41cbf0fd2b feat: preserve type alias in DropByIndex and WithoutBy (#675)
* lint: pin golangci-lint version

* feat: preserve type alias in WithoutBy

* feat: preserve type alias in DropByIndex

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-09-24 22:57:40 +02:00
Nathan Baulch b5e290abe0 fix: more consistent panic strings (#678)
* lint: pin golangci-lint version

* fix: more consistent panic strings

* Update golangci-lint version in workflow

Updated golangci-lint action version to v2.4.

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-09-24 21:02:02 +02:00
Nathan Baulch 3e11f11781 docs: grammar improvements (#673)
* lint: pin golangci-lint version

* docs: grammar fixes

* docs: remove "truthy" terminology

* docs: remove "array" terminology

* docs: grammar fixes

* Update .github/workflows/lint.yml

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-09-24 20:59:41 +02:00
Samuel Berthe 3bc887c57c doc: adding go playground examples 2025-09-23 14:54:30 +02:00
Samuel Berthe 9c8308ffda Style add golangci config (#670)
* style(lint): gofumpt

* style(lint): errcheck

* style(lint): revive

* style(lint): gocritic

* style(lint): forcetypeassert

* style(lint): add .golangci.yml

* oops
2025-09-20 01:37:51 +02:00
Samuel Berthe f8024de2ce fix(chunk): Copy chunk in a new slice (#648)
Prevents memory leak and free memory from initial collection.
2025-07-06 15:33:18 +02:00
Artem Mikheev 38f67506fe optimization: preallocate result in FilterMap (#622)
Co-authored-by: Artem Mikheev <renbou@pinely.com>
2025-04-26 17:34:10 +02:00
overbeck 0fa0632c0f Perf(slice): Optimize iteration function parameters and add test cases (#587)
- Modify the iteration function parameters in the UniqueBy, GroupByMap, and Keyify functions to use index access for collection elements.
- Optimize parameter naming in the GroupByMap function to improve code readability.
- Add test cases for the GroupByMap function to cover different types of inputs.

Co-authored-by: ShuQingZai <overbeck.jack@outlook.com>
2025-02-17 00:10:10 +01:00
Samuel Berthe ce12685a40 fix(groupbymap): remove second iteratee and add test+example+doc 2025-01-28 20:25:04 +01:00
Ran Mariuma 19cb99199d Implemented GroupByMapValues 2025-01-28 20:12:52 +01:00
Samuel Berthe 124d3004de feat: adding FilterSliceToMap (#581) 2025-01-26 23:32:59 +01:00