Skip to content

Instantly share code, notes, and snippets.

@flq
Last active December 16, 2016 13:15
Show Gist options
  • Save flq/fcacdea1eaaedd90c86125bba3a54640 to your computer and use it in GitHub Desktop.
Save flq/fcacdea1eaaedd90c86125bba3a54640 to your computer and use it in GitHub Desktop.
Bitemporal code samples
module BitempTests
type TimePoint<'S> = {
recorded : int;
actual : int;
state : 'S;
}
type HistoryEntry<'S> = {
time : int;
state : 'S;
}
type History<'S> = HistoryEntry<'S> list
let chain1 = [
{ recorded = 1; actual = 1; state = "First" };
{ recorded = 2; actual = 5; state = "Second" };
{ recorded = 3; actual = 7; state = "Third" };
]
let chain2 = [
{ recorded = 1; actual = 1; state = "First" };
{ recorded = 2; actual = 7; state = "Second" };
{ recorded = 3; actual = 5; state = "Third" };
]
let (|MustBeConsidered|CanBeIgnored|) (reference, newTime) =
if (newTime.actual <= reference.time) then MustBeConsidered else CanBeIgnored
let foldHistory<'S> history timePoint =
let newPoint = fun () -> { time = timePoint.actual; state = timePoint.state }
match history with
| [] -> newPoint() :: history
| hstEntry::rest -> match (hstEntry, timePoint) with
| MustBeConsidered -> newPoint() :: history
| CanBeIgnored -> history
[<EntryPoint>]
let main argv =
let recorded = int argv.[0]
let history =
chain2
|> List.sortByDescending (fun tp -> tp.recorded)
|> List.skipWhile (fun tp -> tp.recorded > recorded)
|> List.fold foldHistory []
|> List.iter (printfn "%A")
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment