Why Phobos is cool
Jordan Wilson
wilsonjord at gmail.com
Wed Jul 8 03:53:55 UTC 2020
On Tuesday, 7 July 2020 at 23:38:26 UTC, Paul Backus wrote:
> On Monday, 6 July 2020 at 20:47:36 UTC, Jordan Wilson wrote:
>>
>> Just now I wanted to find the max value of a column in a text
>> file, my thinking went "Excel? It'll take ages to load...I'll
>> just google awk or something...hang on a minute, didn't I read
>> somewhere about D oneliners?"
>>
>> echo 'import std; void main() {
>> File("emu.sift","r").byLine.map!(a => a.splitter("
>> ").front.to!double).maxElement.writeln; }' | dmd -run -
>>
>> Thanks!
>>
>> Jordan
>
> IMO this still doesn't hold a candle to the awk version:
>
> awk '{ if ($1 > max) max = $1 } END { print max }'
>
> With better library functions, though, the D version could be
> much, much nicer. Something like:
>
> stdin.byRecord.map(r => r.fields[0]).maxElement
Oh I agree the awk version is nicer (as is your theoretical D
version). But I couldn't write that off the top of my head,
whereas the D version I could.
Here's something I just did now that might be more relevant:
echo 'import std; void main() {
"bets.json".readText.parseJSON.toPrettyString.writeln; }' | dmd
-run - | more
I did this without looking up anything. Whereas before this
echo/import std; into dmd -run trick, I would have opened up vim,
and hoped that my previous command to pretty print was still in
the history! (using python if I remember correctly).
I *think* that writing a more slightly more verbose D one-liner
without needing to reference anything might prove itself useful
to me in the long run, but we shall see.
Jordan
More information about the Digitalmars-d
mailing list