Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comprehensions for FixedSizeArrays #155

Open
cortner opened this issue Aug 23, 2016 · 6 comments
Open

Comprehensions for FixedSizeArrays #155

cortner opened this issue Aug 23, 2016 · 6 comments

Comments

@cortner
Copy link

cortner commented Aug 23, 2016

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?

@SimonDanisch
Copy link
Owner

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
Copy link
Owner

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

@cortner
Copy link
Author

cortner commented Aug 23, 2016

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?

@SimonDanisch
Copy link
Owner

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

@andyferris
Copy link
Contributor

andyferris commented Aug 24, 2016

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

@cortner
Copy link
Author

cortner commented Aug 24, 2016

that would actually be fine for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants