I agree, however it would perform better if there were too many arguments
(which is an edge case). Nonetheless I would love to know how to harness the
multicore capability inside a for loop in Nim.
For reference here's the program in D:
import std.stdio : writeln;
import std.algorithm : splitter;
import std.range : chain;
import std.path : dirSeparator, pathSeparator;
import std.process : environment;
import std.file : exists;
pragma(inline, true) public void which(in ref string bin) {
foreach (path; environment.get("PATH", "").splitter(pathSeparator)) {
auto probe = chain(path, dirSeparator, bin);
if (probe.exists()) {
writeln(probe);
break;
}
}
}
void main(in string[] args) {
if (args.length == 2) {
which(args[1]);
}
else {
version (par) {
import std.parallelism : parallel;
foreach (arg; args.parallel())
which(arg);
}
version (sng) {
foreach (arg; args)
which(arg);
}
}
}
Run
$ gdc source/app.d -fversion=par -O3
$ time ./a.out $(ls /usr/bin)
// ...
0.040s
$ gdc source/app.d -fversion=sng -O3
// ...
0.084s
$ time ./a.out $(ls /usr/bin)
// ...
Run