countUntil to print all the index of a given string.
Vino
vino.bheeman at hotmail.com
Sat Feb 24 16:41:40 UTC 2018
On Tuesday, 20 February 2018 at 08:50:27 UTC, Jonathan M Davis
wrote:
> On Tuesday, February 20, 2018 08:44:37 aberba via
> Digitalmars-d-learn 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.
>
> You have to read the docs or read the source code, though in
> general, functions that return a range type that wraps the
> original range tend to be lazy, whereas if a function returns
> the original range type or an array, then it's clearly not lazy.
>
> - Jonathan M Davis
Hi All,
Thank you very much, the provide solution work's for the given
example, so how can we achieve the same for the below code
import std.stdio;
import std.container;
import std.algorithm;
void main () {
auto a = Array!string("Test1", "Test2", "Test3", "Test1",
"Test2");
auto b = Array!string("Test1", "Test2", "Test3");
foreach(i; b[]) {
writeln(SList!int(a[].countUntil(i))[]); }
}
Output
[0]
[1]
[2]
Required
[0,3]
[1,4]
[2]
From,
Vino.B
More information about the Digitalmars-d-learn
mailing list