-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9883f44
Added toSet, converts from openArray of set[T] valid Ts
beef331 4ccb063
more accurate description of toSet
beef331 d39f63e
Moved toSet into and made it a template that can take anything itera…
beef331 fa65dbb
Added all <16bit ordinals
beef331 0991f40
Cleaned up sequtils and added tsequtils.nim
beef331 cfdfc38
Further cleaned up to be more correct
beef331 57bf2c4
Added doc comment to setelement
beef331 fd79571
removed typo/ redundant example
beef331 601cd00
Made debug message more informational
beef331 5ad0475
Added distinct char test, and removed useless check
beef331 feeff8d
commented out setelement
beef331 456946d
Updated tsetutils to match request
beef331 abefb17
Nitpickyness cleaned
beef331 157b361
Added setutils to changelog
beef331 3ec9dad
Iterable is english
beef331 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# | ||
# | ||
# The Nim Compiler | ||
# (c) Copyright 2020 Nim Contributors | ||
# | ||
# See the file "copying.txt", included in this | ||
# distribution, for details about the copyright. | ||
# | ||
|
||
## This module adds functionality to the built-in `set` type. | ||
## See also std/packedsets, std/sets | ||
|
||
import typetraits | ||
|
||
#[ | ||
type SetElement* = char|byte|bool|int16|uint16|enum|uint8|int8 | ||
## The allowed types of a built-in set. | ||
]# | ||
|
||
template toSet*(iter: untyped): untyped = | ||
## Return a built-in set from the elements of iterable `iter` | ||
runnableExamples: | ||
timotheecour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert "helloWorld".toSet == {'W', 'd', 'e', 'h', 'l', 'o', 'r'} | ||
assert toSet([10u16, 20, 30]) == {10u16, 20, 30} | ||
assert [30u8, 100, 10].toSet == {10u8, 30, 100} | ||
assert toSet(@[1321i16, 321, 90]) == {90i16, 321, 1321} | ||
assert toSet([false]) == {false} | ||
assert toSet(0u8..10) == {0u8..10} | ||
var result: set[elementType(iter)] | ||
for x in iter: | ||
incl(result, x) | ||
result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
discard """ | ||
targets: "c js" | ||
""" | ||
import std/setutils | ||
timotheecour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
template main = | ||
doAssert "abcbb".toSet == {'a', 'b', 'c'} | ||
doAssert toSet([10u8, 12, 13]) == {10u8, 12, 13} | ||
doAssert toSet(0u16..30) == {0u16..30} | ||
type A = distinct char | ||
doAssert [A('x')].toSet == {A('x')} | ||
|
||
main() | ||
static: main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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