Just to clarify a silly doubt. What is the expected behavior of
where x is an Array? I was expecting that println would be executed to each position of x, but
julia> x = rand(5)
I would like to understand the meaning of those Nothings.
Thank you very much.
Well, it is simple:
When you are using a REPL the returned value is shown (except when it is nothing). println return none, and because you have running println for each value (with println.), you are obtaining an array of nothing. At different of nothing, that it is not shown in the REPL, an array of any type is always shown. This is the reason.
if you do not want the nothing, you can run
Anyway, it is in the REPL, running the code directly would not show anything different than the values of x.
I hope it is more clear now.
@CodeLenz One last comment:
the dot operator (as println.) is usually used when you want the return obtained.
If you only want to run the function for each value, you can use:
foreach, at different of the vectorized, does not return anything.
f.(x) returns an array (or other container) holding the value of
f(x_i) for each
julia> f(x) = 2 * x
f (generic function with 1 method)
julia> f.([1, 2, 3])
nothing, then you will get a collection of
julia> g.([1, 2, 3])
julia> result = println(1.0)
julia> result === nothing
println.(x) returns a vector of
nothings, which then get automatically printed because the REPL always prints out the return value of the last expression.
julia> println.([1, 2, 3])
Edit: @dmolina beat me to it!
I was missing the fact that println returns something (in this case, Nothing ), since I was just looking at the output in the REPL.
Silly me, silly me.
Thanks a lot.