Skip to content

Commit

Permalink
Make sequtils.zip return seq of anonymous tuples
Browse files Browse the repository at this point in the history
Earlier the tuples had named fields "a" and "b" and that made it
difficult to assign the zip returned seqs to other vars which expected
seqs of tuples with field names other than "a" and "b".
  • Loading branch information
kaushalmodi committed Nov 1, 2019
1 parent 3761e62 commit 4cda53a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- `base64.encode` no longer supports `lineLen` and `newLine` use `base64.encodeMIME` instead.
- `os.splitPath()` behavior synchronized with `os.splitFile()` to return "/" as the dir component of "/root_sub_dir" instead of the empty string.

- `sequtils.zip` now returns a sequence of anonymous tuples i.e. those tuples now do not have fields named "a" and "b".

### Breaking changes in the compiler

Expand Down
22 changes: 9 additions & 13 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,13 @@ proc deduplicate*[T](s: openArray[T], isSorted: bool = false): seq[T] =
for itm in items(s):
if not result.contains(itm): result.add(itm)

proc zip*[S, T](s1: openArray[S], s2: openArray[T]): seq[tuple[a: S, b: T]] =
proc zip*[S, T](s1: openArray[S], s2: openArray[T]): seq[(S, T)] =
## Returns a new sequence with a combination of the two input containers.
##
## The input containers can be of different types.
## If one container is shorter, the remaining items in the longer container
## are discarded.
##
## For convenience you can access the returned tuples through the named
## fields `a` and `b`.
##
runnableExamples:
let
short = @[1, 2, 3]
Expand All @@ -223,10 +220,9 @@ proc zip*[S, T](s1: openArray[S], s2: openArray[T]): seq[tuple[a: S, b: T]] =
zip3 = zip(long, letters)
assert zip1 == @[(1, 6), (2, 5), (3, 4)]
assert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
assert zip3 == @[(a: 6, b: 'a'), (a: 5, b: 'b'), (a: 4, b: 'c'),
(a: 3, b: 'd')]
assert zip1[2].b == 4
assert zip2[2].b == "three"
assert zip3 == @[(6, 'a'), (5, 'b'), (4, 'c'), (3, 'd')]
assert zip1[2][1] == 4
assert zip2[2][1] == "three"

var m = min(s1.len, s2.len)
newSeq(result, m)
Expand Down Expand Up @@ -1077,11 +1073,11 @@ when isMainModule:
assert zip3 == @[(1, 6), (2, 5), (3, 4)]
assert zip4 == @[(1, "one"), (2, "two"), (3, "three")]
assert zip5 == @[(1, "one"), (2, "two"), (3, "three")]
assert zip1[2].b == 4
assert zip2[2].b == "three"
assert zip3[2].b == 4
assert zip4[2].b == "three"
assert zip5[2].b == "three"
assert zip1[2][1] == 4
assert zip2[2][1] == "three"
assert zip3[2][1] == 4
assert zip4[2][1] == "three"
assert zip5[2][1] == "three"

block: # distribute tests
let numbers = @[1, 2, 3, 4, 5, 6, 7]
Expand Down
2 changes: 1 addition & 1 deletion tests/manyloc/argument_parser/argument_parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ proc build_help*(expected: seq[Tparameter_specification] = @[],
let width = prefixes.map(proc (x: string): int = 3 + len(x)).max

for line in zip(prefixes, helps):
result.add(line.a & spaces(width - line.a.len) & line.b)
result.add(line[0] & spaces(width - line[0].len) & line[1])


proc echo_help*(expected: seq[Tparameter_specification] = @[],
Expand Down

0 comments on commit 4cda53a

Please sign in to comment.