digitalmars.D.learn - Removing an object from a range
- Andrej M. (15/15) Dec 12 2010 I can't seem to find an easy remove method in std.algorithm that takes a...
- Jonathan M Davis (11/33) Dec 12 2010 Hmm. I thought that there was such a functon, but apparently not. The va...
- Andrej Mitrovic (2/4) Dec 12 2010 Thanks!
- Andrej Mitrovic (5/11) Dec 12 2010 On a side note I have to admit I've never programmed in C# before, but
- Jonathan M Davis (7/11) Dec 12 2010 They're both based on C++ (though C# is also heavily based on Java which...
- bearophile (4/9) Dec 12 2010 Unfortunately D copies many things (especially about OOP) from Java, but...
- Matthias Walter (5/18) Dec 12 2010 Just want to mention that this code does not remove "any matches", but
- Andrej Mitrovic (6/36) Dec 12 2010 Yeah that's what the break is for. But that shouldn't be a test for
- Steven Schveighoffer (10/28) Dec 13 2010 does this work?
- Andrej Mitrovic (10/11) Dec 13 2010 Yes, ty. But I think there's a typo in std.algorithm.remove:
- Andrej Mitrovic (5/17) Dec 13 2010 Does anyone know if there's any way I can get special highlighting for
- Jesse Phillips (3/7) Dec 13 2010 Off the top of my head I can't think of how it would be done. But you ar...
- spir (12/31) Dec 13 2010 object and a range (an array in this case) and removes any matches from...
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! :)
Dec 12 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! :) 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
On 12/13/10, Jonathan M Davis <jmdavisProg gmx.com> wrote:I created an enhancement request for it: http://is.gd/iDSqj - Jonathan M DavisThanks!
Dec 12 2010
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 DavisThanks!
Dec 12 2010
On Sunday 12 December 2010 18:35:38 Andrej Mitrovic wrote: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.but D has taken features from a number of different languages. I doubt that all, and D is still a relatively minor language. - Jonathan M Davis
Dec 12 2010
Jonathan M Davis:but D has taken features from a number of different languages. I doubt that all, and D is still a relatively minor language.Unfortunately D copies many things (especially about OOP) from Java, but it a badly designed language, and some of its details may be worth stealing... Bye, bearophile
Dec 12 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
Dec 12 2010
On 12/13/10, Matthias Walter <xammy xammy.homelinux.net> wrote:On 12/12/2010 08:43 PM, Andrej M. wrote:Yeah that's what the break is for. But that shouldn't be a test for equality either. It should rather be: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. MatthiasBecause 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..foreach (i, child; elements) { if (child is d) { elements = remove(elements, i); break; } }
Dec 12 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! :) 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
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
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
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
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 th= 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! :) =20es. =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