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
11 changes: 11 additions & 0 deletions lib/system/setops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ func incl*[T](x: var set[T], y: T) {.magic: "Incl".} =
a.incl(4)
assert a == {1, 2, 3, 4, 5}

func toSet*[char|byte|bool|int16|uint16|enum](ar: openArray[T]): set[T] =
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
##Iterates through an openArray making a set from it.
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
runnableExamples:
assert "helloWorld".toSet == {'W', 'd', 'e', 'h', 'l', 'o', 'r'}
assert [10u16,20,30].toSet == {10u16, 20, 30}
assert [30u8,100,10].toSet == {10u8, 30, 100}
assert @[1321i16,321, 90].toSet == {90i16, 321, 1321}
assert [false].toSet == {false}
for x in ar:
result.incl(x)

template incl*[T](x: var set[T], y: set[T]) =
## Includes the set `y` in the set `x`.
runnableExamples:
Expand Down