I have this C # example that will create a dictionary of odd and even numbers from an array of integers:
int[] values = new int[] { 1, 2, 3, 5, 7 }; Dictionary<int, string> dictionary = values.ToDictionary(key => key, val => (val % 2 == 1) ? "Odd" : "Even"); // Display all keys and values. foreach (KeyValuePair<int, string> pair in dictionary) { Console.WriteLine(pair); }
Output:
[1, Odd] [2, Even] [3, Odd] [5, Odd] [7, Odd]
I want to do this similarly in F # using ToDictionary, and so far this:
let values = [| 1; 2; 3; 5; 7 |] let dictionary = values.ToDictionary( ??? ) // Display all keys and values. for d in dictionary do printfn "%d %s" d.Key d.Value
But how do I write the logic inside a ToDictionary?
source share