Last active
September 25, 2015 16:59
-
-
Save simonbyrne/110718f98f13dfd5d4f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DataFrame{N,D} | |
data::D | |
end | |
immutable Field{s} | |
end | |
Field(s::Symbol) = Field{s}() | |
function DataFrame(;kwds...) | |
names = Any[] | |
data = Any[] | |
types = Any[] | |
for (n, d) in kwds | |
push!(names,n) | |
push!(data,d) | |
push!(types,typeof(d)) | |
end | |
N = tuple(names...) | |
T = tuple(types...) | |
DataFrame{N,T}(tuple(data...)) | |
end | |
stagedfunction getindex{N,D,s}(d::DataFrame{N,D},f::Field{s}) | |
m = Dict(zip(N,1:length(N))) | |
j = m[s] | |
:(d.data[$j]) | |
end | |
stagedfunction getindex{N,D,s}(d::DataFrame{N,D},i::Integer,f::Field{s}) | |
m = Dict(zip(N,1:length(N))) | |
j = m[s] | |
:(d.data[$j][i]) | |
end | |
stagedfunction getindex{N,D}(d::DataFrame{N,D},i::Integer) | |
Expr(:tuple,[:(d.data[$j][i]) for j in 1:length(D)]...) | |
end | |
d = DataFrame(aa=[1,2,3],bb=[4.0,5.0,6.0]) | |
@code_typed d[Field(:aa)] | |
@code_typed d[Field(:bb)] | |
@code_typed d[1,Field(:aa)] | |
@code_typed d[1,Field(:bb)] | |
@code_typed d[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copying this into REPL results in an error: