Created
November 16, 2013 13:39
-
-
Save davidgrenier/7500271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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