summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorKaushal Modi <kaushal.modi@gmail.com>2019-11-04 15:11:43 -0500
committerAndreas Rumpf <rumpf_a@web.de>2019-11-04 21:11:43 +0100
commitb24560a140f32c6289786a213250a82df2979aa4 (patch)
treea9bfb37927093b7821ad054a9cc1408572d5efed /lib/pure
parentcf5c3f2400a7cfdee139bcd7283042500caf0a3b (diff)
downloadNim-b24560a140f32c6289786a213250a82df2979aa4.tar.gz
Make sequtils.zip return seq of anonymous tuples (#12575)
* Make sequtils.zip return seq of anonymous tuples

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".

* Make sequtils.zip backwards compatible with Nim 1.0.x
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/collections/sequtils.nim104
1 files changed, 64 insertions, 40 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim
index 9573753ff..f71f13358 100644
--- a/lib/pure/collections/sequtils.nim
+++ b/lib/pure/collections/sequtils.nim
@@ -202,36 +202,49 @@ 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]] =
-  ## 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]
-      long = @[6, 5, 4, 3, 2, 1]
-      words = @["one", "two", "three"]
-      letters = "abcd"
-      zip1 = zip(short, long)
-      zip2 = zip(short, words)
-      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"
+template zipImpl(s1, s2, retType: untyped): untyped =
+  proc zip*[S, T](s1: openArray[S], s2: openArray[T]): retType =
+    ## 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.
+    ##
+    ## **Note**: For Nim 1.0.x and older version, ``zip`` returned a seq of
+    ## named tuple with fields ``a`` and ``b``. For Nim versions 1.1.x and newer,
+    ## ``zip`` returns a seq of unnamed tuples.
+    runnableExamples:
+      let
+        short = @[1, 2, 3]
+        long = @[6, 5, 4, 3, 2, 1]
+        words = @["one", "two", "three"]
+        letters = "abcd"
+        zip1 = zip(short, long)
+        zip2 = zip(short, words)
+      assert zip1 == @[(1, 6), (2, 5), (3, 4)]
+      assert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
+      assert zip1[2][0] == 3
+      assert zip2[1][1] == "two"
+      when (NimMajor, NimMinor) <= (1, 0):
+        let
+          zip3 = zip(long, letters)
+        assert zip3 == @[(a: 6, b: 'a'), (5, 'b'), (4, 'c'), (3, 'd')]
+        assert zip3[0].b == 'a'
+      else:
+        let
+          zip3: seq[tuple[num: int, letter: char]] = zip(long, letters)
+        assert zip3 == @[(6, 'a'), (5, 'b'), (4, 'c'), (3, 'd')]
+        assert zip3[0].letter == 'a'
 
-  var m = min(s1.len, s2.len)
-  newSeq(result, m)
-  for i in 0 ..< m:
-    result[i] = (s1[i], s2[i])
+    var m = min(s1.len, s2.len)
+    newSeq(result, m)
+    for i in 0 ..< m:
+      result[i] = (s1[i], s2[i])
+
+when (NimMajor, NimMinor) <= (1, 0):
+  zipImpl(s1, s2, seq[tuple[a: S, b: T]])
+else:
+  zipImpl(s1, s2, seq[(S, T)])
 
 proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] =
   ## Splits and distributes a sequence `s` into `num` sub-sequences.
@@ -1070,18 +1083,29 @@ when isMainModule:
       zip1 = zip(short, long)
       zip2 = zip(short, words)
       zip3 = zip(ashort, along)
-      zip4 = zip(ashort, awords)
-      zip5 = zip(ashort, words)
     assert zip1 == @[(1, 6), (2, 5), (3, 4)]
     assert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
     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
+    when (NimMajor, NimMinor) <= (1, 0):
+      let
+        # In Nim 1.0.x and older, zip returned a seq of tuple strictly
+        # with fields named "a" and "b".
+        zipAb = zip(ashort, awords)
+      assert zipAb == @[(a: 1, b: "one"), (2, "two"), (3, "three")]
+      assert zipAb[2].b == "three"
+    else:
+      let
+        # As zip returns seq of anonymous tuples, they can be assigned
+        # to any variable that's a sequence of named tuples too.
+        zipXy: seq[tuple[x: int, y: string]] = zip(ashort, awords)
+        zipMn: seq[tuple[m: int, n: string]] = zip(ashort, words)
+      assert zipXy == @[(x: 1, y: "one"), (2, "two"), (3, "three")]
+      assert zipMn == @[(m: 1, n: "one"), (2, "two"), (3, "three")]
+      assert zipXy[2].y == "three"
+      assert zipMn[2].n == "three"
 
   block: # distribute tests
     let numbers = @[1, 2, 3, 4, 5, 6, 7]
@@ -1228,10 +1252,10 @@ when isMainModule:
     block:
       let
         numeric = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
-        odd_numbers = toSeq(filter(numeric) do (x: int) -> bool:
+        oddNumbers = toSeq(filter(numeric) do (x: int) -> bool:
           if x mod 2 == 1:
             result = true)
-      assert odd_numbers == @[1, 3, 5, 7, 9]
+      assert oddNumbers == @[1, 3, 5, 7, 9]
 
     block:
       doAssert [1, 2].toSeq == @[1, 2]
blame/changelog.md?h=devel&id=517dd800f8e621aae0221f15c4ad7ad011c910b5'>^
cd9af6b80 ^


03c146cd9 ^
cd9af6b80 ^
caf30e7cb ^


7ce035835 ^
da29222f8 ^
d23446c6b ^
a3a317b76 ^

b8e6ea754 ^



3e060cfb0 ^


03c146cd9 ^
3e060cfb0 ^










48e7775ad ^




3e060cfb0 ^
d11cb9d49 ^


06dfd3161 ^
7f377da1d ^
06dfd3161 ^
041ee92bb ^


1fb2a5867 ^


256841cf2 ^
1fb2a5867 ^




c7a1a7b8b ^

6aa971d39 ^
82e424189 ^
bf604c682 ^
9c42ae91b ^
59ba4d8c0 ^
3e5743d6f ^
1b41c3122 ^
6aa971d39 ^
b70fd0400 ^
da29222f8 ^




03c146cd9 ^
9295251e6 ^


























037990bc7 ^
dd24004fa ^

9acbf99ef ^

de27910ab ^
03c146cd9 ^























c285739d5 ^
99c198625 ^
04deb6c94 ^
00fa7a574 ^
7d17cd34b ^
f7d2f9c5f ^
9c46927fa ^
a890aa75a ^
c777f2fb6 ^

5c534b294 ^








411be506a ^


dda6f3f6d ^
3573a4f9c ^


c777f2fb6 ^





3d20f1419 ^

58282547f ^


685d03e2e ^

74d1f2501 ^
0c56eeda0 ^
28510a9da ^

7d17cd34b ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227