| Thread overview | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|
February 05, 2015 Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to FrankLike | Contains: array.canFind(element) | |||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | 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.
| |||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote: But do you have some better way for remove item ? such as c#: array.remove(item) ,array.removeAt(index)? Thank you. | |||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to FrankLike | "FrankLike" wrote in message news:yuinuamnmiqjxqatrvmv@forum.dlang.org... > But do you have some better way for remove item ? > such as c#: array.remove(item) ,array.removeAt(index)? There is http://dlang.org/phobos/std_algorithm.html#.remove which can be 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); | |||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to FrankLike | 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...)
| |||
February 05, 2015 Re: Do you want add contains and remove item Function in array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BBaz | On Thursday, 5 February 2015 at 15:21:56 UTC, BBaz wrote:
> 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...)
Thank you very much.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply