Jump to page: 1 2
Thread overview
Removing an object from a range
Dec 13, 2010
Andrej M.
Dec 13, 2010
Jonathan M Davis
Dec 13, 2010
Andrej Mitrovic
Dec 13, 2010
Andrej Mitrovic
Dec 13, 2010
Jonathan M Davis
Dec 13, 2010
bearophile
Dec 13, 2010
Matthias Walter
Dec 13, 2010
Andrej Mitrovic
Dec 13, 2010
Andrej Mitrovic
Dec 13, 2010
Andrej Mitrovic
Dec 13, 2010
Jesse Phillips
Dec 13, 2010
spir
December 13, 2010
I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:

private DrawingElement[] elements;

public override void Remove(DrawingElement d)
{
    foreach (i, child; elements)
    {
        if (child == d)
        {
            elements = remove(elements, i);
            break;
        }
    }
}

Ugly! :)

It's a direct port from some C# code so don't mind the silly variable names.

December 13, 2010
On Sunday 12 December 2010 17:43:12 Andrej M. wrote:
> I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:
> 
> private DrawingElement[] elements;
> 
> public override void Remove(DrawingElement d)
> {
>     foreach (i, child; elements)
>     {
>         if (child == d)
>         {
>             elements = remove(elements, i);
>             break;
>         }
>     }
> }
> 
> Ugly! :)
> 
> It's a direct port from some C# code so don't mind the silly variable names.

Hmm. I thought that there was such a functon, but apparently not. The various remove functions are normally implemented on a container and are listed in std.container. However, arrays obviously don't have them built in, so they'd need them in std.array (just like popFront() and the like are in std.array), but they aren't there. So, they obviously need to be added. They may not have been created yet because std.container is still fairly experimental, but certainly once we're sure of what std.container's remove functions should look like, std.array should have them for arrays.

I created an enhancement request for it: http://is.gd/iDSqj

- Jonathan M Davis
December 13, 2010
On 12/13/10, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> I created an enhancement request for it: http://is.gd/iDSqj
>
> - Jonathan M Davis
>

Thanks!
December 13, 2010
On a side note I have to admit I've never programmed in C# before, but I don't seem to have much trouble understanding it. From the looks of it, the two languages borrow a lot from each other, and have very similar syntax.

On 12/13/10, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 12/13/10, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
>> I created an enhancement request for it: http://is.gd/iDSqj
>>
>> - Jonathan M Davis
>>
>
> Thanks!
>
December 13, 2010
On Sunday 12 December 2010 18:35:38 Andrej Mitrovic wrote:
> On a side note I have to admit I've never programmed in C# before, but I don't seem to have much trouble understanding it. From the looks of it, the two languages borrow a lot from each other, and have very similar syntax.

They're both based on C++ (though C# is also heavily based on Java which is based on C++). D probably does take some features from C# (such as delegates), but D has taken features from a number of different languages. I doubt that anything in C# is based on anything in D though. C# is from Microsoft after all, and D is still a relatively minor language.

- Jonathan M Davis
December 13, 2010
On 12/12/2010 08:43 PM, Andrej M. wrote:
> I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:
>
> private DrawingElement[] elements;
>
> public override void Remove(DrawingElement d)
> {
>     foreach (i, child; elements)
>     {
>         if (child == d)
>         {
>             elements = remove(elements, i);
>             break;
>         }
>     }
> }
Just want to mention that this code does not remove "any matches",  but only the first one! So if you want all removed, you have to improve it a bit.

Matthias
December 13, 2010
On 12/13/10, Matthias Walter <xammy@xammy.homelinux.net> wrote:
> On 12/12/2010 08:43 PM, Andrej M. wrote:
>> I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:
>>
>> private DrawingElement[] elements;
>>
>> public override void Remove(DrawingElement d)
>> {
>>     foreach (i, child; elements)
>>     {
>>         if (child == d)
>>         {
>>             elements = remove(elements, i);
>>             break;
>>         }
>>     }
>> }
> Just want to mention that this code does not remove "any matches",  but only the first one! So if you want all removed, you have to improve it a bit.
>
> Matthias
>

Yeah that's what the break is for. But that shouldn't be a test for equality either. It should rather be:

>>     foreach (i, child; elements)
>>     {
>>         if (child is d)
>>         {
>>             elements = remove(elements, i);
>>             break;
>>         }
>>     }

Because I would really need to be looking for a specific object, I think. Anyway it's just some code from some design patterns, demonstration code..
December 13, 2010
Jonathan M Davis:

> They're both based on C++ (though C# is also heavily based on Java which is based on C++). D probably does take some features from C# (such as delegates), but D has taken features from a number of different languages. I doubt that anything in C# is based on anything in D though. C# is from Microsoft after all, and D is still a relatively minor language.

Unfortunately D copies many things (especially about OOP) from Java, but it copies almost nothing from C#. I have suggested Walter many times to take a look at C#, I think with no result. I prefer D over C# but C# is far from being a badly designed language, and some of its details may be worth stealing...

Bye,
bearophile
December 13, 2010
On Sun, 12 Dec 2010 20:43:12 -0500, Andrej M. <none@none.com> wrote:

> I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:
>
> private DrawingElement[] elements;
>
> public override void Remove(DrawingElement d)
> {
>     foreach (i, child; elements)
>     {
>         if (child == d)
>         {
>             elements = remove(elements, i);
>             break;
>         }
>     }
> }
>
> Ugly! :)
>
> It's a direct port from some C# code so don't mind the silly variable names.

does this work?

elementsRemoved = std.algorithm.remove!((child){return child == d})(elements);

From there, you can shrink the original array like:

elements = elements[0..$-elementsRemoved.length];

Search for remove on http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html (there are several cases, look for the one that takes a predicate)

-Steve
December 13, 2010
On Sun, 12 Dec 2010 20:43:12 -0500
"Andrej M." <none@none.com> wrote:

> I can't seem to find an easy remove method in std.algorithm that takes an object and a range (an array in this case) and removes any matches from the range. I'm using this snippet for now:
> 
> private DrawingElement[] elements;
> 
> public override void Remove(DrawingElement d)
> {
>     foreach (i, child; elements)
>     {
>         if (child == d)
>         {
>             elements = remove(elements, i);
>             break;
>         }
>     }
> }
> 
> Ugly! :)
> 
> It's a direct port from some C# code so don't mind the silly variable names.

A common idiom in various languages for remove is collection.replace(something, nothing). (This indeed works fine in D for strings :-)


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

« First   ‹ Prev
1 2