@@ -77,15 +77,59 @@ module IReadOnlyDictionary =
7777 | None -> ()
7878 dct :> IReadOnlyDictionary< 'Key, 'U>
7979
80- /// <summary>Applies given function to each value of the given read-only dictionary.</summary>
81- /// <param name="f ">The mapping function.</param>
82- /// <param name="x ">The input IReadOnlyDictionary.</param>
80+ /// <summary>Maps the given function over each key and value in the read-only dictionary.</summary>
81+ /// <param name="mapper ">The mapping function.</param>
82+ /// <param name="source ">The input IReadOnlyDictionary.</param>
8383 ///
84- /// <returns>Returns IReadOnlyDictionary with values x for each dictionary value where the function returns Some(x) .</returns>
85- let chooseValues f ( x : IReadOnlyDictionary < 'Key , 'T >) =
84+ /// <returns>The mapped IReadOnlyDictionary .</returns>
85+ let mapi mapper ( source : IReadOnlyDictionary < 'Key , 'T >) =
8686 let dct = Dictionary< 'Key, 'U> ()
87- for KeyValue( k, v) in x do
88- match f v with
87+ for KeyValue( k, v) in source do
88+ dct.Add ( k, mapper k v)
89+ dct :> IReadOnlyDictionary< 'Key, 'U>
90+
91+ /// <summary>Applies the given action over each key and value in the read-only dictionary.</summary>
92+ /// <param name="action">The action to apply.</param>
93+ /// <param name="source">The input IReadOnlyDictionary.</param>
94+ ///
95+ /// <returns>The mapped IReadOnlyDictionary.</returns>
96+ let iter action ( source : IReadOnlyDictionary < 'Key , 'T >) = for KeyValue( k, v) in source do action k v
97+
98+
99+ /// <summary>Applies a function to each value in a read-only dictionary and then returns
100+ /// a read-only dictionary of entries <c>v</c> where the applied function returned <c>Some(v)</c>.
101+ ///
102+ /// Returns an empty read-only dictionary when the input read-only dictionary is empty or when the applied chooser function
103+ /// returns <c>None</c> for all elements.
104+ /// </summary>
105+ ///
106+ /// <param name="chooser">The function to be applied to the read-only dictionary values.</param>
107+ /// <param name="source">The input read-only dictionary.</param>
108+ ///
109+ /// <returns>The resulting read-only dictionary comprising the entries <c>v</c> where the chooser function returned <c>Some(x)</c>.</returns>
110+ let chooseValues chooser ( source : IReadOnlyDictionary < 'Key , 'T >) =
111+ let dct = Dictionary< 'Key, 'U> ()
112+ for KeyValue( k, v) in source do
113+ match chooser v with
114+ | Some v -> dct.Add ( k, v)
115+ | None -> ()
116+ dct :> IReadOnlyDictionary< 'Key, 'U>
117+
118+ /// <summary>Applies a function to each key and value in a read-only dictionary and then returns
119+ /// a read-only dictionary of entries <c>v</c> where the applied function returned <c>Some(v)</c>.
120+ ///
121+ /// Returns an empty read-only dictionary when the input read-only dictionary is empty or when the applied chooser function
122+ /// returns <c>None</c> for all elements.
123+ /// </summary>
124+ ///
125+ /// <param name="chooser">The function to be applied to the read-only dictionary values.</param>
126+ /// <param name="source">The input read-only dictionary.</param>
127+ ///
128+ /// <returns>The resulting read-only dictionary comprising the entries <c>v</c> where the chooser function returned <c>Some(x)</c>.</returns>
129+ let choose chooser ( source : IReadOnlyDictionary < 'Key , 'T >) =
130+ let dct = Dictionary< 'Key, 'U> ()
131+ for KeyValue( k, v) in source do
132+ match chooser k v with
89133 | Some v -> dct.Add ( k, v)
90134 | None -> ()
91135 dct :> IReadOnlyDictionary< 'Key, 'U>
@@ -152,4 +196,33 @@ module IReadOnlyDictionary =
152196 /// Returns the intersection of two read-only dictionaries, preferring values from the first in case of duplicate keys.
153197 let intersect ( source1 : IReadOnlyDictionary < 'Key , 'T >) ( source2 : IReadOnlyDictionary < 'Key , 'T >) =
154198 intersectWith ( fun a _ -> a) source1 source2
199+
155200 #endif
201+
202+ let empty < 'Key , 'U when 'Key : equality > = Dictionary< 'Key, 'U> () :> IReadOnlyDictionary<_,_>
203+
204+ /// <summary>Converts a read-only dictionary to a ResizeArray.</summary>
205+ /// <param name="source">The source IReadOnlyDictionary.</param>
206+ ///
207+ /// <returns>A ResizeArray containing the Key and Value of the original IReadOnlyDictionary.</returns>
208+ let toResizeArray ( source : IReadOnlyDictionary < 'Key , 'T >) =
209+ let arr = ResizeArray< KeyValuePair< 'Key, 'T>> ()
210+ for KeyValue( k, x) in source do
211+ arr.Add ( KeyValuePair ( k, x))
212+ arr
213+
214+ /// <summary>Converts a read-only dictionary to a sequence.</summary>
215+ /// <param name="source">The source IReadOnlyDictionary.</param>
216+ ///
217+ /// <returns>A sequence containing the Key and Value of the original IReadOnlyDictionary.</returns>
218+ let toSeq ( source : IReadOnlyDictionary < 'Key , 'T >) = toResizeArray source :> seq<_>
219+
220+ /// Folds over the bindings in the Dictionary
221+ let fold ( folder : 'State -> 'Key -> 'T -> 'State ) ( state : 'State ) ( source : IReadOnlyDictionary < 'Key , 'T >) =
222+ let unzip source = Seq.map fst source, Seq.map snd source
223+ source |> toSeq |> Seq.map (| KeyValue|) |> unzip ||> Seq.fold2 folder state
224+
225+ /// Folds over the bindings in the Dictionary
226+ let foldBack ( folder : 'Key -> 'T -> 'State -> 'State ) ( source : IReadOnlyDictionary < 'Key , 'T >) state =
227+ let unzip source = Seq.map fst source, Seq.map snd source
228+ source |> toSeq |> Seq.map (| KeyValue|) |> unzip ||> Seq.foldBack2 folder <| state
0 commit comments