Comprehensions for FixedSizeArrays
Opened this issue · 6 comments
Vec(i for i=1:3)
generates a Vec(Base.Generator{UnitRange{Int64},##7#8}(#7,1:3))
I can of course do Vec([i for i=1:3])
, but then I first allocate a Vector
and then convert it to a Vec
. If I have to do many of these, then this will be inefficient. (I am assuming?)
Can the functionality I am asked for be implemented?
We could add a constructor like:
(::Type{T}){T<:FixedArray}(x::Base.Generator) = map(i->next(x, i)[2], T)
Note, that we need to infer the length T
in a type stable way...
(That's a cool idea by the way!)
sounds great if it can be done. map(i->next(x, i)[2], T)
generates an array again, though.
Does the fact that tuple
doesn't take a generator as an argument indicate this may not be possible?
If T
is some kind of (fully typed) fixed size vector, this should generate an instance of T
!
In StaticArrays I played with the idea of indexing the iterator in the generator directly. The user had to specify at least the size as a type parameter, so something like Vec{3}(sin(i) for i = 1:3)
. Restricting the iterator to be indexable (such as ranges) let me unroll the expression for speed.
In mythical future versions of Julia, constant propagation may let you obtain the size of 1:3
during inference, but I think the type parameter is a necessary evil for now...
that would actually be fine for me