FixedSizeArrays.jl icon indicating copy to clipboard operation
FixedSizeArrays.jl copied to clipboard

Comprehensions for FixedSizeArrays

Open cortner opened this issue 9 years ago • 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?

cortner avatar Aug 23 '16 16:08 cortner

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...

SimonDanisch avatar Aug 23 '16 17:08 SimonDanisch

(That's a cool idea by the way!)

SimonDanisch avatar Aug 23 '16 17:08 SimonDanisch

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?

cortner avatar Aug 23 '16 17:08 cortner

If T is some kind of (fully typed) fixed size vector, this should generate an instance of T!

SimonDanisch avatar Aug 23 '16 17:08 SimonDanisch

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...

andyferris avatar Aug 24 '16 00:08 andyferris

that would actually be fine for me

cortner avatar Aug 24 '16 05:08 cortner