On Sunday, 4 September 2016 at 14:49:30 UTC, Lodovico Giaretta wrote:
On Sunday, 4 September 2016 at 14:24:12 UTC, data pulverizer wrote:
On Sunday, 4 September 2016 at 14:20:24 UTC, data pulverizer wrote: @Lodovico Giaretta BTW what do you mean that my code is not very D style? Please expand on this ...

The constructors can be less. In fact, a typesafe variadic ctor also works for the single element case and for the array case. But you already recognized that.

Instead of reinventing the wheel for your GenericVector!T, you could use an `alias this` to directly inherit all operation on the underlying array, without having to reimplement them (like your append method).

Your getCol(i) could become getCol!T(i) and return an instance of GenericVector!T directly, after checking that the required column has in fact that type:

GenericVector!T getCol!T(size_t i)
{
    if(typeid(cols[i]) == typeid(GenericVector!T))
        return cast(GenericVector!T)cols[i];
    else
        // assert(0) or throw exception
}

Another solution: if you don't need to dynamically change the type of the columns you can have the addColumn function create a new type. I show you with Tuples because it's easier:

Tuple!(T,U) append(U, T...)(Tuple!T tup, U col)
{
    return Tuple!(T,U)(tup.expand, col);
}

Tuple!int t1;
Tuple!(int, float) t2 = t1.append(2.0);
Tuple!(int, float, char) t3 = t2.append('c');

Thank you for the very useful suggestions, I shall take these forward. On the suggestion of creating Tuple-like tables, I already tried that but found as you said that once the table is created, adding/removing columns is essentially creating a different data type, which needs a new variable name each time.

I am building a table type I hope will be used for data manipulation for data science and statistics applications, so I require a data structure that can allow adding and removing columns of various types as well as a data structure that can cope with any type that hasn't been planned for, which is why I selected this polymorphic template approach. It is more flexible than other data structures I have seen in dynamic programming languages R's data frame and Python pandas. Even Scala's Spark dataframes rely on wrapping everything in Any and the user still has to write a special data structure for each new type. The only thing that is similar to this approach is Julia's DataFrame but Julia - though a very good programming language has limitations.

I feel as if I am constantly scratching the surface of what D can do, but I have recently managed to get more time on my hands and it looks as if that will continue into the future which will mean more focusing on D, improving my generic programming skills and hopefully creating some useful artifacts. Perhaps I need to read Andrei's Modern C++ Design book for a better way to think about generics.


Reply via email to