Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/slice abstractions #27

Draft
wants to merge 5 commits into
base: feature/generics
Choose a base branch
from

Conversation

schonmann
Copy link

@schonmann schonmann commented Feb 4, 2022

What this PR does?

Creating slice abstractions using Go generics feature.

- Before

No slice abstractions.

- After

An initial version of slice abstractions.

- How to test it?

make test

@schonmann schonmann requested a review from a team February 4, 2022 12:31
@schonmann schonmann self-assigned this Feb 4, 2022
lib/slice.go Outdated Show resolved Hide resolved
Comment on lines +9 to +17
func (slice Slice[T]) Filter(predicate Predicate[T]) Slice[T] {
filtered := Slice[T]{}
for _, v := range slice {
if predicate == nil || predicate(v) {
filtered = append(filtered, v)
}
}
return filtered
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (slice Slice[T]) Filter(predicate Predicate[T]) Slice[T] {
filtered := Slice[T]{}
for _, v := range slice {
if predicate == nil || predicate(v) {
filtered = append(filtered, v)
}
}
return filtered
}
func Filter(slice Slice[T], predicate Predicate[T]) Slice[T] {
filtered := Slice[T]{}
for _, v := range slice {
if predicate == nil || predicate(v) {
filtered = append(filtered, v)
}
}
return filtered
}

i think this type of params maybe better for utilization in code

Copy link
Author

@schonmann schonmann Feb 4, 2022

Choose a reason for hiding this comment

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

Can you be more specific on the reasons why? I aggree that this could have less friction with legacy code, but at this point we should question what is indeed idiomatic in Golang. It seems quite reasonable that generic type abstractions like Slice[T] or Map[TK,TV] would be a better choice for 1.18+ Golang code, as custom, reusable behaviour can be easily added to satistfy application needs.

Copy link
Contributor

Choose a reason for hiding this comment

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

i think is better have less friction with legacy code, cuz the community probably will make something similar to this PR in the std lib, so we can make an agnostic function that we recive any slice is better for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants