|
|
|
|
|
by chubot
4114 days ago
|
|
Right, I was referring to all ML-based languages -- so SML, OCaml, Haskell, F#, and possibly even Rust. Do you know how this would look in Haskell? # Return K most common lines in a file
def top_k(f, k):
counts = collections.defaultdict(int)
for line in f:
counts[line] += 1
return sorted(counts.items(), key=lambda x: x[1], reverse=True))[:k]
I was actually looking for the OCaml example which does this. I think it was in "Real World OCaml", and I remember it being horribly ugly compared to Python. I couldn't find it though. |
|