4 ms·
Obviously, Scala is better than either of those languages. #!/usr/bin/env amm import $ivy.`com.typesafe.play::play-json:2.5.8` import java.nio.file
by paulddraper 10y ago
Obviously, Scala is better than either of those languages.
#!/usr/bin/env amm
import $ivy.`com.typesafe.play::play-json:2.5.8`
import java.nio.file._, play.api.libs.json._
val bookOrder = Seq(
"Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy",
"Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings",
"1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job",
"Psalms", "Proverbs", "Ecclesiastes", "Song of Solomon", "Isaiah",
"Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos",
"Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai",
"Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "Acts",
"Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians",
"Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians",
"1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James",
"1 Peter", "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation"
).zipWithIndex.toMap
val bible = Json.parse(Files.readAllBytes(Paths.get("ESV.json")))
.as[Map[String,Map[String,Map[String,String]]]]
val flattened = for((book, x) <- bible; (chapter, x) <- x; (verse, x) <- x)
yield (book, chapter.toInt, verse.toInt, x)
val result = flattened.toSeq.sortBy { case (b, c, v, _) => (bookOrder(b), c, v) }
println(result)
---
Also, Python
#!/usr/bin/env python3
import json
books = [
"Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy",
"Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings",
"1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job",
"Psalms", "Proverbs", "Ecclesiastes", "Song of Solomon", "Isaiah",
"Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos",
"Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai",
"Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "Acts",
"Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians",
"Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians",
"1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James",
"1 Peter", "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation"
]
books = {book:i for i, book in enumerate(books)}
with open('ESV.json') as f:
result = sorted(
((b, int(c), int(v), x) for b, x in json.load(f).items() for c, x in x.items() for v, x in x.items()),
key=lambda part: (books[part[0]], part[1:])
)
print(result)
---
Glad we've put that to rest.
- piaste 10y agoCode golf? Code golf! let o = [|"Genesis"; "Exodus"; "Leviticus"; "Numbers"; "Deuteronomy"; "Joshua"; "Judges"; "Ruth"; "1 Samuel"; "2 Samuel"; "1 Kings"; "2 Kings"; "1 Chronicles"; "2 Chronicles"; "Ezra"; "Nehemiah"; "Esther"; "Job"; "Psalms"; "Proverbs"; "Ecclesiastes"; "Song of Solomon"; "Isaiah"; "Jeremiah"; "Lamentations"; "Ezekiel"; "Daniel"; "Hosea"; "Joel"; "Amos"; "Obadiah"; "Jonah"; "Micah"; "Nahum"; "Habakkuk"; "Zephaniah"; "Haggai"; "Zechariah"; "Malachi"; "Matthew"; "Mark"; "Luke"; "John"; "Acts"; "Romans"; "1 Corinthians"; "2 Corinthians"; "Galatians"; "Ephesians"; "Philippians"; "Colossians"; "1 Thessalonians"; "2 Thessalonians"; "1 Timothy"; "2 Timothy"; "Titus"; "Philemon"; "Hebrews"; "James"; "1 Peter"; "2 Peter"; "1 John"; "2 John"; "3 John"; "Jude"; "Revelation"|] let a s = Map.toSeq >> Seq.sortBy s >> Seq.toArray JsonConvert.DeserializeObject<_> BIBLE_TEXT |> Map.map (fun _ -> Map.map (fun _ -> a fst) >> a fst) |> a (fun (n, _) -> Array.findIndex ((=) n) o) |> printfn "%A"
- paulddraper 10y agoF#, right?
- piaste 10y agoYup. 'Minified', for fun: let a s=Map.toSeq>>Seq.sortBy s>>Seq.toArray let m f=Map.map(fun _->f) printfn"%A"(a(fun(n, _)->Array.findIndex((=)n)o)(m(m(a fst)>>a fst)(JsonConvert.DeserializeObject<_>BIBLE_TEXT)))