From 2aeab302506992e560c30cc950d8b50352f08c69 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 17 Sep 2024 19:28:00 +0200 Subject: [PATCH] Add missing iter functions to Extensions --- src/FSharpPlus/Control/Functor.fs | 10 +++++----- src/FSharpPlus/Extensions/Dict.fs | 10 ++++++++++ src/FSharpPlus/Extensions/Dictionary.fs | 10 ++++++++++ .../Extensions/IReadOnlyCollection.fs | 7 ++++++- .../Extensions/IReadOnlyDictionary.fs | 17 ++++++++++------- src/FSharpPlus/Extensions/IReadOnlyList.fs | 5 ++++- src/FSharpPlus/Extensions/Map.fs | 5 +++++ src/FSharpPlus/Extensions/ResizeArray.fs | 8 ++++++++ 8 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/FSharpPlus/Control/Functor.fs b/src/FSharpPlus/Control/Functor.fs index 545a66b45..70609fb6b 100644 --- a/src/FSharpPlus/Control/Functor.fs +++ b/src/FSharpPlus/Control/Functor.fs @@ -21,8 +21,8 @@ open FSharpPlus.Data type Iterate = static member Iterate (x: Lazy<'T> , action) = action x.Value : unit static member Iterate (x: seq<'T> , action) = Seq.iter action x - static member Iterate (x: option<'T> , action) = match x with Some x -> action x | _ -> () - static member Iterate (x: voption<'T>, action) = match x with ValueSome x -> action x | _ -> () + static member Iterate (x: option<'T> , action) = Option.iter action x + static member Iterate (x: voption<'T>, action) = ValueOption.iter action x static member Iterate (x: list<'T> , action) = List.iter action x static member Iterate ((_: 'W, a: 'T), action) = action a :unit static member Iterate (x: 'T [] , action) = Array.iter action x @@ -41,12 +41,12 @@ type Iterate = #else static member Iterate (x: Async<'T> , action: 'T -> unit) = (x |> Async.map action |> Async.AsTask).Wait () #endif - static member Iterate (x: Result<'T, 'E> , action) = match x with Ok x -> action x | _ -> () + static member Iterate (x: Result<'T, 'E> , action) = Result.iter action x static member Iterate (x: Choice<'T, 'E> , action) = match x with Choice1Of2 x -> action x | _ -> () static member Iterate (KeyValue(_: 'Key, x: 'T), action) = action x : unit static member Iterate (x: Map<'Key,'T> , action) = Map.iter (const' action) x - static member Iterate (x: Dictionary<'Key, 'T> , action) = Seq.iter action x.Values - static member Iterate (x: _ ResizeArray , action) = Seq.iter action x + static member Iterate (x: Dictionary<'Key, 'T> , action) = Dictionary.iterValues action x + static member Iterate (x: _ ResizeArray , action) = ResizeArray.iter action x // Restricted static member Iterate (x:string , action) = String.iter action x diff --git a/src/FSharpPlus/Extensions/Dict.fs b/src/FSharpPlus/Extensions/Dict.fs index e54743052..8d0532f47 100644 --- a/src/FSharpPlus/Extensions/Dict.fs +++ b/src/FSharpPlus/Extensions/Dict.fs @@ -46,6 +46,16 @@ module Dict = /// A seq of the values in the dictionary. let values (source: IDictionary<_, _>) = Seq.map (fun (KeyValue(_, v)) -> v) source + /// Applies the given function to each key and value pair of the dictionary. + /// The function to apply to each key and value pair of the input dictionary. + /// The input dictionary. + let iter action (source: IDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v + + /// Applies the given function to each value of the dictionary. + /// The function to apply to each value of the input dictionary. + /// The input dictionary. + let iterValues action (source: IDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v + /// Maps the given function over each value in the dictionary. /// The mapping function. /// The input dictionary. diff --git a/src/FSharpPlus/Extensions/Dictionary.fs b/src/FSharpPlus/Extensions/Dictionary.fs index c569cf466..3030f0783 100644 --- a/src/FSharpPlus/Extensions/Dictionary.fs +++ b/src/FSharpPlus/Extensions/Dictionary.fs @@ -48,6 +48,16 @@ module Dictionary = /// A seq of the values in the dictionary. let values (source: Dictionary<_,_>) = Seq.map (fun (KeyValue(_, v)) -> v) source + /// Applies the given function to each key and value pair of the dictionary. + /// The function to apply to each key and value pair of the input dictionary. + /// The input dictionary. + let iter action (source: Dictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v + + /// Applies the given function to each value of the dictionary. + /// The function to apply to each value of the input dictionary. + /// The input dictionary. + let iterValues action (source: Dictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v + /// Maps the given function over each value in the dictionary. /// The mapping function. /// The input dictionary. diff --git a/src/FSharpPlus/Extensions/IReadOnlyCollection.fs b/src/FSharpPlus/Extensions/IReadOnlyCollection.fs index 1a3b5626a..047c4f048 100644 --- a/src/FSharpPlus/Extensions/IReadOnlyCollection.fs +++ b/src/FSharpPlus/Extensions/IReadOnlyCollection.fs @@ -11,5 +11,10 @@ module IReadOnlyCollection = let ofList (source: 'T list) = source :> IReadOnlyCollection<'T> let ofSeq (source: seq<'T>) = source |> Array.ofSeq :> IReadOnlyCollection<'T> let map mapping (source: IReadOnlyCollection<'T>) = Seq.map mapping source |> Seq.toArray :> IReadOnlyCollection<'U> - let iter mapping (source: IReadOnlyCollection<'T>) = Seq.iter mapping source + + /// Applies the given function to each element of the collection. + /// The function to apply to elements from the input collection. + /// The input collection. + let iter action (source: IReadOnlyCollection<'T>) = Seq.iter action source + let isEmpty (source: IReadOnlyCollection<'T>) = source.Count = 0 diff --git a/src/FSharpPlus/Extensions/IReadOnlyDictionary.fs b/src/FSharpPlus/Extensions/IReadOnlyDictionary.fs index 9414c0c54..20b07caee 100644 --- a/src/FSharpPlus/Extensions/IReadOnlyDictionary.fs +++ b/src/FSharpPlus/Extensions/IReadOnlyDictionary.fs @@ -49,6 +49,16 @@ module IReadOnlyDictionary = /// A seq of the values in the read-only dictionary. let values (source: IReadOnlyDictionary<'Key, 'Value>) = Seq.map (fun (KeyValue(_, v)) -> v) source + /// Applies the given function to each key and value pair of the read-only dictionary. + /// The function to apply to each key and value pair of the input dictionary. + /// The input dictionary. + let iter action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v + + /// Applies the given function to each value of the read-only dictionary. + /// The function to apply to each value of the input dictionary. + /// The input dictionary. + let iterValues action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v + /// Maps the given function over each value in the read-only dictionary. /// The mapping function. /// The input IReadOnlyDictionary. @@ -106,13 +116,6 @@ module IReadOnlyDictionary = dct.Add (k, mapper k v) dct :> IReadOnlyDictionary<'Key, 'U> - /// Applies the given action over each key and value in the read-only dictionary. - /// The action to apply. - /// The input IReadOnlyDictionary. - /// - /// The mapped IReadOnlyDictionary. - let iter action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v - /// Applies a function to each value in a read-only dictionary and then returns /// a read-only dictionary of entries v where the applied function returned Some(v). diff --git a/src/FSharpPlus/Extensions/IReadOnlyList.fs b/src/FSharpPlus/Extensions/IReadOnlyList.fs index e4ad6c2c2..a9ae04840 100644 --- a/src/FSharpPlus/Extensions/IReadOnlyList.fs +++ b/src/FSharpPlus/Extensions/IReadOnlyList.fs @@ -32,4 +32,7 @@ module IReadOnlyList = if 0 <= i && i < source.Count then Some source.[i] else None - let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source \ No newline at end of file + /// Applies the given function to each element of the collection. + /// The function to apply to elements from the input list. + /// The input list. + let iter action (source: IReadOnlyList<'T>) = Seq.iter action source \ No newline at end of file diff --git a/src/FSharpPlus/Extensions/Map.fs b/src/FSharpPlus/Extensions/Map.fs index c6d241faf..d6781d45b 100644 --- a/src/FSharpPlus/Extensions/Map.fs +++ b/src/FSharpPlus/Extensions/Map.fs @@ -33,6 +33,11 @@ module Map = /// let values (source: Map<'Key, 'T>) = Seq.map (fun (KeyValue(_, v)) -> v) source + /// Applies the given function to each value of the Map. + /// The function to apply to each value of the input Map. + /// The input Map. + let iterValues action (source: Map<'Key, 'T>) = Map.iter (fun _ v -> action v) source + /// Maps the values of the original Map. /// /// The core `Map.map` function maps over values too, but it passes both diff --git a/src/FSharpPlus/Extensions/ResizeArray.fs b/src/FSharpPlus/Extensions/ResizeArray.fs index 425873748..f321b13e6 100644 --- a/src/FSharpPlus/Extensions/ResizeArray.fs +++ b/src/FSharpPlus/Extensions/ResizeArray.fs @@ -21,6 +21,14 @@ module ResizeArray = ResizeArray (Seq.map mapping source) + /// Applies the given function to each element of the collection. + /// The function to apply to elements from the input ResizeArray. + /// The input ResizeArray. + let iter (action: 'T -> 'U) (source: ResizeArray<'T>) = + raiseIfNull (nameof source) source + + ResizeArray (Seq.map action source) + /// Applies a ResizeArray of functions to a ResizeArray of values and concatenates them. /// The functions. /// The values.