July 21, 2008
Luis P. Mendes:
> So can I define something as this kind of list:
> [type char, type int, type float,...] ?

There are arrays of boxes/variants that allows you to used mixed types, but you generally don't want to use them in your D code (and boxes have some bugs too).

You may want to use a struct for that. There are ways to create them on the fly too (Tuple!(), toTuple() in std.typecons and in my libs), etc.


>I wanted to know if I can load values for attributes in a concise manner.<

Can you show a working Python example of what you mean? So I/we can avoid guessing many times.


>For example, right now I'm working on a project that reads and writes circa 40 values for each instance of an object and in Python I'm able to load the values on a for loop as mentioned above.<

For example objects and structs have the .tupleof that allows you to refer to attributes as items of a untyped array.
And for example in my libs you can find HasMethod!() and autoAssign(), that can be used like (I don't use them much, but they are there, I'll prune away little used stuff in the future, I'm in the inflation phase still):

assert( HasMethod!(__LINE__, MyClass, "foo", int, float) );

assert( HasMethod!(__LINE__, MyClass, "foo") );

(I'd like to remove that __LINE__).

// And:
this(int first, int second, int third) {
    mixin(autoAssign("first second third"));
}

// Is the same as:
this(int first, int second, int third) {
    this.first = first;
    this.second = second;
    this.third = third;
}

So if you know attribute names at compile time you probably need "just" some template trickery to solve your problem (but despite D templates being simpler than C++ ones it may require you some time to learn to use them to perform such tricks).

-----------------

Jarrett Billingsley:

>I'm not sure what connection bearophile was making between what OS you use and what version of D to use.<

Generally I talk only about things I know. Not knowing much about D compilers on Linux, I have restricted my suggestion to the Win case :-) Sorry for not being more explicit in my post.

Bye,
bearophile
July 21, 2008
Mon, 21 Jul 2008 13:10:54 -0400, bearophile wrote:

>>I wanted to know if I can load values for attributes in a concise manner.<
> 
> Can you show a working Python example of what you mean? So I/we can avoid guessing many times.
Sure, for example:

self.lista_campos:  ['indice', 'emac_periodos', 'emal_bool', 'emal_periodos', 'tr1_bool', 'tr1_periodos', 'tr1_sinal', 'tr1_percent', 'tr2_bool', 'tr2_periodos', 'tr2_sinal', 'tr2_percent', 'tr2_vezes']

individuo_sql:  (2264, 42, True, 88, True, 15, '>', Decimal("0.49"), False, 6, '<', Decimal("0.84"), 4]

for atributo, valor in zip(self.lista_campos, individuo_sql): setattr
(self, atributo, valor)

so that:
print self.indice
2264

print self.emac_periodos
42

and so on.

In this one I also used a zip function to pack values from two lists together.

> 
> 
>>For example, right now I'm working on a project that reads and writes circa 40 values for each instance of an object and in Python I'm able to load the values on a for loop as mentioned above.<
> 
> For example objects and structs have the .tupleof that allows you to refer to attributes as items of a untyped array. And for example in my libs you can find HasMethod!() and autoAssign(), that can be used like (I don't use them much, but they are there, I'll prune away little used stuff in the future, I'm in the inflation phase still):
> 
> assert( HasMethod!(__LINE__, MyClass, "foo", int, float) );
> 
> assert( HasMethod!(__LINE__, MyClass, "foo") );
> 
> (I'd like to remove that __LINE__).
> 
> // And:
> this(int first, int second, int third) {
>     mixin(autoAssign("first second third"));
> }
> 
> // Is the same as:
> this(int first, int second, int third) {
>     this.first = first;
>     this.second = second;
>     this.third = third;
> }
> 
> So if you know attribute names at compile time you probably need "just" some template trickery to solve your problem (but despite D templates being simpler than C++ ones it may require you some time to learn to use them to perform such tricks).
Yes, I guess so! :-)


Luis

July 22, 2008
Luis P. Mendes wrote:
> Hi,
> 
> I use to program in Python, but I need some programs to run much faster.  So, D seems to be as the best programming language for my needs.
>  Still, there's a long way to go because I've never programmed in C.
> 
> To begin with, is version 2 just a developer version or should I start by using it? 
> 
> In Python, lists can have variables, functions, text and others as elements.  As far as I can recall from reading a C book, arrays in C don't have this possibility.  What about it in D?  
> 
> And regarding list comprehensions like li = [elem*2 for elem in li]?  is there something close in D?
> 
> For example, how could I do something like:
> valores = []
> for c in lista_campos: valores.append(getattr(self,c)), so that I can have all the 
> 
> Is there also any construct similar to dictionaries?
> 
> Are there D libraries to access PostgreSQL database or do I have to use C's?
> 
> I don't know about the theoretical issues regarding language development, so documentation with good examples is a must have. Is there something like www.diveintopython.org in the short term horizon?
> 
> These are just some topics I need to know about D.  I'd appreciate some answers.
> 
> 
> Luis

Ok, I assume you might know some of this but I'm going to talk about some of the basics.  Also note that I have the barest knowledge about python.

D should be faster then Python in the general case.  Its very easy to drop down to the bare metal when necessary.  Its a systems language after all.

You'll find that you won't always get as concise syntax as python.  I don't think that's necessarily a bad thing as it provides extra type safely that you don't get in python.  Note: typesafty also means you'll need to write less unit checks because the compiler will do a lot of that testing for you, which I think is an actual time saver.

Having said that D is certainly easier to write then C and provides nicer lists.  Note lists are typesafe as well so you can only put one type of thing into them.  That's not a bad thing (in my option) because normally you need to know what you are going to pull out in the first place.

Instead of Duck typing you'll need to learn use inheritance and polymorphism.  This is yet another form of compiletime/typesafety.

A website you might find useful is the wiki: http://prowiki.org/wiki4d/wiki.cgi

-Joel
July 22, 2008
bearophile wrote:
<snip>
> If you take a look at the Lua language, you can see that they have recently found ways to speed up their associative arrays significantly.

Do you have a website about this?  How recent?  What version?

Cheers,
JAnderson
July 22, 2008
JAnderson:
> Do you have a website about this?  How recent?  What version?

I don't think that's much recent, here, from chapter 4, page 6-7, from lua 5.0: http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf

Bye,
bearophile
July 22, 2008
bearophile wrote:
> JAnderson:
>> Do you have a website about this?  How recent?  What version?
> 
> I don't think that's much recent, here, from chapter 4, page 6-7, from lua 5.0:
> http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf
> 
> Bye,
> bearophile

ok cheers.  I was just wondering if I should upgrade Lua since its one of my bottlenecks however I'm using 5.

Cheers,
-Joel
July 22, 2008
On Mon, 21 Jul 2008 12:32:12 -0400, Jarrett Billingsley wrote:

> "Luis P. Mendes" <luislupeXXXX@gmailXXXX.com> wrote in message
>> So can I define something as this kind of list: [type char, type int, type float,...] ?
> 
> No.  There are structures and classes if you want to group multiple different types, as well as tuples, but they are not first-class objects as in Python and are strictly compile-time entities.
> 
> It's not that lists of varying types aren't useful, it's that the way you'd do it in D would be (possibly very) different from the way you'd do it in Python, is all.  Syntax is sugar.  It's not enough to say that "language X doesn't have feature Y so it's not possible to do algorithm Z."  It probably is possible, just not in the same way as if you had feature Y.
It's fine with me to have different syntax.

One of the things that caught my eye on D website is that it is said that D combines power and performance of C, C++ with programmer productivity of modern languages like Ruby and Python.  This is the main reason I posted to this group. I wanted to know from very knowledgeable peoplein D whether there were similar structures between D and Python (with increased performance), at least the ones I keep on using in Python.


Luis
July 22, 2008
JAnderson:
> ok cheers.  I was just wondering if I should upgrade Lua since its one of my bottlenecks however I'm using 5.

Lua is a fast dynamic languages, so if you find bottlenecks in your Lua code you may be doing something wrong (like using Lua for the wrong purpose, etc).
Anyway, can't you use the JIT? (http://luajit.org/ ), with that Lua becomes very fast :-)

Bye,
bearophile
July 23, 2008
bearophile wrote:
> JAnderson:
>> ok cheers.  I was just wondering if I should upgrade Lua since its one of my bottlenecks however I'm using 5.
> 
> Lua is a fast dynamic languages, so if you find bottlenecks in your Lua code you may be doing something wrong (like using Lua for the wrong purpose, etc).
> Anyway, can't you use the JIT? (http://luajit.org/ ), with that Lua becomes very fast :-)
> 
> Bye,
> bearophile

Lua is fast but not as fast as C++ even with the JIT.  If things become slow in lua (or any scripting language) the typical solution is to move the slow bits over to C++.  Of course then designers can't tweak those parts anymore.

Aside from communication bottlenecks between C++ and lua, lua has all this extra overhead for allowing things like nil to be safely passed around and having objects that can be anything at anytime.  Lua's pretty fast for a scripting language.

As far as using it for the wrong purpose; typically I don't like general purpose scripting languages at all for game programming but that's just me.  However its not my decision to use it.  I like constrained DSL languages (that are tool specific) that meet the task and I'm not one to do unnecessary work if I don't need to.  I've only come to that concussion from seeing the results both approaches applied many times.

The one argument that half makes sense to me is the cost of VS argument, however I still think a constrained lib is better.  A few years ago when I was using it Lua was so untype safe that I've spend more time fighting issues brought up by designers then I think its actually saved.

I think there may be a good use for something like lua in game programming however I haven't seen it yet.

-Joel
July 24, 2008
On Mon, 21 Jul 2008 12:25:26 -0400, Jason House wrote:

> Luis P. Mendes Wrote:
>> >> And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D?
>> > 
>> > Depends on how you define close. There is foreach and with some template trickery you can get similar syntax:
>> > 
>> > // Please note that this works only if you define the template "each"
>> > yourself!
>> > [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}",
>> > value).newline; });
>> > 
>> > But I would recommend you concentrate on the basic stuff first.
>> By something close I meant some kind of (high-level) construct that D would have to offer.  As in this example, as in other I wrote I didn't want to mimic Python.
> 
> I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like:
> 
> foreach(index, elem; li)
>   li[index] = elem*2;
> 
> Note that I may have reversed the order of index and elem.  I never remember which one should come first...

You are correct that index occurs first.