3 ms·
In F# you'd normally use a type provider for this kind of work, but for the sake of exercise: type Chapter = Map<int, string> type Book = Map<int, C
by piaste 10y ago
In F# you'd normally use a type provider for this kind of work, but for the sake of exercise:
type Chapter = Map<int, string>
type Book = Map<int, Chapter>
type Bible = Map<string, Book>
let bookOrder = [|"Genesis"; "Exodus"; "Leviticus"; (* etc.. *) |]
let sortChapter = Map.toSeq >> Seq.sortBy fst
>> Seq.toArray
let sortBook = Map.map (fun _ -> sortChapter)
>> Map.toSeq >> Seq.sortBy fst
>> Seq.toArray
let sortBible = Map.map (fun _ -> sortBook)
>> Map.toSeq >> Seq.sortBy (fun (bookname, _)-> Array.findIndex ((=) bookname) bookOrder)
>> Seq.toArray
BIBLE_TEXT
|> JsonConvert.DeserializeObject<Bible>
|> sortBible
|> printfn "%A"
FP 101 suggests you could extract the Map.toSeq >> Seq.sortby x >> Seq.toArray function, but I don't think it gains any readability, and in any case it's just an artifact of the problem requiring an array output even though a Map would make more sense.