Thread overview | |||||
---|---|---|---|---|---|
|
October 16, 2014 Check if give an array is equal to first elements in another array | ||||
---|---|---|---|---|
| ||||
Is there one function in the Phobos library to check if give an array is equal to first elements in another array? e.g, f(a, b) the entire b array must match to first elements in a and then return true otherwise false, ie: > a[0 .. b.length] == b probably there's no such a function (but I wouldn't find bad if there was) in the D's library so what's a good name (that'a descriptive) to such a function? btw, what it solves is: > if(a.length >= b1.length && a[0 .. b1.length] == b1) { ... } > if(a.length >= b2.length && a[0 .. b2.length] == b2) { ... } since if bx.length < a.length is valid I can't remove all the a.length >= bx.length checks otherwise I can have a range violation error |
October 16, 2014 Re: Check if give an array is equal to first elements in another array | ||||
---|---|---|---|---|
| ||||
Posted in reply to MachineCode | On Thursday, 16 October 2014 at 04:35:13 UTC, MachineCode wrote:
> Is there one function in the Phobos library to check if give an array is equal to first elements in another array?
auto n = min(a.length, b.length);
if (a[0..n] == b[0..n]) ...
|
October 16, 2014 Re: Check if give an array is equal to first elements in another array | ||||
---|---|---|---|---|
| ||||
Posted in reply to MachineCode | On 10/15/2014 09:35 PM, MachineCode wrote:
> Is there one function in the Phobos library to check if give an array is
> equal to first elements in another array? e.g, f(a, b) the entire b
> array must match to first elements in a and then return true otherwise
> false, ie:
>
>> a[0 .. b.length] == b
>
> probably there's no such a function (but I wouldn't find bad if there
> was) in the D's library so what's a good name (that'a descriptive) to
> such a function?
>
> btw, what it solves is:
>
>> if(a.length >= b1.length && a[0 .. b1.length] == b1) { ... }
>> if(a.length >= b2.length && a[0 .. b2.length] == b2) { ... }
>
> since if bx.length < a.length is valid I can't remove all the a.length
> >= bx.length checks otherwise I can have a range violation error
import std.algorithm;
void main()
{
auto a = [ 1, 42, 100, 7 ];
auto b = [ 1, 42 ];
assert(a.startsWith(b));
}
Ali
|
Copyright © 1999-2021 by the D Language Foundation