Few things II

bearophile bearophile
Thu Aug 9 02:01:48 PDT 2007


I have changed the way I answer, hopefully this is better, now I group answers to a person.

Sean Kelly:

>It's hardly efficient, but:<

I was looking for something O(1).


>Though I suppose a property method could be created:<

Nice, I'll try that. But maybe a syntax like this one is simpler:
myArray[1 .. 3].copyTo(2, 4);


>Not hard to do.<

Well copying large portions of hashing Python/Perl C sourcecode isn't that easy, it has to be adapted.


>A library hashtable is probably the best option for more specialized needs.<

I know, but I was talking about improving the D built in AA copying the already refined implementations that come from 15+ years of community efforts to produce the best.


>What about .stringof?<

Thank you.

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

BCS <ao at pathlink dot com>:

>under the covers, yeild is a disaster IMO<

Maybe this is true for a low level language, I don't know. But in Python it's very useful and quite commonly used (but I don't like its V.2.5 use for coroutines, it's too much confusing and its syntax is dirty because it looks like a statement used as an expression). It's used in C# too and it seem to cause no disasters :-) It avoids you to mess with the opApply.

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

Oskar Linde:

>But I agree that it would be useful to have a function that prints strings with "".<

I have already written it, it's called put/putr, and it solves all the problems I was talking of :-)


>Preferably, it should be able to convert any expression of basic types into a string representation, that when parsed by the compiler as a literal yields an identical value.<

This may require some extra testing to see if it's true.


>Ideally, a formatter should allow you to define any format you want.<

You can define all the formats you want, but sensible defaults are necessary anyway.
I was talking about the standard tools in standard configuration, that you use when you debug, when you write things quickly, etc.


>This is not a writefln inconsistency as much as it is a DMD inconsistency.<

I see, and I think it deserves to be fixed :-)


>.dup should definitely not be any slower (unless you have a GC compiled with DbC that does a memcmp after each .dup). Do you have a benchmark showing this?<

I have just written a benchmark, and now .dup is quite faster than the other (about 50% faster, and it's a repeatable result), so I shut up.


>Something like this should work with current DMD and derivations that keep the memory layout of AA's:<

Thank you, I'll test it.


>Using string mixins and some CTFE it is not that hard implementing a bit field replacement.<

Okay. Maybe this can be standardized inside a Phobos lib (unrelated: bitvectors of Phobos are nice, but they are slow compared to the old 'bit' type).


>My solution would be to make string literals dynamic arrays instead of static.<

I'd like that. I don't know what's the point of having them a different kind of strings (but I like to have a way to use static array of chars anyway, they may be useful now and then).


>I usually make my own containers when the built in AA doesn't cut it, such as:<

Designing and tuning a very good associative array/set requires months or years, so I was talking about the built-in one.


>I have implemented heaps of such functions too and I believe most people have implemented at least a few of those.<

I am sure lot of people have done similar things, because when used (and not over-used) they can help (expecially if you are a bit used to functional programming). It's just I think I have implemented lot of them, and mine are probably more flexible (they usually accept arrays, AAs and iterable objects, more than one when possible, mixed types when possible, etc. There are some limitations that I have tries to push, some can't be removed) :-)


>Luckily, Tango has some really well implemented and solid functions in tango.core.Array. A collection that can only grow.<

I have seen them, and I think they are nice, but I was talking about something for functional-style coding.


>BTW, I really like your naming convention for in place versions (as I assume they are). I might steal that right away :). Or does i mean those are iterator functions?<

You are free to steal. I can probably offer my functional module to Tango developers too. My "isomething" has the same meaning as in the itertools module of Python, they are related to lazy iterable objects.


>Another example is comparing two structs for equality.<

I see :-)


>First, the name space "pollution" is no problem in D. You can never accidentally call an unwanted overload of a function from another module. D will issue an ambiguity error when the same identifier matches instances in several different modules.<

I see.


>Is both much harder to read and parse (to me atleast) and also hard to indent in any meaningful way.<

I think both versions aren't much readable (but the first is better), I suggest you to split your lines in more parts, so you can give those parts a name and you can comment the lines too.

Thank you for the many answers, and the two snippets of code I may find useful.

Bear hugs,
bearophile



More information about the Digitalmars-d mailing list