Skip to content

feat(gslice): add ToBoolMap to collect slice elements into a set (map[T]bool)#20

Merged
XQ-Gang merged 4 commits intobytedance:mainfrom
GarrickZ2:feature/slice_to_set
Jul 30, 2025
Merged

feat(gslice): add ToBoolMap to collect slice elements into a set (map[T]bool)#20
XQ-Gang merged 4 commits intobytedance:mainfrom
GarrickZ2:feature/slice_to_set

Conversation

@GarrickZ2
Copy link
Copy Markdown
Contributor

Add ToSet for converting slice to map[T]bool

What

This PR adds a generic utility function ToSet, which converts a slice of comparable elements into a map[T]bool — commonly used in Go as a set.

It builds on top of ToMap but provides a more ergonomic API for a frequent use case.

Why

In Go, it’s common to use map[T]bool as a set. While ToMap can technically be used to construct such sets:

ToMap(s, func(x T) (T, bool) { return x, true })

this approach is verbose and repetitive, especially for a pattern that’s so frequently needed in practice.

ToSet eliminates the need for writing an anonymous function every time, and makes the intent clearer at callsite.

Motivation

  • Provides a lightweight and idiomatic way to create sets in Go
  • Eliminates boilerplate for common set creation patterns
  • Aligns with similar patterns in other languages:
    • toSet() in Python
    • setOf() in Kotlin

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jul 22, 2025

CLA assistant check
All committers have signed the CLA.

ToSet converts a slice of comparable elements into a set-like map[T]bool,
deduplicating elements and enabling fast membership checks.
Supports any comparable type including struct, alias, etc.
@GarrickZ2 GarrickZ2 force-pushed the feature/slice_to_set branch from 10ada7e to 82e9830 Compare July 22, 2025 19:28
Comment thread gslice/gslice.go Outdated
// The element type T must be comparable, as required by Go map keys.
//
// 💡 AKA: setOf(...) in Kotlin, .toSet() in Python
func ToSet[T comparable](s []T) map[T]bool {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We already have a collection/set data type, the name ToSet may be misleading. I prefer using ToBoolMap, as we already have ToMap function, and in the future we may add ToStructMap for returning map[V]strcut{}.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense — my initial thought was to mirror naming from languages like Kotlin/Python (toSet), but I can see how ToBoolMap better aligns with our existing ToMap and avoids potential confusion with collection/set. I've updated the name accordingly.

Comment thread gslice/gslice.go Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.92%. Comparing base (f5f07b9) to head (0df2e15).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #20      +/-   ##
==========================================
+ Coverage   95.79%   95.92%   +0.13%     
==========================================
  Files          34       34              
  Lines        4443     4445       +2     
==========================================
+ Hits         4256     4264       +8     
+ Misses        144      139       -5     
+ Partials       43       42       -1     
Flag Coverage Δ
go-1.18.x 94.78% <100.00%> (-0.14%) ⬇️
go-1.19.x 94.78% <100.00%> (-0.14%) ⬇️
go-1.20.x 95.31% <100.00%> (+<0.01%) ⬆️
go-1.21.x 95.52% <100.00%> (-0.14%) ⬇️
go-1.22.x 95.38% <100.00%> (+0.06%) ⬆️
go-1.23.x 95.45% <100.00%> (+0.20%) ⬆️
go-1.24.x 95.38% <100.00%> (+0.20%) ⬆️
unittests 95.92% <100.00%> (+0.13%) ⬆️

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.

…ribe its behavior and usage, including examples and notes about type requirements.
Copy link
Copy Markdown
Collaborator

@SilverRainZ SilverRainZ 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 your contribution.

Comment thread README.md Outdated
@SilverRainZ SilverRainZ changed the title feat(utils): add ToSet to collect slice elements into a set (map[T]bool) feat(utils): add ToBoolMap to collect slice elements into a set (map[T]bool) Jul 29, 2025
@SilverRainZ SilverRainZ changed the title feat(utils): add ToBoolMap to collect slice elements into a set (map[T]bool) feat(gslice): add ToBoolMap to collect slice elements into a set (map[T]bool) Jul 29, 2025
@XQ-Gang XQ-Gang merged commit 420f836 into bytedance:main Jul 30, 2025
11 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.

4 participants