3 ms·
That's really interesting and I like seeing what can be done in more "evolved" languages than awk. Awk has the advantage of being everywhere and not needing ins
by rakoo 3y ago
That's really interesting and I like seeing what can be done in more "evolved" languages than awk. Awk has the advantage of being everywhere and not needing installation phases, but that's a price you only pay once.
Your Kotlin code feels more imperative to me even though I'm used to that kind of things; I like that awk still feels more like describing stuff, but that's only me.
- mike_hearn 3y agoYou can write it more functionally very easily, something like (untested): Path("measurements.txt") .readLines() .groupBy({ it.substringBefore(';') }, { it.substringAfter(';').toDouble() }) .forEach { (name, temps) -> println("%s=%.1f/%.1f/%.1f".format(name, temps.min(), temps.average(), temps.max())) } I didn't do it that way because with one billion rows, it runs out of memory. The imperative version that mutates in place doesn't. You can probably write a functional version that also doesn't run out of memory using .groupingBy{}, because it all applies to lazy sequences. I just didn't bother working out how. The Java streams framework can do the same things and has the advantage that it can sometimes make it easy to parallelize.