countUntil to print all the index of a given string.
Seb
seb at wilzba.ch
Sun Feb 25 01:49:05 UTC 2018
On Tuesday, 20 February 2018 at 08:44:37 UTC, aberba wrote:
> On Sunday, 18 February 2018 at 15:23:14 UTC, Cym13 wrote:
>> On Sunday, 18 February 2018 at 14:48:59 UTC, Cym13 wrote:
>>> [...]
>>
>> Just thought of a much better/simpler solution for that last
>> case that also doesn't force you to read all data (which might
>> be impossible when dealing with infinite ranges):
>>
>> import std.range;
>> import std.algorithm;
>>
>> a[]
>> .enumerate // get tuples (index,
>> value)
>> .filter!(t => t[1] == "Test2") // keep only if value ==
>> "Test2"
>> .map!(t => t[0]) // keep only the index
>> part
>> .writeln;
>>
>> Completely lazy.
>
> How does one detect an operation as lazy or not? Is the some
> compile-time or runtime check for that?
>
> My guess is by referring to the docs function signature.
While it's not a replacement for checking the code manually,
@nogc helps a lot:
---
void main() @nogc
{
import std.container, std.stdio;
auto a = Array!string("Test1", "Test2", "Test3", "Test1",
"Test2");
import std.algorithm, std.range;
auto r = a[].enumerate.filter!(t => t[1] == "Test2").map!(t
=> t[0]);
debug r.writeln; // std.stdio.writeln allocates at the moment
}
---
https://run.dlang.io/is/Fo32sN
More information about the Digitalmars-d-learn
mailing list