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

Adds toSet to create sets from iterables #16276

Merged
merged 15 commits into from
Dec 14, 2020
Merged

Adds toSet to create sets from iterables #16276

merged 15 commits into from
Dec 14, 2020

Conversation

beef331
Copy link
Collaborator

@beef331 beef331 commented Dec 7, 2020

Replicates the toHashSet proc for normal sets.

lib/system/setops.nim Outdated Show resolved Hide resolved
lib/system/setops.nim Outdated Show resolved Hide resolved
lib/system/setops.nim Outdated Show resolved Hide resolved
@beef331
Copy link
Collaborator Author

beef331 commented Dec 7, 2020

Thank you for all your comments/insight, the latest commit should check all of the boxes, atleast as far as I know.

lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
@beef331
Copy link
Collaborator Author

beef331 commented Dec 7, 2020

I'm inept with the rebase :D

@beef331
Copy link
Collaborator Author

beef331 commented Dec 7, 2020

There we go this should be fine now, thanks.

lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
assert toSet([false]) == {false}
assert toSet(0u8..10u8) == {0u8..10u8}
type E = elementType(iter)
static: doAssert E is SetElement, "`iter` does not yield a `SetElement`"
Copy link
Member

Choose a reason for hiding this comment

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

actually, why do we even need the static: doAssert E is SetElement ?
you'll still get an error when attempting var result: set[E] if say, E is int64.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well the main reason is to give a slightly more informative error instead of just set is to large

Copy link
Member

@timotheecour timotheecour Dec 7, 2020

Choose a reason for hiding this comment

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

then we should fix the errmsg in the compiler, where it belongs (which would benefit other code):

nim> var z: set[int]
Error: set is too large

=>

Error: set is too large, got `int` but expected ...

tests/stdlib/tsetutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
@timotheecour
Copy link
Member

timotheecour commented Dec 7, 2020

LGTM, thanks! I restarted CI, you're hitting windows flaky test timotheecour#411 (EDIT: reported it as #16317)

@timotheecour
Copy link
Member

now you're hitting a different CI issue I haven't seen before (timotheecour#441); I restarted failing job

@beef331
Copy link
Collaborator Author

beef331 commented Dec 9, 2020

Leave it to me to hit issues no one has hit before.

@timotheecour timotheecour changed the title Adds toSet to create sets from openArrays Adds toSet to create sets from iterables Dec 9, 2020
@timotheecour timotheecour mentioned this pull request Dec 9, 2020
4 tasks
@timotheecour
Copy link
Member

just bikeshedding but another candidate name would be:
std/builtinsets to avoid confusion with existing (and poorly named) std/sets

I'm ok with either, though

@beef331
Copy link
Collaborator Author

beef331 commented Dec 9, 2020

Do you mean builtinsets or builtinsetutils? The former may cause confusion with where set[T] comes from.

@timotheecour
Copy link
Member

The former may cause confusion with where set[T] comes from.

well by definition they're builtin ;-) ; builtinsetutils is a bit of a mouthful... maybe setutils is good enough

@Araq
Copy link
Member

Araq commented Dec 10, 2020

So ... rather than {'a', 'b', 'c'} you want to write toSet ['a', 'b', 'c']? Doesn't seem useful to me. What's the use case? And now don't say "generic programming", that is not a use case.

@timotheecour
Copy link
Member

rather than {'a', 'b', 'c'} you want to write toSet ['a', 'b', 'c']?

intended use case is not for cases where input is a litteral (in which case you'd write {'a', 'b', 'c'} directly), just like intended use case for toSeq (or toHashSet, etc) is not for cases where input is a literal. Instead, it's for cases where input is some iterable, which includes strings/seq/array/iterator call etc.

examples:

let input: string = getInput()
let uniqueLetters = input.toSet # or: callFn(input.toSet)

I've often needed this myself as well, it fits well with the rest of stdlib:

  • toSeq
  • toHashSet
  • toPackedSet
  • toIntSet
    etc...

to the point that not having toSet seems like a weird omission

Copy link
Contributor

@ee7 ee7 left a comment

Choose a reason for hiding this comment

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

Some nitpicks.

lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
tests/stdlib/tsetutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
lib/std/setutils.nim Outdated Show resolved Hide resolved
@timotheecour
Copy link
Member

timotheecour commented Dec 10, 2020

@beef331 you can click "Resolve Conversation" on each item that was resolved (unless there's a disagreement) :)

@timotheecour
Copy link
Member

hitting #16317 again; I've restarted failing tests

@timotheecour
Copy link
Member

I restarted again;
you're hitting #16317 again (which is recently very flaky) and also #16315 (which has been fixed already)

This is why flaky tests really need to be fixed..

@Araq
Copy link
Member

Araq commented Dec 14, 2020

Needs a changelog entry and maybe (!) a ```--define:nimHasSetUtils`` (edit compiler/condsysm.nim to add it).

@timotheecour
Copy link
Member

timotheecour commented Dec 14, 2020

Needs a changelog

indeed

and maybe (!) a ```--define:nimHasSetUtils`` (edit compiler/condsysm.nim to add it).

this hasn't been needed for other new modules, and in fact will be obsoleted by whichModule timotheecour#376 once I get to it (it's not hard); plus, condsysm.nim is usually about the nim binary, not the stdlib; the distinction matters because of --lib flag etc

changelog.md Outdated
@@ -57,6 +57,8 @@

- `strscans.scanf` now supports parsing single characters.

- Added `setutils.toSet` that can take any iteratable and convert it to a built-in set,
if the iteratable yields a built-in settable type.
Copy link
Member

Choose a reason for hiding this comment

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

iteratable doesn't seem enligh; should be iterable

@timotheecour timotheecour merged commit 1082d62 into nim-lang:devel Dec 14, 2020
@timotheecour
Copy link
Member

merged; previous CI was green and last commit just changed changelog.

mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
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