Finding and removing an element from an array

5,790 views
Skip to first unread message

Nathan Hüsken

unread,
Jan 31, 2014, 10:51:37 AM1/31/14
to haxe...@googlegroups.com
Hey,

A task I find myself to do very often is iterating through an array
searching for an element (the first element) matching a certain criteria
and than removing it from the array.
I like to use the Lambda functions for tasks on arrays, but as far as I
can see this does not seem to be possible for this. So I do:

for (i in 0...array.length) {
if (criteria(array[i])) {
var remember = array[i];
array[i] = array[array.length-1];
array.pop();
return remember;
}
}

So I am wondering if it would not be nice to have some function in
Lambda that is more helpful here.
A function "indexOfCrit" (probably there should be a better name)
returning the first element matching a criteria (as for the exists
function) would be nice!
Regards,
Nathan

LE JOLLY David

unread,
Jan 31, 2014, 1:41:01 PM1/31/14
to haxe...@googlegroups.com
Maybe you can use Lambda.filter (array, criteria).first() and then remove the result from the array.

Best,
David Le Jolly



2014-01-31 Nathan Hüsken <nathan....@posteo.de>:


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
--- You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.



--
Le Jolly David
Résidence Montesquieu porte 6
5Bis rue Montesquieu
49000 ANGERS

06 67 56 57 93
09 53 85 50 79

Nathan Hüsken

unread,
Feb 1, 2014, 7:26:07 AM2/1/14
to haxe...@googlegroups.com
Possible yes. But if the criteria meets 90% of the array, would that approach not copy 90% if the array (due to lack of lazy evaluation)?
That can get very inefficient in certain situations ...

Raivo Fish

unread,
Feb 1, 2014, 8:02:50 AM2/1/14
to haxe...@googlegroups.com
Like this:

var a:Array<String> = ['abc', 'def', '123'];
var pos = a.indexOf('def');
if (pos >= 0) {
  a.splice(pos, 1);
}
trace(a);

Raivo Fish

unread,
Feb 1, 2014, 8:03:50 AM2/1/14
to haxe...@googlegroups.com
ahh, you need specific criteria, my sample works only for equality.

Saar Korren

unread,
Feb 1, 2014, 9:05:47 AM2/1/14
to haxe...@googlegroups.com
On the off chance of sounding like Not-Invented-Here-Syndrome, if you're lacking a function for this, why not write one? I mean, that's the great thing about being a programmer. You're not limited by what's available. You can always write your own stuff.

Not EVERYTHING has to be handled by standard libraries. Sometimes it helps to have your own utility class which is reused by your programs. The code snippet you placed can easily be written as both a function which takes "criteria" as an argument, or as a macro.

Unless, of course, you can come up with a convincing argument as to why this would be a common use-case for people other than yourself.


On Friday, January 31, 2014 5:51:37 PM UTC+2, Nathan Hüsken wrote:

Simon Krajewski

unread,
Feb 1, 2014, 9:20:23 AM2/1/14
to haxe...@googlegroups.com
This kind of operation does not make much sense from a Lambda point of
view because Lambda assumes immutable collections. Operations in
Lambda.hx are defined for Iterable<T> types, which have no notion of
removing elements from them or modifying them in any other way.

For arrays you could utilize the splice method and wrap it in some
helper function. It may be worth considering a different data structure
altogether, because something like a linked list would make this
operation trivial (and much more efficient).

Simon

Nathan Hüsken

unread,
Feb 1, 2014, 2:03:14 PM2/1/14
to haxe...@googlegroups.com
You are of course right. My post was more like "I use this often, maybe other use it often too and it should be in the standard library".
But if course, having my own utility function is fine.
--
Reply all
Reply to author
Forward
0 new messages