Last active
May 11, 2022 10:56
-
-
Save t1m0thyj/89c63f7c17da8b030f331c427fd2b1f1 to your computer and use it in GitHub Desktop.
Implementation of time interval problem in F# with error handling
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
// Coded online at https://repl.it/@t1m0thyj/F-Scratchpad-1 | |
open System | |
let dateStr = "1/1/2000,2/29/2000,2/29/2004,2/29/2008,2/29/2012,2/29/2016,3/14/15 9:26:53" | |
let getDayDiff (date1:string, date2:string) = | |
let couldParse, startDate = DateTime.TryParse date1 | |
if (not couldParse) then failwithf "Date '%s' is not valid" date1 | |
let couldParse, endDate = DateTime.TryParse date2 | |
if (not couldParse) then failwithf "Date '%s' is not valid" date2 | |
abs (endDate - startDate).Days | |
let timestamps = dateStr.Split([|','|]) | |
if (timestamps.Length < 2) then failwith "You must enter at least 2 dates" | |
let intervals = | |
timestamps | |
|> Seq.pairwise | |
|> Seq.map getDayDiff | |
Seq.iter (printfn "%A") intervals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome!