A.Yazdiha
A.Yazdiha

Reputation: 1378

How to know the index of the iterator when using map in Julia

I have an Array of arrays, called y:

y=Array(Vector{Int64}, 10)

which is basically a list of 1-dimensional arrays(10 of them), and each 1-dimensional array has length 5. Below is an example of how they are initialized:

for i in 1:10
    y[i]=sample(1:20, 5)
end

Each 1-dimensional array includes 5 randomly sampled integers between 1 to 20.

Right now I am applying a map function where for each of those 1-dimensional arrays in y , excludes which numbers from 1 to 20:

map(x->setdiff(1:20, x), y)

However, I want to make sure when the function applied to y[i], if the output of setdiff(1:20, y[i]) includes i, i is excluded from the results. in other words I want a function that works like

setdiff(deleteat!(Vector(1:20),i) ,y[i])

but with map.

Mainly my question is that whether you can access the index in the map function.

P.S, I know how to do it with comprehensions, I wanted to know if it is possible to do it with map.

comprehension way:

[setdiff(deleteat!(Vector(1:20), index), value) for (index,value) in enumerate(y)]

Upvotes: 3

Views: 2504

Answers (1)

Alexander Morley
Alexander Morley

Reputation: 4181

Like this?

map(x -> setdiff(deleteat!(Vector(1:20), x[1]),x[2]), enumerate(y))

For your example gives this:

[2,3,4,5,7,8,9,10,11,12,13,15,17,19,20]
[1,3,5,6,7,8,9,10,11,13,16,17,18,20]
....
[1,2,4,7,8,10,11,12,13,14,15,16,17,18]
[1,2,3,5,6,8,11,12,13,14,15,16,17,19,20]

Upvotes: 6

Related Questions