Skip to content

Instantly share code, notes, and snippets.

@t1m0thyj
Last active May 11, 2022 10:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t1m0thyj/89c63f7c17da8b030f331c427fd2b1f1 to your computer and use it in GitHub Desktop.
Save t1m0thyj/89c63f7c17da8b030f331c427fd2b1f1 to your computer and use it in GitHub Desktop.
Implementation of time interval problem in F# with error handling
// 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
@Kazark
Copy link

Kazark commented Apr 11, 2018

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment