Hi,
I have a fairly simple question that would like to sort out with your kind help The fact is that I like quite a lot the conditional syntax
{something} ? true : false
which works great for short decissions like
x>0.5 ? 1 : 0
Is it possible to do something similar not for scalar valued x but for x a vector or array? How would that be done? I understand that
x.>0
works, but what then? Of course I can use the map functionto do what I want, but was wondering if it is possible to do something simpler or performance wise (?)
One thing to keep in mind is that ifelse() and the ? operator are actually a bit different because ifelse evaluates both of its arguments, while ? only evaluates the argument chosen by the test. For example:
julia> function f(x)
println("calling f")
1
end
f (generic function with 1 method)
julia> function g(x)
println("calling g")
2
end
g (generic function with 1 method)
julia> y = true ? f(1) : g(1)
calling f
1
julia> y = ifelse(true, f(1), g(1))
calling f
calling g
1
It seems like this would make ifelse() always worse, but in fact the predictability of its behavior (always evaluating both arguments) can sometimes allow for more efficient pipelined code.