Proposal for a set handling library

Raphaël Jakse raphael.jakse at gmail.com
Thu Jan 2 00:09:54 PST 2014


Le 02/01/2014 01:44, bearophile a écrit :
> Raphaël Jakse:
>
>>         auto E = new Set!int([1,2,3,4]); // it is possible to declare
>> an empty set
>
> You can add a factory helper:
> auto e = set([1, 2, 3, 4]);
>
> Or even:
> auto e = set(1, 2, 3, 4);

I'm going to add this.
Two questions:
  - By default, should set be typed or untyped?
    I would go for typed set, but what about people's expectations?
  - What is preferable: first version or second version of the proposed 
set factory function? (imho both cannot co-exist because of sets of 
arrays handling)
     * The first version allows creation of a set from a array. That is 
already possible with the constructor, but then you have to pass the 
type template, this is not beautiful.
     * The second version is more pretty. But then, we'll need a 
setFromArray function which makes a set from a list.
    I prefer the second option, but then, set([1, 2, 3, 4]) would make a 
set containing on element: an array of four ints. Could this be misleading?



>
>
>>         writeln("union: ", E.Union(F));
>>         writeln("inter: ", E.Inter(F));
>>         writeln("symmetric difference: ", E.SymDiff(F));
>>         writeln("minus: ", E.Minus(F));
>>         writeln("Powerset: ", E.Powerset);
>
> In idiomatic D all the member functions start with a lowercase letter.

I prefer lowercase for first letters too, but there is the "union" 
member, which is a keyword, so I put them all in uppercase.

Any idea?

>
> And a little operator overloading helps, look at the Python set() for a
> nice API.
>
>
>>             // beware, because S is an untyped set (Set!() or
>> Set!Object),
>
> Probably in idiomatic D you use templates, so Set!Object is a very
> uncommon usage, unlike Java.

What do you mean?


about Set!Object:

I needed a means to specify the type of elements in the Set. So I went 
with Set!the_type. Then, I needed a special type to make sets able to 
hold any value. I went for Object. void might have been nice, though it 
would probably have complicated things.

I tried to make `auto S = new Set;` create a untyped set, but I didn't 
succeed.

So again, what do you suggest?

>
>
>> Should Phobos have something similar built in?
>
> A set is a good thing to have in Phobos, but the API and design should
> be a little different from yours.

I'm eager to make my library close to "idiomatic D", so feel free to 
make any remark, suggestion for this or for anything else.

>
> Bye,
> bearophile



More information about the Digitalmars-d mailing list