jules
October 19, 2021, 5:34pm
62
Not sure if this was answered already, but I think it would still short-circuit in broadcasting. Just not the way that people wrote above.
In this example, I don’t think you’d see anything printed, although I can’t test as I’m still on an older Julia version.
function f(x)
println("f was called")
isodd(x)
end
x = [false, false, false]
y = [1, 2, 3]
z = x .&& f.(y)
I think this should be the same as this, so it shouldn’t execute f
:
broadcast(x, y) do _x, _y
_x && f_(y)
end
The examples above tried to short-circuit before the array arguments were even created, which doesn’t work.
lmiq
October 19, 2021, 5:44pm
63
In 1.7:
julia> f(x) = x
f (generic function with 2 methods)
julia> x = [true,false,true];
julia> x .&& f.([1,2,3])
3-element Vector{Integer}:
1
false
3
or
julia> f(x) = println("I'm here $x")
f (generic function with 2 methods)
julia> x .&& f.([1,2,3])
I'm here 1
I'm here 3
3-element Vector{Union{Nothing, Bool}}:
nothing
false
nothing
2 Likes
jules
October 19, 2021, 6:48pm
64
Yup, there’s the short circuiting
Mason
October 19, 2021, 6:55pm
65
MarcMush:
other types are allowed on right side, but they are still evaluated, when they wouldn’t be with &&
, that’s what I mean with short-circuiting
julia> false && error("not allowed")
false
julia> [false, false] .&& error("not allowed")
ERROR: not allowed
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:33
[2] top-level scope
@ REPL[24]:1
You’re missing a dot.
julia> [false, false] .&& error.("not allowed")
2-element BitVector:
0
0
When you write [false, false] .&& error("not allowed")
that’s a little like writing
A = [false, false]
b = error("not allowed")
C = BitVector(undef, 2)
for i in eachindex(A)
C[i] = A[i] && b
end
whereas when you write [false, false] .&& error("not allowed")
that instead becomes something like
A = [false, false]
C = BitVector(undef, 2)
for i in eachindex(A)
C[i] = A[i] && error("not allowed")
end
4 Likes
.&&
just became much more intersting to me!
I will update my previous answers
3 Likes