diff options
author | Kaushal Modi <kaushal.modi@gmail.com> | 2020-02-20 13:15:34 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-20 19:15:34 +0100 |
commit | bb777fef76057d594e48848693b7032b3b97068a (patch) | |
tree | 1e7f5c337a7f05d3e730b0b78abf493d89936497 | |
parent | 479f4ca6a3d02a85eb26243403ada518b341f430 (diff) | |
download | Nim-bb777fef76057d594e48848693b7032b3b97068a.tar.gz |
Add `sequtils.unzip` to complement `sequtils.zip` (#13429)
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | lib/pure/collections/sequtils.nim | 15 |
2 files changed, 16 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md index ad18bec6b..ce47249a8 100644 --- a/changelog.md +++ b/changelog.md @@ -61,7 +61,7 @@ - Added `wrapnils` module for chains of field-access and indexing where the LHS can be nil. This simplifies code by reducing need for if-else branches around intermediate maybe nil values. E.g. `echo ?.n.typ.kind` -- Added `minIndex` and `maxIndex` to the `sequtils` module +- Added `minIndex`, `maxIndex` and `unzip` to the `sequtils` module. - Added `os.isRelativeTo` to tell whether a path is relative to another - Added `resetOutputFormatters` to `unittest` diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index d56742ba9..ac2201377 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -281,6 +281,21 @@ when (NimMajor, NimMinor) <= (1, 0): else: zipImpl(s1, s2, seq[(S, T)]) +proc unzip*[S, T](s: openArray[(S, T)]): (seq[S], seq[T]) {.since: (1, 1).} = + ## Returns a tuple of two sequences split out from a sequence of 2-field tuples. + runnableExamples: + let + zipped = @[(1, 'a'), (2, 'b'), (3, 'c')] + unzipped1 = @[1, 2, 3] + unzipped2 = @['a', 'b', 'c'] + assert zipped.unzip() == (unzipped1, unzipped2) + assert zip(unzipped1, unzipped2).unzip() == (unzipped1, unzipped2) + result[0] = newSeqOfCap[S](s.len) + result[1] = newSeqOfCap[T](s.len) + for elem in s: + result[0].add(elem[0]) + result[1].add(elem[1]) + proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = ## Splits and distributes a sequence `s` into `num` sub-sequences. ## |