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

constructing a FixedSizeArray from a generator #20

Closed
nsajko opened this issue Apr 22, 2024 · 2 comments · Fixed by #48
Closed

constructing a FixedSizeArray from a generator #20

nsajko opened this issue Apr 22, 2024 · 2 comments · Fixed by #48

Comments

@nsajko
Copy link
Collaborator

nsajko commented Apr 22, 2024

Regular Arrays can be conveniently constructed using comprehensions:

julia> [(i, j) for i=1:3, j=1:2]
3×2 Matrix{Tuple{Int64, Int64}}:
 (1, 1)  (1, 2)
 (2, 1)  (2, 2)
 (3, 1)  (3, 2)

The above is basically equivalent to constructing a generator and collecting it:

julia> collect((i, j) for i=1:3, j=1:2)
3×2 Matrix{Tuple{Int64, Int64}}:
 (1, 1)  (1, 2)
 (2, 1)  (2, 2)
 (3, 1)  (3, 2)

A proposal exists to have Array constructors behave equivalently to collect here: JuliaLang/julia#16029. We do need some nice way of constructing FixedSizeArrays, so maybe we could have a constructor method that takes a Base.Generator? Or at least a convert method.

@nsajko
Copy link
Collaborator Author

nsajko commented Apr 22, 2024

A related issue is that constructing FixedSizeArrays from iterators could be more convenient if we could use Julia's type inference, and some packages do use Core.Compiler.return_type, but this is currently not permitted. Perhaps an issue should be created on JuliaLang/julia to try and get an interface for type inference.

@nsajko
Copy link
Collaborator Author

nsajko commented Apr 23, 2024

xref JuliaLang/julia#54207

@nsajko nsajko closed this as completed in #48 May 1, 2024
nsajko added a commit that referenced this issue May 1, 2024
* introduce function `collect_as` for construction from an iterator

Makes constructing `FixedSizeArray`s more convenient!

Inspired by
JuliaLang/julia#36288

This currently ignores `Base.IteratorElType`, xref
https://discourse.julialang.org/t/i-dont-get-base-iteratoreltype/113604

The allocations in some code paths are probably excessive/could be
optimized. But I guess this is good for a start.

Fixes #20

* also test an iterator with `BigInt`-valued `size` and `length`

* remove the premature optimization for `AbstractArray`

* improve tests

* add to the Readme

* whitespace/formatting fix

* delete two useless `nothing` lines

One of these wasn't being recorded by code coverage (another Julia
coverage bug, I guess).

* simplify a bit
This issue was closed.
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

Successfully merging a pull request may close this issue.

1 participant