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

Split truthy into sub-packages #2

Closed
wants to merge 6 commits into from
Closed

Split truthy into sub-packages #2

wants to merge 6 commits into from

Conversation

jhbforlife
Copy link

I was excited to find this package! I would find it very useful to have this type of functionality so thought I would add it.

@earthboundkid
Copy link
Owner

Thanks. I have another package with pointer.Deref. I wonder if they should just be combined? Seems related.

@earthboundkid
Copy link
Owner

@jhbforlife
Copy link
Author

I definitely think that could work! I can mess around with what that could look like later today

@jhbforlife
Copy link
Author

@carlmjohnson just pushed an update! I may have gone overboard but I actually kind of like having everything separated like this - it makes it clearer to me what exactly I'm trying to use

@jhbforlife jhbforlife changed the title Added ValueDeref Split truthy into sub-packages Jan 9, 2024
is/is.go Outdated Show resolved Hide resolved
is/is.go Outdated
// TruthySlice returns true if the length of the slice is greater than 0.
// Note that it does not distinguish nil slices from empty slices.
func TruthySlice[T any, S ~[]T](v S) bool {
return len(v) > 0
Copy link
Owner

Choose a reason for hiding this comment

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

is.TruthySlice(s) is longer than len(s) > 0. Is the clarity worth it? Maybe if it were a little shorter?

Copy link
Author

Choose a reason for hiding this comment

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

what do you think about e66db9d?

import "github.com/carlmjohnson/truthy/is"

// GetFirst returns the first value in vs which is non-zero.
func GetFirst[T comparable](vs ...T) (t T) {
Copy link
Owner

Choose a reason for hiding this comment

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

Go 1.22 will have cmp.Or (which I added 😊) that does this same thing, so it's kind of redundant to have it here. OTOH, FirstAny is still useful.

Copy link
Author

Choose a reason for hiding this comment

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

hmm... could it just be removed once it's officially live in Go? that's pretty sweet though

@earthboundkid
Copy link
Owner

What if the subpackages were bools and pointers? The idea is the standard library has strings, bytes, slices, maps… and we're adding bools and pointers. The bools package would have stuff about truthiness and pointers would be what you have here.

@earthboundkid
Copy link
Owner

A bools package could also have bools.Int like this proposal: golang/go#64825.

Copy link
Owner

@earthboundkid earthboundkid left a comment

Choose a reason for hiding this comment

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

Overall, it looks good, but since this is supposed to just be a convenience thing, I think we should push the names to be short as we can without losing clarity. Name suggestions:

  • Rename package pointers to ptrs. I think ptr is used as a variable too much to be a package name, but ptrs should be free to use, and it's much shorter.
  • bools.Comparable to bools.Value.
  • bools.GetFirst and bools.GetFirstAny to bools.Or and bools.AnyOr.
  • bools.SetFirst and SetFirstAny to bools.Set and bools.AnySet.
  • bools.Ternary to bools.If.

What do you think?

@jhbforlife jhbforlife closed this Mar 4, 2024
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