# Issue while using basic functions in julia

**URL:** <https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607>\
**Category:** General Usage\
**Created:** [January 27, 2023, 6:02am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607 "2023-01-27T06:02:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [January 27, 2023, 6:02am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607/1 "2023-01-27T06:02:27Z")

</div>

Dear julia community, I am a beginner in julia can you please explain to me why I am facing the following problems…?  
1.

```julia
#code
a = [1 2 3; 4 5 6]

map(x -> println(a), [1,2])

```

```julia
#output in notebook
[1 2 3; 4 5 6]
[1 2 3; 4 5 6]
2-element Vector{Nothing}:
 nothing
 nothing

```

```julia
#output in REPL
[1 2 3; 4 5 6]
[1 2 3; 4 5 6]

```

Why are my outputs different…?

1. I want shuffle matrix “a” along the rows

```julia
#code
map!(x -> shuffle!(a[x,:]), [1,2]) |> println
println(a)

```

```julia
# output
[[3, 2, 1], [5, 4, 6]]
[1 2 3; 4 5 6]

```

can someone please explain my mistake…?

1. 

```julia
#Shuffling only one row
shuffle!(a[1,:])
println(a)
shuffle!(view(a[1,:]))
println(a)

```

```julia
#output
[1 2 3; 4 5 6]
BoundsError: attempt to access 3-element Vector{Int64} at index []

Stacktrace:
 [1] throw_boundserror(A::Vector{Int64}, I::Tuple{})
   @ Base .\abstractarray.jl:703
 [2] checkbounds
   @ .\abstractarray.jl:668 [inlined]
 [3] view(::Vector{Int64})
   @ Base .\subarray.jl:177
 [4] top-level scope
   @ c:\VSP.ipynb:3

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [January 27, 2023, 7:13am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607/2 "2023-01-27T07:13:02Z")

</div>

1. The output in the REPL is the same:

```julia-repl
julia> map(x -> println(a), [1,2])
[1 2 3; 4 5 6]
[1 2 3; 4 5 6]
2-element Vector{Nothing}:
 nothing
 nothing

```

`map(f, x)` means: for each item in `x`, do `f(x)`. Since in your example `x` is `[1, 2]` you’re doing:  
for the number 1, `println(a)`, then for the number 2 `println(a)`. Since `println` returns `nothing`, you get `[nothing, nothing]` at the end.

2./3. The main thing that’s happening here is that `a[x, :]` creates a new Vector. So if you shuffle that vector, that won’t shuffle `a`, because they’re no longer related to each other. It also means you can’t do `view(a[1, :])`. You can do `view(a, 1, :)` or `@view a[1, :]`

Using a proper view, I can shuffle the first row:

```julia-repl
julia> shuffle!(@view a[1,:])
3-element view(::Matrix{Int64}, 1, :) with eltype Int64:
 3
 2
 1

julia> a
2×3 Matrix{Int64}:
 3 2 1
 4 5 6

```

One way to shuffle all the rows is to use `eachrow`, which creates an iterators of `view`s

```julia-repl
julia> a = [1 2 3; 4 5 6; 7 8 9];

julia> shuffle!.(eachrow(a));

julia> a
3×3 Matrix{Int64}:
 1 3 2
 5 4 6
 7 9 8

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 27, 2023, 7:28am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607/3 "2023-01-27T07:28:54Z")

</div>

If you dont want the outputs in an array, replace `map` with `foreach`, or use a `for` loop.

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [January 27, 2023, 8:16am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607/4 "2023-01-27T08:16:45Z")

</div>

Thank you @tomerarnon for your response

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [January 27, 2023, 8:17am UTC](https://discourse.julialang.org/t/issue-while-using-basic-functions-in-julia/93607/5 "2023-01-27T08:17:29Z")

</div>

Thank you @gustaphe for your response
