Created
August 5, 2016 15:06
-
-
Save cobalamin/c2c609b8c98df414683bd2c2d5d50bf0 to your computer and use it in GitHub Desktop.
Parsing a stopwatch-like time string format in Elm
This file contains hidden or 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
| module ParseStopwatch exposing (..) | |
| import Parser exposing (..) | |
| import Parser.Char exposing (..) | |
| import Parser.Number exposing (digit) | |
| import Char | |
| import Time exposing (..) | |
| twoDigitInt : Parser Int | |
| twoDigitInt = | |
| (map (\tens ones -> tens * 10 + ones)) digit `and` digit | |
| threeDigitInt : Parser Int | |
| threeDigitInt = | |
| (map (\hundreds tens ones -> hundreds * 100 + tens * 10 + ones)) digit `and` digit `and` digit | |
| (**) x y = (toFloat x) * y | |
| sumTimes : Int -> Int -> Int -> Int -> Time | |
| sumTimes h m s ms = | |
| (h ** hour) + (m ** minute) + (s ** second) + (ms ** millisecond) | |
| parseTime : Parser Time | |
| parseTime = | |
| sumTimes | |
| `map` twoDigitInt | |
| <* symbol ':' | |
| `and` twoDigitInt | |
| <* symbol ':' | |
| `and` twoDigitInt | |
| <* symbol ',' | |
| `and` threeDigitInt | |
| -- parse parseTime "07:39:01,921" == Ok 27541921 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment