www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Removing an object from a range

reply Andrej M. <none none.com> writes:
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.
Dec 12 2010
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Dec 12 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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!
Dec 12 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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!

Dec 12 2010
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Dec 12 2010
parent bearophile <bearophileHUGS lycos.com> writes:
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
Dec 12 2010
prev sibling next sibling parent Matthias Walter <xammy xammy.homelinux.net> writes:
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;
         }
     }
 }

only the first one! So if you want all removed, you have to improve it a bit. Matthias
Dec 12 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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;
         }
     }
 }

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..
Dec 12 2010
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Dec 13 2010
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Does anyone know if there's any way I can get special highlighting for
lambda functions in say, Vim? It gets hard to distinguish between
regular parameters and one-liner lambdas, if I could change the
background color of a lambda it could really help out..

On 12/13/10, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:
 On 12/13/10, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 does this work?

Yes, ty. But I think there's a typo in std.algorithm.remove: Range remove(alias pred, SwapStrategy s = SwapStrategy.stable, Range)(Range range); Reduces the length of the bidirectional range range by only keeping elements that satisfy pred. This should probably be "by only keeping elements that *do not* satisfy pred.", right? Here's a short example where I just shorten the length of an array: http://pastebin.com/gL6EYJzd

Dec 13 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic Wrote:

 Does anyone know if there's any way I can get special highlighting for
 lambda functions in say, Vim? It gets hard to distinguish between
 regular parameters and one-liner lambdas, if I could change the
 background color of a lambda it could really help out..

Off the top of my head I can't think of how it would be done. But you are welcome to make a feature request: https://github.com/he-the-great/d.vim
Dec 13 2010
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
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=

e range. I'm using this snippet for now:
=20
 private DrawingElement[] elements;
=20
 public override void Remove(DrawingElement d)
 {
     foreach (i, child; elements)
     {
         if (child =3D=3D d)
         {
             elements =3D remove(elements, i);
             break;
         }
     }
 }
=20
 Ugly! :)
=20
 It's a direct port from some C# code so don't mind the silly variable nam=

=20 A common idiom in various languages for remove is collection.replace(someth= ing, nothing). (This indeed works fine in D for strings :-) Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 13 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 12/13/10, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 does this work?

Yes, ty. But I think there's a typo in std.algorithm.remove: Range remove(alias pred, SwapStrategy s = SwapStrategy.stable, Range)(Range range); Reduces the length of the bidirectional range range by only keeping elements that satisfy pred. This should probably be "by only keeping elements that *do not* satisfy pred.", right? Here's a short example where I just shorten the length of an array: http://pastebin.com/gL6EYJzd
Dec 13 2010