Skip to content

Simpler first example #310

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

Open
femtotrader opened this issue Apr 25, 2024 · 1 comment
Open

Simpler first example #310

femtotrader opened this issue Apr 25, 2024 · 1 comment

Comments

@femtotrader
Copy link

Hello,

I think it would help to have a simpler first example with a Vector and NamedTuple

Here is a proposal.

julia> using StructArrays

julia> adata = [1, 2]
2-element Vector{Int64}:
 1
 2

julia> bdata = [10.1, 20.1]
2-element Vector{Float64}:
 10.1
 20.1

julia> x = StructArray{NamedTuple{(:a, :b), Tuple{Int, Float64}}}((adata, bdata))
2-element StructArray(::Vector{Int64}, ::Vector{Float64}) with eltype @NamedTuple{a::Int64, b::Float64}:
 (a = 1, b = 10.1)
 (a = 2, b = 20.1)

julia> x.a
2-element Vector{Int64}:
 1
 2

julia> x.b
2-element Vector{Float64}:
 10.1
 20.1

julia> x[1]
(a = 1, b = 10.1)

Any opinion?

@aplavin
Copy link
Member

aplavin commented Apr 22, 2025

Makes sense! Maybe even shorter,

x = StructArray(a=adata, b=bdata)

?
Or

julia> a = [1, 2]
2-element Vector{Int64}:
 1
 2

julia> b = [10.1, 20.1]
2-element Vector{Float64}:
 10.1
 20.1

julia> x = StructArray(; a, b)

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

2 participants