diff options
author | Jason Beetham <beefers331@gmail.com> | 2020-12-14 16:20:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-14 15:20:21 -0800 |
commit | 1082d62b089e54a584e15b0f97a4b247e214d9ef (patch) | |
tree | 6a84846b1b9e82b695ff69e5c0e8c48da31d5549 /lib/std/setutils.nim | |
parent | 38eb021f81589c11453bf5b175f4395d6b25ef86 (diff) | |
download | Nim-1082d62b089e54a584e15b0f97a4b247e214d9ef.tar.gz |
Adds toSet to create sets from iterables (#16276)
Diffstat (limited to 'lib/std/setutils.nim')
-rw-r--r-- | lib/std/setutils.nim | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/setutils.nim b/lib/std/setutils.nim new file mode 100644 index 000000000..c6385180e --- /dev/null +++ b/lib/std/setutils.nim @@ -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: + 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 |