There’s a language feature that I have always wanted and am wondering if julia has anything close. Just wanted to check before attempting to implement anything…
My idea is based on the fact that one way of accessing and viewing data is often times not enough. It would be great to be able to have multiple conceptual schematics of the same data and be able to access that data in the most convenient way. As an example, I found this code in the wild:
type Vector
x::Real
y::Real
z::Real
end
Vector() = Vector(0, 0, 0)
function toList(a::Vector)
return [a.x, a.y, a.z]
end
function toVec(a::Array)
return Vector(a[1], a[2], a[3])
end
...
As you can see the author of this code really wants to be able to simultaneously treat this data as both an array and a structure (type). I’ve been there myself! I would guess that the best form for the computer is an array whereas the best conceptual representation for a human is a named data structure. I’m assuming that the rest of the program --which appears to use these functions regularly-- would likely incur execution speed penalties by constantly restructuring the data. Would that be correct? …if so, then this is not only a style issue …but one of efficiency as well.
There’s no end to the posible applications for the ability to easily and cleanly designate abstractions of subsets of data. For example, in working with images one often has to work with subsets of data such as row, col, pixel, red, green, blue… even a sliding Window (to which a kernel is applied for convolution, etc). Some of these abstractions are very different from others.
- In the case of color channels, they’re similar to the case above where the elements of an array dimension are named.
- With a “row” of pixels, the situation is slightly different. You would be naming sets/offsets of elements.
- A sliding Window is still more complex…
I know that a lot of great work has been done in julia to simplify and make things easier but is there anything like what I’m looking for? Let’s say something like this:
julia> concept V
[ a b c ]::Int64
end
julia> typeof(V)
Concept: V::Array{Int64,3};
a, b, c = V[1], V[2], V[3] # or something better ;)
julia> v = V(1,2,3)
1×3 ConceptualArray{Int64,3}:
1 2 3
julia> v.b
2
julia> v[2]
2
julia> v .^ 2
1×3 ConceptualArray{Int64,3}:
1 4 9
The word abstract is already being used so I’ll use “concept”.
The objective here is to have a very clean and simple way of accessing data from within different conceptual frameworks depending on needs and thereby simplify otherwise complex code. I will give/invent a more complex example:
julia> concept Pixel, Color # pixel same as color when ignoring image's context
[ red green blue alpha ]::UInt8
RGB, RGBA, HSV # Associated "concepts" help define the way data is accessed
end
julia> concept Row # row must access the width variable from it's parent
[ Offset() : Next() ]::Pixel # Asume internally defined: "Offset() = index*width, Next() = Offset()+width"
Pixel, Color
end
julia> concept Col; ... ; end
julia> concept Layer; ... ; end
julia> concept Window; ... ; end
julia> concept Kernel; ... ; end
julia> concept Image
[ width, height ]::Pixel # let's say pixel is the base unit
Pixel, Color, Row, Col, Layer, Window
image() = new(width, height) # hmm... probably not right!
end
julia> pic = Image( 150, 150 ) # Let's create an image!
150×150 ConceptualArray{Pixel,2}: # something similar to this...
julia> pic[10,10].Pixel
1x4 ConceptualArray{Int8,1}:
... # probably = "[0 0 0 0]" since we didn't set color data; let's set them...
julia> pic[10,10].Pixel = RGB[220 20 60] # let's say this sets it to crimson
julia> pic[10,10].Pixel
1x4 ConceptualArray{Int8,1}:
220 20 60
I hope the idea is clear enough. I haven’t really put enough thought into implementation yet but I am looking forward to getting some feedback on this.
How much does this idea overlap with the NamedArrays package? …I think it’s different. Is this realistic? Would anyone else like something like this? …or am I missing something and it already exists? Would this be compatible with the julian way of thinking? How would you further simplify and polish this -thrown together- set of examples??? Could this be extended beyond arrays?