@@ -1087,34 +1087,6 @@ foreach(f) = (f(); nothing)
1087
1087
foreach (f, itr) = (for x in itr; f (x); end ; nothing )
1088
1088
foreach (f, itrs... ) = (for z in zip (itrs... ); f (z... ); end ; nothing )
1089
1089
1090
- # generic map on any iterator
1091
- function map (f, iters... )
1092
- result = []
1093
- len = length (iters)
1094
- states = [start (iters[idx]) for idx in 1 : len]
1095
- nxtvals = cell (len)
1096
- cont = true
1097
- for idx in 1 : len
1098
- if done (iters[idx], states[idx])
1099
- cont = false
1100
- break
1101
- end
1102
- end
1103
- while cont
1104
- for idx in 1 : len
1105
- nxtvals[idx],states[idx] = next (iters[idx], states[idx])
1106
- end
1107
- push! (result, f (nxtvals... ))
1108
- for idx in 1 : len
1109
- if done (iters[idx], states[idx])
1110
- cont = false
1111
- break
1112
- end
1113
- end
1114
- end
1115
- result
1116
- end
1117
-
1118
1090
# # map over arrays ##
1119
1091
1120
1092
# # transform any set of dimensions
@@ -1175,39 +1147,6 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
1175
1147
return R
1176
1148
end
1177
1149
1178
-
1179
- # using promote_type
1180
- function promote_to! {T,F} (f:: F , offs, dest:: AbstractArray{T} , A:: AbstractArray )
1181
- # map to dest array, checking the type of each result. if a result does not
1182
- # match, do a type promotion and re-dispatch.
1183
- for i = offs: length (A)
1184
- @inbounds Ai = A[i]
1185
- el = f (Ai)
1186
- S = typeof (el)
1187
- if S === T || S <: T
1188
- @inbounds dest[i] = el:: T
1189
- else
1190
- R = promote_type (T, S)
1191
- if R != = T
1192
- new = similar (dest, R)
1193
- copy! (new,1 , dest,1 , i- 1 )
1194
- new[i] = el
1195
- return promote_to! (f, i+ 1 , new, A)
1196
- end
1197
- @inbounds dest[i] = el
1198
- end
1199
- end
1200
- return dest
1201
- end
1202
-
1203
- function map_promote (f, A:: AbstractArray )
1204
- if isempty (A); return similar (A, Bottom); end
1205
- first = f (A[1 ])
1206
- dest = similar (A, typeof (first))
1207
- dest[1 ] = first
1208
- return promote_to! (f, 2 , dest, A)
1209
- end
1210
-
1211
1150
# These are needed because map(eltype, As) is not inferrable
1212
1151
promote_eltype_op (:: Any ) = (@_pure_meta ; Bottom)
1213
1152
promote_eltype_op {T} (op, :: AbstractArray{T} ) = (@_pure_meta ; promote_op (op, T))
@@ -1226,39 +1165,7 @@ function map!{F}(f::F, dest::AbstractArray, A::AbstractArray)
1226
1165
return dest
1227
1166
end
1228
1167
1229
- function map_to! {T,F} (f:: F , offs, st, dest:: AbstractArray{T} , A)
1230
- # map to dest array, checking the type of each result. if a result does not
1231
- # match, widen the result type and re-dispatch.
1232
- i = offs
1233
- while ! done (A, st)
1234
- @inbounds Ai, st = next (A, st)
1235
- el = f (Ai)
1236
- S = typeof (el)
1237
- if S === T || S <: T
1238
- @inbounds dest[i] = el:: T
1239
- i += 1
1240
- else
1241
- R = typejoin (T, S)
1242
- new = similar (dest, R)
1243
- copy! (new,1 , dest,1 , i- 1 )
1244
- @inbounds new[i] = el
1245
- return map_to! (f, i+ 1 , st, new, A)
1246
- end
1247
- end
1248
- return dest
1249
- end
1250
-
1251
- function map (f, A:: AbstractArray )
1252
- if isempty (A)
1253
- return isa (f,Type) ? similar (A,f) : similar (A)
1254
- end
1255
- st = start (A)
1256
- A1, st = next (A, st)
1257
- first = f (A1)
1258
- dest = similar (A, typeof (first))
1259
- dest[1 ] = first
1260
- return map_to! (f, 2 , st, dest, A)
1261
- end
1168
+ map {F} (f:: F , A:: AbstractArray ) = collect (Generator (f,A))
1262
1169
1263
1170
# # 2 argument
1264
1171
function map! {F} (f:: F , dest:: AbstractArray , A:: AbstractArray , B:: AbstractArray )
@@ -1268,34 +1175,6 @@ function map!{F}(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray)
1268
1175
return dest
1269
1176
end
1270
1177
1271
- function map_to! {T,F} (f:: F , offs, dest:: AbstractArray{T} , A:: AbstractArray , B:: AbstractArray )
1272
- for i = offs: length (A)
1273
- @inbounds Ai, Bi = A[i], B[i]
1274
- el = f (Ai, Bi)
1275
- S = typeof (el)
1276
- if (S != = T) && ! (S <: T )
1277
- R = typejoin (T, S)
1278
- new = similar (dest, R)
1279
- copy! (new,1 , dest,1 , i- 1 )
1280
- @inbounds new[i] = el
1281
- return map_to! (f, i+ 1 , new, A, B)
1282
- end
1283
- @inbounds dest[i] = el:: T
1284
- end
1285
- return dest
1286
- end
1287
-
1288
- function map (f, A:: AbstractArray , B:: AbstractArray )
1289
- shp = promote_shape (size (A),size (B))
1290
- if prod (shp) == 0
1291
- return similar (A, promote_type (eltype (A),eltype (B)), shp)
1292
- end
1293
- first = f (A[1 ], B[1 ])
1294
- dest = similar (A, typeof (first), shp)
1295
- dest[1 ] = first
1296
- return map_to! (f, 2 , dest, A, B)
1297
- end
1298
-
1299
1178
# # N argument
1300
1179
1301
1180
ith_all (i, :: Tuple{} ) = ()
@@ -1311,32 +1190,9 @@ end
1311
1190
1312
1191
map! {F} (f:: F , dest:: AbstractArray , As:: AbstractArray... ) = map_n! (f, dest, As)
1313
1192
1314
- function map_to_n! {T,F} (f:: F , offs, dest:: AbstractArray{T} , As)
1315
- for i = offs: length (As[1 ])
1316
- el = f (ith_all (i, As)... )
1317
- S = typeof (el)
1318
- if (S != = T) && ! (S <: T )
1319
- R = typejoin (T, S)
1320
- new = similar (dest, R)
1321
- copy! (new,1 , dest,1 , i- 1 )
1322
- @inbounds new[i] = el
1323
- return map_to_n! (f, i+ 1 , new, As)
1324
- end
1325
- @inbounds dest[i] = el:: T
1326
- end
1327
- return dest
1328
- end
1193
+ spread (f) = (args)-> f (args... )
1329
1194
1330
- function map (f, As:: AbstractArray... )
1331
- shape = mapreduce (size, promote_shape, As)
1332
- if prod (shape) == 0
1333
- return similar (As[1 ], promote_eltype (As... ), shape)
1334
- end
1335
- first = f (map (a-> a[1 ], As)... )
1336
- dest = similar (As[1 ], typeof (first), shape)
1337
- dest[1 ] = first
1338
- return map_to_n! (f, 2 , dest, As)
1339
- end
1195
+ map (f, iters... ) = collect (Generator (spread (f),zip (iters... )))
1340
1196
1341
1197
# multi-item push!, unshift! (built on top of type-specific 1-item version)
1342
1198
# (note: must not cause a dispatch loop when 1-item case is not defined)
0 commit comments