# Behavior of map function

**URL:** <https://discourse.julialang.org/t/behavior-of-map-function/51205>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 3, 2020, 4:02pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205 "2020-12-03T16:02:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 3, 2020, 4:02pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/1 "2020-12-03T16:02:13Z")

</div>

I’ve been messing around with `map` and `count` for golfing purposes, and recently noticed this.

```julia
julia> map(i->print(i),[1,2,3,4])
12344-element Array{Nothing,1}:
nothing
nothing
nothing
nothing

julia> print(map(i->print(i),[1,2,3,4]))
1234[nothing, nothing, nothing, nothing]

```

How to explain that array of `nothing`s there ?  
Edit: Thanks to @jling and @bkamins, I understand both why the above is happening and how to (somewhat) avoid it

Originally, I was trying to evaluate the following function

```julia
a(k)=map((i,j)->count(l->l[mod(i+=j,31)+1]=='#',readlines(file)),-k,k)

```

at `k=1,3,5,7`, by doing `map(a,[1,3,5,7])` but only the first value of k that I pass in the array prints the correct value, rest all produce 0.  
Even if I try to evaluate `a` ‘normally’ as:

```julia
print("$(a(7)) and $(a(3))")

```

my second call always returns 0. Why is this happening ? (I’m on Julia 1.5.2 btw)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 3, 2020, 4:02pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/2 "2020-12-03T16:02:44Z")

</div>

`map` allocates, `print` returns `nothing`, thus the result array.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 3, 2020, 4:06pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/3 "2020-12-03T16:06:43Z")

</div>

use `foreach` if you do not want to collect return values.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 3, 2020, 4:10pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/4 "2020-12-03T16:10:21Z")

</div>

Thank you, that explains the test case’s behaviour.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 3, 2020, 4:11pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/5 "2020-12-03T16:11:29Z")

</div>

But why is the second invocation of `a` always giving zero while the first invocation always gives the correct answer?

Edit: I get it, it’s because `readlines` exhausts the file after first invocation -\_-

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 3, 2020, 9:50pm UTC](https://discourse.julialang.org/t/behavior-of-map-function/51205/6 "2020-12-03T21:50:13Z")

</div>

> [@Ayush\_Rawat](#):
>
> `map(i->print(i),[1,2,3,4])`

For golfing purposes, you can save a few strokes:

```julia
map(print,[1,2,3,4])

```

The first argument to map is a function. Your anonymous function` i->print(i)` is one function simply calling another.
