Jump to page: 1 2
Thread overview
delete an element from an array
Oct 24, 2010
Adam Cigánek
Oct 24, 2010
Simen kjaeraas
Oct 24, 2010
Adam Cigánek
Oct 24, 2010
Stewart Gordon
Oct 24, 2010
Adam Cigánek
Oct 24, 2010
Jonathan M Davis
Oct 24, 2010
bearophile
Oct 24, 2010
spir
Oct 24, 2010
bearophile
Oct 24, 2010
Adam Cigánek
Oct 24, 2010
spir
Oct 24, 2010
Adam Cigánek
Oct 24, 2010
Manfred_Nowak
Oct 24, 2010
bearophile
Oct 24, 2010
spir
Oct 24, 2010
bearophile
Oct 25, 2010
Manfred_Nowak
Oct 26, 2010
Michael Rynn
October 24, 2010
Hello,

Is there a function in the standard library to delete an element from an array (or range)? Something like:

  auto a = [1, 2, 3, 4, 5, 6];
  auto b = delete(a, 4);

  assert([1, 2, 3, 4, 6] == b);

I've noticed there is eliminate in std.algorithm, which seems to be doing just that, but it's commented out.

It's not difficult to roll my own, (with the help of indexOf and remove), but I thought that this is pretty common operation so it should be in the standard library.


adam.
October 24, 2010
On Sun, 24 Oct 2010 13:02:24 +0200, Adam Cigánek <adam.ciganek@gmail.com> wrote:

> Hello,
>
> Is there a function in the standard library to delete an element from
> an array (or range)? Something like:
>
>   auto a = [1, 2, 3, 4, 5, 6];
>   auto b = delete(a, 4);
>
>   assert([1, 2, 3, 4, 6] == b);
>
> I've noticed there is eliminate in std.algorithm, which seems to be
> doing just that, but it's commented out.
>
> It's not difficult to roll my own, (with the help of indexOf and
> remove), but I thought that this is pretty common operation so it
> should be in the standard library.

std.algorithm has remove, which does what you want.

-- 
Simen
October 24, 2010
remove removes element at a given offset. I want to remove element with a given value. This is example shows it better:

  auto a = ["foo", "bar", "baz"];
  auto b = delete(a, "bar");

  assert(["foo", "baz"] == b);


adam.

2010/10/24 Simen kjaeraas <simen.kjaras@gmail.com>:
> On Sun, 24 Oct 2010 13:02:24 +0200, Adam Cigánek <adam.ciganek@gmail.com> wrote:
>
>> Hello,
>>
>> Is there a function in the standard library to delete an element from an array (or range)? Something like:
>>
>>  auto a = [1, 2, 3, 4, 5, 6];
>>  auto b = delete(a, 4);
>>
>>  assert([1, 2, 3, 4, 6] == b);
>>
>> I've noticed there is eliminate in std.algorithm, which seems to be doing just that, but it's commented out.
>>
>> It's not difficult to roll my own, (with the help of indexOf and remove), but I thought that this is pretty common operation so it should be in the standard library.
>
> std.algorithm has remove, which does what you want.
>
> --
> Simen
>
October 24, 2010
On Sunday 24 October 2010 04:24:07 Adam Cigánek wrote:
> remove removes element at a given offset. I want to remove element with a given value. This is example shows it better:
> 
>   auto a = ["foo", "bar", "baz"];
>   auto b = delete(a, "bar");
> 
>   assert(["foo", "baz"] == b);
> 
> 
> adam.
> 
> 2010/10/24 Simen kjaeraas <simen.kjaras@gmail.com>:
> > On Sun, 24 Oct 2010 13:02:24 +0200, Adam Cigánek <adam.ciganek@gmail.com>
> > 
> > wrote:
> >> Hello,
> >> 
> >> Is there a function in the standard library to delete an element from an array (or range)? Something like:
> >> 
> >>  auto a = [1, 2, 3, 4, 5, 6];
> >>  auto b = delete(a, 4);
> >> 
> >>  assert([1, 2, 3, 4, 6] == b);
> >> 
> >> I've noticed there is eliminate in std.algorithm, which seems to be doing just that, but it's commented out.
> >> 
> >> It's not difficult to roll my own, (with the help of indexOf and remove), but I thought that this is pretty common operation so it should be in the standard library.
> > 
> > std.algorithm has remove, which does what you want.
> > 
> > --
> > Simen

Well, then use indexOf() to get the offset and remove() to remove the element.

- Jonathan M Davis
October 24, 2010
> Well, then use indexOf() to get the offset and remove() to remove the element.
>

Yes, that's what I'm doing. I just thought that I maybe overlook such function in the standard library. So there is none. No problem, I'll keep using my own. Still think it would be useful to add it, because I believe such operation is quite common.


thanks,
adam.
October 24, 2010
Jonathan M Davis:

> Well, then use indexOf() to get the offset and remove() to remove the element.

But you must test the result value of indexOf, because it returns -1 (a signed value, probably an integer, not a signed word, so it may give troubles on 64 bit systems) when the item is missing. This is why Python has both a way to remove an item by index and by value.

Bye,
bearophile
October 24, 2010
On Sun, 24 Oct 2010 09:31:12 -0400
bearophile <bearophileHUGS@lycos.com> wrote:

> Jonathan M Davis:
> 
> > Well, then use indexOf() to get the offset and remove() to remove the element.
> 
> But you must test the result value of indexOf, because it returns -1 (a signed value, probably an integer, not a signed word, so it may give troubles on 64 bit systems) when the item is missing. This is why Python has both a way to remove an item by index and by value.
> 
> Bye,
> bearophile

In my opinion, such non-obvious complications are good reasons to have seemingly trivial operations implemented as builtin routines. (and should throw error in case of failure)


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

spir.wikidot.com

October 24, 2010
> In my opinion, such non-obvious complications are good reasons to have seemingly trivial operations implemented as builtin routines. (and should throw error in case of failure)

Or have them in the standard library - better for keeping the language small.

adam.

>
>
> Denis
> -- -- -- -- -- -- --
> vit esse estrany ☣
>
> spir.wikidot.com
>
>
October 24, 2010
On Sun, 24 Oct 2010 15:51:29 +0200
Adam Cigánek <adam.ciganek@gmail.com> wrote:

> > In my opinion, such non-obvious complications are good reasons to have seemingly trivial operations implemented as builtin routines. (and should throw error in case of failure)
> 
> Or have them in the standard library - better for keeping the language small.
> 
> adam.

Yes, that's what I meant, in fact (but was unclear).

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

spir.wikidot.com

October 24, 2010
On 24/10/2010 12:24, Adam Cigánek wrote:
> remove removes element at a given offset. I want to remove element
> with a given value. This is example shows it better:
<snip>

Your new example doesn't show it better, it's the only one you've given that shows it at all.  What you had originally was

  auto a = [1, 2, 3, 4, 5, 6];
  auto b = delete(a, 4);

  assert([1, 2, 3, 4, 6] == b);

which shows the removal of the element at index 4, not the element with value 4.

But there are further questions to be answered:

- What do you want it to do if the value isn't in the array?  Just return the original array, return a copy of the original array, or throw an exception?

- What do you want it to do if the value is in the array more than once?  Remove the first, remove the last, remove all of them, or pick one at random?

Stewart.
« First   ‹ Prev
1 2