Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You 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.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: