// going from F# to Rust. open System; open System.Collections.Generic; // aliasing some F# types type Vec<'T> = ResizeArray<'T> // destructible list, with .Add method type HashMap<'K,'T> = Dictionary<'K,'T> // maps name to set of names x -> {y,z} means y and z are friends of x let friends = HashMap>(); // add friend set for "mary": friends.Add("mary",HashSet()); friends.Add("Nev Erstudy",HashSet()); for x in ["larz";"narx"] do friends.["mary"].Add(x) |> ignore; for x in ["Haten Umbers";"Isa Taparty";"P. Artyan Imal"] do friends.["Nev Erstudy"].Add(x) |> ignore; //friends.["Nev Erstudy"].Add("mary") |> ignore; // will affect entire closure // make "friends" commutative: if x is friend of y then y is friend of x for x in friends.Keys do for f in friends.[x] do if not(friends.ContainsKey(f)) then friends.Add(f,HashSet()); friends.[f].Add(x) |> ignore; // make "friends" associative: if y is friend of x and z is friend of y // then z is friend of x. This requires writing a "Floyd-Warshall" closure // function that keeps running as long as changes are being made. let friends_closure() = let mutable changes = true; // indicates that changes where made while changes do changes <- false for x in friends.Keys do for y in friends.[x] do for z in friends.[y] do changes <- friends.[x].Add(z) || changes //.Add returns false if duplicate already exists // while changes //friends_closure(); //calls function // But make "friends" anti-reflexive: remove x from friends.[x]: for x in friends.Keys do friends.[x].Remove(x) |> ignore // print all friends for x in friends.Keys do for f in friends.[x] do printfn "%s is friends with %s" x f;;