# arrays – Julia中随机输出的Array Comprehension

This question is an extension if one I asked earlier today.基本上,我试图在Julia中编写一个数组解析,它调用一个函数f(x),其输出是一个随机数.当达到小于0.5的随机数时,我希望它杀死该函数.我能够编写以下代码：

X=[f(i) for i in 1:1:100 if (j=f(i) ;j < 0.5 ? false: j>0.5)]

X=[J=f(i) for i in 1:1:100 if (J < 0.5 ? false: J>0.5)]

```X = ( r=Vector{Float64}() ;
any(i->(v=f(i) ; v>0.5 ? ( push!(r,v) ; false) : true), 1:100)
; r )```

[单行被分成三行,因为它有点长;)]

`(r=ones(0); any(i->(v=rand(); v>0.5 ? (push!(r,v); false) : true), 1:10); r)`

```collectwhilecond(f,cond,itr) = begin
r=Vector{typeof(f(first(itr)))}()
all(x->(y=f(x); cond(y) ? (push!(r,y);true):false),itr)
return r
end```

```julia> joker(i) = 1.0 + 4*rand() - log(i)

julia> collectwhilecond(joker, x->x>=0.5, 1:100)
3-element Array{Float64,1}:
4.14222
3.42955
2.76387```

```collectwhilecond(f,cond,itr) = begin
t = Base.promote_op(f,eltype(itr))  # unofficial and subject to change
r = Vector{t}()
all( x -> ( y=f(x) ; cond(y) ? (push!(r,y) ; true) : false), itr )
return r
end```