Manipulate slice or specific element of range and return range

David Bennett davidbennett at bravevision.com
Thu Mar 22 07:34:18 UTC 2018


On Thursday, 22 March 2018 at 04:49:39 UTC, Seb wrote:
>
> No need for regular expressions. D is powerful enough without 
> them:
>
> ```
> alias lowercased = (m, n) => 
> m.take(n).asLowerCase.chain(m.drop(n));
> ```
>
> https://run.dlang.io/is/cSL0si

And with the filter, so it passes the assert:

```
string input = "My Capital String";
auto lower1 = 
input.take(1).asLowerCase.chain(input.drop(1).filter!(c => c != ' 
')).array;
assert(lower1 == "myCapitalString");
```

Also a few more options (including a slice version):

https://run.dlang.io/is/dZHcSo

As the input is never read until the array() is run, each element 
in the array should only be copied from RAM to the stack once.

And if you compile with LDC a lot of it can be inlined and the 
stack copies of the range structs and elements should be 
minimised even further.


More information about the Digitalmars-d-learn mailing list