-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
NonEmptySet data structure? #123
Comments
an issue with NonEmptySet(1, Set(1)) |
@julien-truffaut if you take a look at the gist that @refried linked, the I think something like this could be useful. I think I would prefer that it is its own class that could potentially use I could see two siblings of this existing: one for std I'm curious what others think about this. Have you run into the need for a non-empty set? |
@ceedubs ah good point, I missed the constructor logic. I agree with you, I think we should not expose that |
@ceedubs whoops, yeah; it didn't occur to me that the constructor logic could be side/stepped. I had just written it this way in my application to use all the OneAnd instances for free. Having a real class addresses the syntax quirk too. |
If we aren't going to expose the underlying structure, is an implementation using |
Sorry, I didn't understand the second part (just using Do you mean just duplicate the functionality of But, it think that getting the internally- |
No wait, maybe you mean, just use Set, and use the new constructor to require an element. Maybe the only difference in that case would be reusing |
Right I meant something like: class NonEmptySet[A] private[cats] (val toSet: Set[A]) extends Function1[A, Boolean] {
def size: Int = toSet.size
def apply(a: A): Boolean = toSet(a)
def map[B](f: A => B): NonEmptySet[B] = new NonEmptySet(toSet.map(f))
def filter(f: A => Boolean): Option[NonEmptySet[A]] = NonEmptySet(toSet.filter(f))
def +(a: A): NonEmptySet[A] = new NonEmptySet(toSet + a)
def -(a: A): Option[NonEmptySet[A]] = NonEmptySet(toSet - a)
def |(that: NonEmptySet[A]): NonEmptySet[A] = new NonEmptySet(toSet | that.toSet)
def &(that: NonEmptySet[A]): Option[NonEmptySet[A]] = NonEmptySet(toSet & that.toSet)
// and so on...
}
object NonEmptySet {
def apply[A](as: A*): Option[NonEmptySet[A]] =
if (as.isEmpty) None else Some(new NonEmptySet(as))
} Since On the other hand, using |
what the benefit of having |
@non I'll take a crack at it. Maybe have both? |
@julien-truffaut The only real advantage I saw was that you can use sets directly in place of a |
I have nothing against having an |
What is the status of this? I see that the issue is closed but I don't quite follow why. I encounter NonEmptySets all the time. |
BTW, I think it's possible to just use class NonEmptySet[T] private(private val underying: immutable.Set[T]) extends AnyVal {
// def methods here...
}
object NonEmptySet {
def apply[T](set: Set[T]): Option[NonEmptySet[T]] = {
if (set.isEmpty) {
None
} else {
Some(new NonEmptySet(set))
}
}
def apply[T](elem: T) = new NonEmptySet[T](Set(elem))
} I didn't care about any convariant/contravariant stuff in this code ( |
@boggle the issue is open, check out the upper-left corner. |
Oh indeed, I confused it with the closed status of the quoted other issue |
I'd like to give this another shot, what does everyone think about using |
Hi, |
Hi @channingwalton, I think it looks really good, maybe we could wrap a |
Hi.
That’s a good idea. I’ll do that.
Regards,
Lance
… On 5 Nov 2017, at 15:26, LukaJCB ***@***.***> wrote:
Hi @channingwalton <https://github.com/channingwalton>, I think it looks really good, maybe we could wrap a SortedSet with an Order[A] instead of the Set based on universal equality :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#123 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAcMr-ARWySVVsZAAwCIWZcgQ4hPHN-4ks5szdOqgaJpZM4DdUFQ>.
|
Hi. I've looked at the code for NonEmptyList and we probably want to make NonEmptySet consistent with that. I'll do some work on this over the next week or so. |
@lancewalton This looks awesome! Thanks so much for working on it. |
This was resolved (at least for sorted sets) by #2143. |
I don't have a good sense for whether something like this belong's in cats, but I have a quick and dirty scalaz-based NonEmptySet[A] = OneAnd[A,Set] implementation that I'd be happy to contribute:
https://gist.github.com/refried/9b68a08ff24640b86fb2
Obvious issues that I can do something about:
Let me know what you think.
I've been using it with that argmaxes method, and to back a categorical probability distribution (generalized Bernoulli distribution) (must have at least one element) which I'm also happy to contribute if it makes sense here.
The text was updated successfully, but these errors were encountered: