LeetCode 981. Time Based Key-Value Store in F#

URL

leetcode.com/problems/time-based-key-value-..

Code

github.com/syohex/dotnet-study/blob/master/..

type TimeMap =
    { table: Map<string, Map<int, string>> }

    static member empty() : TimeMap = { table = Map.empty }

    static member set (key: string) (value: string) (timestamp: int) (tm: TimeMap) : TimeMap =
        match Map.tryFind key tm.table with
        | None -> { table = Map.add key (Map.add timestamp value Map.empty) tm.table }
        | Some (m) -> { table = Map.add key (Map.add timestamp value m) tm.table }

    static member get (key: string) (timestamp: int) (tm: TimeMap) : string =
        match Map.tryFind key tm.table with
        | None -> ""
        | Some (m) ->
            match Map.tryFind timestamp m with
            | Some (v) -> v
            | None ->
                m
                |> Map.fold
                    (fun (ret, max) k v ->
                        if k <= timestamp && k > max then
                            v, k
                        else
                            ret, max)
                    ("", System.Int32.MinValue)
                |> fst