I wanted to compare execution time for two list comprehensions one which uses if-else keywords and another that uses ternary operator.
arr = [5, 8, 12, 17, 24, 42];
function squares_and_cubes(array)
return [ x%2 ==0 ? x^2 : x^3 for x in array]
end
function squares_and_cubes_if_else(array)
[ x^2 if (x%2==0) else x^3 end for x in array]
end
@time squares_and_cubes(arr)
@time squares_and_cubes_if_else(arr)
However, I get the following error message ERROR: syntax: invalid comprehension syntax
when trying to execute [ x^2 if (x%2==0) else x^3 end for x in arr]. As a Julia newbie, I can't figure out what is the issue with this syntax - I am using v 1.7 .