Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created November 16, 2013 13:39
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 davidgrenier/7500271 to your computer and use it in GitHub Desktop.
Save davidgrenier/7500271 to your computer and use it in GitHub Desktop.
let (|Empty|_|) = function
| xs when Set.isEmpty xs -> Some ()
| _ -> None
[
"rule1", ["192"; "193"]
"rule2", ["193"; "194"]
"rule3", ["194"; "195"]
]
|> Seq.fold (fun data (rule, dst) ->
let dst = set dst
data
|> Map.toSeq
|> Seq.map (fun (dsts, rules) ->
let common = Set.intersect dsts dst
common, rules
)
|> Seq.sortBy (fun (common, _) -> -common.Count)
|> Seq.fold (fun (data, dst) (common, rules) ->
match dst with
| Empty -> data, dst
| _ -> Map.add common (rule :: rules) data, dst - common
) (data, dst)
|> function
| newData, Empty -> newData
| newData, rest -> newData.Add(rest, [rule])
) Map.empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment