digitalmars.D - Do you want add contains and remove item Function in array?
- FrankLike (29/29) Feb 05 2015 Now operate array is not very quick,such as contains
- Daniel Murphy (1/1) Feb 05 2015 Contains: array.canFind(element)
- FrankLike (3/4) Feb 05 2015 Oh, canFind is better than Contains.
- FrankLike (4/4) Feb 05 2015 On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote:
- Daniel Murphy (7/9) Feb 05 2015 There is http://dlang.org/phobos/std_algorithm.html#.remove which can be...
- BBaz (26/55) Feb 05 2015 If you encounter difficulties to memorize the functions then you
- FrankLike (2/63) Feb 05 2015 Thank you very much.
Now operate array is not very quick,such as contains function,remove item function can't get from arry module. template contains(T) { bool contains(T[] Array,T Element) { foreach(T ArrayElement; Array) { if(Element==ArrayElement) { return true; } } return false; } } template remove(T) { bool remove(T[] Array,T Element) { ????? return true; } } or remove!("a == ?")(arr) How to get the easy and quickly way? Thank you.
Feb 05 2015
On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote:Contains: array.canFind(element)Oh, canFind is better than Contains. Thank you.
Feb 05 2015
On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote: But do you have some better way for remove item ? Thank you.
Feb 05 2015
"FrankLike" wrote in message news:yuinuamnmiqjxqatrvmv forum.dlang.org...But do you have some better way for remove item ?used like removeAt(index). array = array.remove(3); And it looks like there's an overload that can remove matching elements like this: array = array.remove!(e => e == value);
Feb 05 2015
On Thursday, 5 February 2015 at 14:09:06 UTC, FrankLike wrote:Now operate array is not very quick,such as contains function,remove item function can't get from arry module. template contains(T) { bool contains(T[] Array,T Element) { foreach(T ArrayElement; Array) { if(Element==ArrayElement) { return true; } } return false; } } template remove(T) { bool remove(T[] Array,T Element) { ????? return true; } } or remove!("a == ?")(arr) How to get the easy and quickly way? Thank you.If you encounter difficulties to memorize the functions then you can wrap the usefull thing in a struct, e.g: --- struct array(T) { T[] _arr; alias _arr this ; bool opIn_r(T)(T t) { import std.algorithm; return canFind(_arr, t); } alias canFind = opIn_r; typeof(_arr) remove(T t) { _arr = std.algorithm.remove(_arr, t); return _arr; } } --- If the editor you use has a completion proposal system then it'll work like a charm. I've myself in the process of doing something similar for DList because i've been intoxicated for years with the easiness of the pascal Run Time Library lists (they have exchange, add , remove etc...)
Feb 05 2015
On Thursday, 5 February 2015 at 15:21:56 UTC, BBaz wrote:On Thursday, 5 February 2015 at 14:09:06 UTC, FrankLike wrote:Thank you very much.Now operate array is not very quick,such as contains function,remove item function can't get from arry module. template contains(T) { bool contains(T[] Array,T Element) { foreach(T ArrayElement; Array) { if(Element==ArrayElement) { return true; } } return false; } } template remove(T) { bool remove(T[] Array,T Element) { ????? return true; } } or remove!("a == ?")(arr) How to get the easy and quickly way? Thank you.If you encounter difficulties to memorize the functions then you can wrap the usefull thing in a struct, e.g: --- struct array(T) { T[] _arr; alias _arr this ; bool opIn_r(T)(T t) { import std.algorithm; return canFind(_arr, t); } alias canFind = opIn_r; typeof(_arr) remove(T t) { _arr = std.algorithm.remove(_arr, t); return _arr; } } --- If the editor you use has a completion proposal system then it'll work like a charm. I've myself in the process of doing something similar for DList because i've been intoxicated for years with the easiness of the pascal Run Time Library lists (they have exchange, add , remove etc...)
Feb 05 2015