On Tuesday, 28 January 2020 at 14:01:35 UTC, Mike Parker wrote:
Robert Schadek was inspired by a post he saw on Hacker News a
while back showing an implementation of wc in Haskell totaling
80 lines. He decided he could do better in D. So he did. This
post on the D blog shows what he came up with and also provides
a brief introduction to ranges.
The blog:
https://dlang.org/blog/2020/01/28/wc-in-d-712-characters-without-a-single-branch/
Reddit:
https://www.reddit.com/r/programming/comments/ev5w91/wc_in_d_712_characters_without_a_single_branch/
It's also on Hacker News, but please don't post a direct link
to it if you find it there.
https://news.ycombinator.com/
Golfed down to 9 lines:
import std;
void main(string[] args)
{
args[1].File.byLine(Yes.keepTerminator)
.map!(l => [l.byCodePoint.walkLength,
l.splitter.walkLength])
.fold!((o, l) => [o[0]+1, o[1]+l[1], o[2]+l[0]])([0uL,
0, 0])
.writefln!"%(%u %) %s"(args[1]);
}
Sacrificed most of the readability of course, but I think it is
functionally equivalent.