Skip to content

Commit

Permalink
Merge pull request #295 from krymtkts:feature/roman
Browse files Browse the repository at this point in the history
Add support for Roman numeral page ranges in booklogs
  • Loading branch information
krymtkts authored Dec 29, 2024
2 parents 26a348c + 9495274 commit 4f2fa70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions contents/booklogs/2024.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
- date: 2024-08-01
bookTitle: A book
readCount: 2
pages: 1 ~ 20
pages: i ~ xix, 1 ~ 20
notes: |
read again.
- date: 2024-09-01
Expand All @@ -73,7 +73,7 @@
pages: 81 ~ 99
- date: 2024-11-30
bookTitle: C book
pages: 1 ~ 12
pages: i ~ viii, 1 ~ 12
- date: 2024-12-01
bookTitle: C book
pages: 13 ~ 20
Expand Down
35 changes: 32 additions & 3 deletions src/Booklog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,41 @@ module Misc =
| true, int -> Some int
| _ -> None

let readPages (pages: string) =
pages.Split([| '~' |])
let private romanMap = Map [ ('i', 1); ('v', 5); ('x', 10) ]

let private toInt x =
x
|> Char.ToLower
|> romanMap.TryFind
|> function
| Some v -> v
| None -> 0

let private romanToArabic (roman: string) =
let roman = roman |> _.Trim()

roman
|> Seq.map toInt
|> Seq.pairwise
|> Seq.fold (fun acc (v1, v2) -> if v1 < v2 then acc - v1 else acc + v1) 0
|> (+) (Seq.last roman |> toInt)

let private (|Roman|_|) (str: string) =
match romanToArabic str with
| 0 -> None
| i -> Some i

let readPages (pages: string) =
pages
|> _.Split([| ',' |])
|> Seq.map _.Split([| '~' |])
|> Seq.map (function
| [| Int s; Int e |] -> e - s + 1
| [| Int _ |] -> 1
| _ -> 0
| [| Roman s; Roman e |] -> e - s + 1
| [| Roman _ |] -> 1
| _ -> 0)
|> Seq.sum

let private getMaxPagesRead (booklogs: Booklog list) =
booklogs |> List.map (_.pages >> readPages) |> List.max
Expand Down

0 comments on commit 4f2fa70

Please sign in to comment.