Array or set of conditional evaluation with julia

Viewed 1852

I am new with Julia. I was wandering if it is possible to write in a single line an if statement in an array or a set in Julia.

For example in python I can write a list such as

s = [1 if np.random.uniform() < 0.5 else 0 for i in range(10)]

which in Julia, if I'm right, should be an array like this

s = []
for i in 0:10
  if rand()<0.5
    push!(s, 1)
  else
    push!(s, 0)
  end
end 

I know I can write the for cycle in a single line i.e.

s =[1 for i in 1:10]

But what about the if..else statement?

4 Answers
Related