Last active
August 15, 2017 22:54
-
-
Save aryszka/fd69465760f6951e8d51bff7505fa695 to your computer and use it in GitHub Desktop.
sort csv in mml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/mml | |
// sort csv by a column | |
import ( | |
csvparser "mml/parser/csv" | |
csvtemplate "mml/template/csv" | |
"mml/function" | |
"mml/lists" | |
"mml/log" | |
"mml/os" | |
"mml/sort" | |
"mml/values" | |
) | |
let columnIndex values.convertOrAbort( | |
values.stringToInt | |
"expected one number argument" | |
lists.atOrDefault("", 1, os.args) | |
) | |
fn compare (left, right) { | |
let ( | |
sleft lists.atOrDefault("", columnIndex, left) | |
sright lists.atOrDefault("", columnIndex, right) | |
nleft values.stringToNumberOrKeep(sleft) | |
nright values.stringToNumberOrKeep(sright) | |
) | |
match [nleft, nright] { | |
case [number, number]: | |
sort.compare(nleft, nright) | |
default: | |
sort.compare(sleft, sright) | |
} | |
} | |
os.stdin | |
-> csvparser.parse | |
-> function.ifNotError(sort.do(compare)) | |
-> function.ifNotError(csvtemplate.println) | |
-> log.ifError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment