Skip to content

Commit 2aeab30

Browse files
committed
Add missing iter functions to Extensions
1 parent 941e4b4 commit 2aeab30

File tree

8 files changed

+58
-14
lines changed

8 files changed

+58
-14
lines changed

src/FSharpPlus/Control/Functor.fs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ open FSharpPlus.Data
2121
type Iterate =
2222
static member Iterate (x: Lazy<'T> , action) = action x.Value : unit
2323
static member Iterate (x: seq<'T> , action) = Seq.iter action x
24-
static member Iterate (x: option<'T> , action) = match x with Some x -> action x | _ -> ()
25-
static member Iterate (x: voption<'T>, action) = match x with ValueSome x -> action x | _ -> ()
24+
static member Iterate (x: option<'T> , action) = Option.iter action x
25+
static member Iterate (x: voption<'T>, action) = ValueOption.iter action x
2626
static member Iterate (x: list<'T> , action) = List.iter action x
2727
static member Iterate ((_: 'W, a: 'T), action) = action a :unit
2828
static member Iterate (x: 'T [] , action) = Array.iter action x
@@ -41,12 +41,12 @@ type Iterate =
4141
#else
4242
static member Iterate (x: Async<'T> , action: 'T -> unit) = (x |> Async.map action |> Async.AsTask).Wait ()
4343
#endif
44-
static member Iterate (x: Result<'T, 'E> , action) = match x with Ok x -> action x | _ -> ()
44+
static member Iterate (x: Result<'T, 'E> , action) = Result.iter action x
4545
static member Iterate (x: Choice<'T, 'E> , action) = match x with Choice1Of2 x -> action x | _ -> ()
4646
static member Iterate (KeyValue(_: 'Key, x: 'T), action) = action x : unit
4747
static member Iterate (x: Map<'Key,'T> , action) = Map.iter (const' action) x
48-
static member Iterate (x: Dictionary<'Key, 'T> , action) = Seq.iter action x.Values
49-
static member Iterate (x: _ ResizeArray , action) = Seq.iter action x
48+
static member Iterate (x: Dictionary<'Key, 'T> , action) = Dictionary.iterValues action x
49+
static member Iterate (x: _ ResizeArray , action) = ResizeArray.iter action x
5050

5151
// Restricted
5252
static member Iterate (x:string , action) = String.iter action x

src/FSharpPlus/Extensions/Dict.fs

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ module Dict =
4646
/// <returns>A seq of the values in the dictionary.</returns>
4747
let values (source: IDictionary<_, _>) = Seq.map (fun (KeyValue(_, v)) -> v) source
4848

49+
/// <summary>Applies the given function to each key and value pair of the dictionary.</summary>
50+
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
51+
/// <param name="source">The input dictionary.</param>
52+
let iter action (source: IDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v
53+
54+
/// <summary>Applies the given function to each value of the dictionary.</summary>
55+
/// <param name="action">The function to apply to each value of the input dictionary.</param>
56+
/// <param name="source">The input dictionary.</param>
57+
let iterValues action (source: IDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v
58+
4959
/// <summary>Maps the given function over each value in the dictionary.</summary>
5060
/// <param name="mapper">The mapping function.</param>
5161
/// <param name="source">The input dictionary.</param>

src/FSharpPlus/Extensions/Dictionary.fs

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ module Dictionary =
4848
/// <returns>A seq of the values in the dictionary.</returns>
4949
let values (source: Dictionary<_,_>) = Seq.map (fun (KeyValue(_, v)) -> v) source
5050

51+
/// <summary>Applies the given function to each key and value pair of the dictionary.</summary>
52+
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
53+
/// <param name="source">The input dictionary.</param>
54+
let iter action (source: Dictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v
55+
56+
/// <summary>Applies the given function to each value of the dictionary.</summary>
57+
/// <param name="action">The function to apply to each value of the input dictionary.</param>
58+
/// <param name="source">The input dictionary.</param>
59+
let iterValues action (source: Dictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v
60+
5161
/// <summary>Maps the given function over each value in the dictionary.</summary>
5262
/// <param name="mapping">The mapping function.</param>
5363
/// <param name="source">The input dictionary.</param>

src/FSharpPlus/Extensions/IReadOnlyCollection.fs

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ module IReadOnlyCollection =
1111
let ofList (source: 'T list) = source :> IReadOnlyCollection<'T>
1212
let ofSeq (source: seq<'T>) = source |> Array.ofSeq :> IReadOnlyCollection<'T>
1313
let map mapping (source: IReadOnlyCollection<'T>) = Seq.map mapping source |> Seq.toArray :> IReadOnlyCollection<'U>
14-
let iter mapping (source: IReadOnlyCollection<'T>) = Seq.iter mapping source
14+
15+
/// <summary>Applies the given function to each element of the collection.</summary>
16+
/// <param name="action">The function to apply to elements from the input collection.</param>
17+
/// <param name="source">The input collection.</param>
18+
let iter action (source: IReadOnlyCollection<'T>) = Seq.iter action source
19+
1520
let isEmpty (source: IReadOnlyCollection<'T>) = source.Count = 0

src/FSharpPlus/Extensions/IReadOnlyDictionary.fs

+10-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ module IReadOnlyDictionary =
4949
/// <returns>A seq of the values in the read-only dictionary.</returns>
5050
let values (source: IReadOnlyDictionary<'Key, 'Value>) = Seq.map (fun (KeyValue(_, v)) -> v) source
5151

52+
/// <summary>Applies the given function to each key and value pair of the read-only dictionary.</summary>
53+
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
54+
/// <param name="source">The input dictionary.</param>
55+
let iter action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v
56+
57+
/// <summary>Applies the given function to each value of the read-only dictionary.</summary>
58+
/// <param name="action">The function to apply to each value of the input dictionary.</param>
59+
/// <param name="source">The input dictionary.</param>
60+
let iterValues action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v
61+
5262
/// <summary>Maps the given function over each value in the read-only dictionary.</summary>
5363
/// <param name="mapper">The mapping function.</param>
5464
/// <param name="source">The input IReadOnlyDictionary.</param>
@@ -106,13 +116,6 @@ module IReadOnlyDictionary =
106116
dct.Add (k, mapper k v)
107117
dct :> IReadOnlyDictionary<'Key, 'U>
108118

109-
/// <summary>Applies the given action over each key and value in the read-only dictionary.</summary>
110-
/// <param name="action">The action to apply.</param>
111-
/// <param name="source">The input IReadOnlyDictionary.</param>
112-
///
113-
/// <returns>The mapped IReadOnlyDictionary.</returns>
114-
let iter action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v
115-
116119

117120
/// <summary>Applies a function to each value in a read-only dictionary and then returns
118121
/// a read-only dictionary of entries <c>v</c> where the applied function returned <c>Some(v)</c>.

src/FSharpPlus/Extensions/IReadOnlyList.fs

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ module IReadOnlyList =
3232
if 0 <= i && i < source.Count then Some source.[i]
3333
else None
3434

35-
let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source
35+
/// <summary>Applies the given function to each element of the collection.</summary>
36+
/// <param name="action">The function to apply to elements from the input list.</param>
37+
/// <param name="source">The input list.</param>
38+
let iter action (source: IReadOnlyList<'T>) = Seq.iter action source

src/FSharpPlus/Extensions/Map.fs

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ module Map =
3333
/// </remarks>
3434
let values (source: Map<'Key, 'T>) = Seq.map (fun (KeyValue(_, v)) -> v) source
3535

36+
/// <summary>Applies the given function to each value of the Map.</summary>
37+
/// <param name="action">The function to apply to each value of the input Map.</param>
38+
/// <param name="source">The input Map.</param>
39+
let iterValues action (source: Map<'Key, 'T>) = Map.iter (fun _ v -> action v) source
40+
3641
/// <summary>Maps the values of the original Map.</summary>
3742
/// <remarks>
3843
/// The core `Map.map` function maps over values too, but it passes both

src/FSharpPlus/Extensions/ResizeArray.fs

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ module ResizeArray =
2121

2222
ResizeArray (Seq.map mapping source)
2323

24+
/// <summary>Applies the given function to each element of the collection.</summary>
25+
/// <param name="action">The function to apply to elements from the input ResizeArray.</param>
26+
/// <param name="source">The input ResizeArray.</param>
27+
let iter (action: 'T -> 'U) (source: ResizeArray<'T>) =
28+
raiseIfNull (nameof source) source
29+
30+
ResizeArray (Seq.map action source)
31+
2432
/// <summary>Applies a ResizeArray of functions to a ResizeArray of values and concatenates them.</summary>
2533
/// <param name="f">The functions.</param>
2634
/// <param name="ra">The values.</param>

0 commit comments

Comments
 (0)