Skip to content

Instantly share code, notes, and snippets.

@Swoorup
Created December 13, 2020 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swoorup/c7451973ce64980d65246ef5e6ef263a to your computer and use it in GitHub Desktop.
Save Swoorup/c7451973ce64980d65246ef5e6ef263a to your computer and use it in GitHub Desktop.
type CompareResult<'a, 'b> =
| FoundPair of 'a * 'b
| UnmatchedLHS of 'a
| UnmatchedRHS of 'b
[<RequireQualifiedAccess>]
module ListCompare =
let compare xsKeySelector ysKeySelector xs ys =
let xs =
xs
|> Seq.toList
|> List.map (fun x -> x, xsKeySelector x)
|> List.sortBy snd
let ys =
ys
|> Seq.toList
|> List.map (fun y -> y, ysKeySelector y)
|> List.sortBy snd
(xs, ys)
|> List.unfold (function
| [], [] -> None
| (x, mx) :: xs, ((_, my) :: _ as ys) when mx < my -> Some(UnmatchedLHS x, (xs, ys))
| (x, _) :: xs, [] -> Some(UnmatchedLHS x, (xs, []))
| ((_, mx) :: _ as xs), (y, my) :: ys when my < mx -> Some(UnmatchedRHS y, (xs, ys))
| [], (y, _) :: ys -> Some(UnmatchedRHS y, ([], ys))
| (x, _) :: xs, (y, _) :: ys -> Some(FoundPair(x, y), (xs, ys)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment