Skip to content

Commit

Permalink
Add missing iter functions to Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Sep 17, 2024
1 parent 941e4b4 commit 960f3c2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/FSharpPlus/Extensions/Dict.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ module Dict =
/// <returns>A seq of the values in the dictionary.</returns>
let values (source: IDictionary<_, _>) = Seq.map (fun (KeyValue(_, v)) -> v) source

/// <summary>Applies the given function to each key and value pair of the dictionary.</summary>
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iter action (source: IDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v

/// <summary>Applies the given function to each value of the dictionary.</summary>
/// <param name="action">The function to apply to each value of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iterValues action (source: IDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v

/// <summary>Maps the given function over each value in the dictionary.</summary>
/// <param name="mapper">The mapping function.</param>
/// <param name="source">The input dictionary.</param>
Expand Down
10 changes: 10 additions & 0 deletions src/FSharpPlus/Extensions/Dictionary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ module Dictionary =
/// <returns>A seq of the values in the dictionary.</returns>
let values (source: Dictionary<_,_>) = Seq.map (fun (KeyValue(_, v)) -> v) source

/// <summary>Applies the given function to each key and value pair of the dictionary.</summary>
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iter action (source: Dictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v

/// <summary>Applies the given function to each value of the dictionary.</summary>
/// <param name="action">The function to apply to each value of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iterValues action (source: Dictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v

/// <summary>Maps the given function over each value in the dictionary.</summary>
/// <param name="mapping">The mapping function.</param>
/// <param name="source">The input dictionary.</param>
Expand Down
7 changes: 6 additions & 1 deletion src/FSharpPlus/Extensions/IReadOnlyCollection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>Applies the given function to each element of the collection.</summary>
/// <param name="action">The function to apply to elements from the input collection.</param>
/// <param name="source">The input collection.</param>
let iter action (source: IReadOnlyCollection<'T>) = Seq.iter action source

let isEmpty (source: IReadOnlyCollection<'T>) = source.Count = 0
17 changes: 10 additions & 7 deletions src/FSharpPlus/Extensions/IReadOnlyDictionary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ module IReadOnlyDictionary =
/// <returns>A seq of the values in the read-only dictionary.</returns>
let values (source: IReadOnlyDictionary<'Key, 'Value>) = Seq.map (fun (KeyValue(_, v)) -> v) source

/// <summary>Applies the given function to each key and value pair of the read-only dictionary.</summary>
/// <param name="action">The function to apply to each key and value pair of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iter action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(k, v) in source do action k v

/// <summary>Applies the given function to each value of the read-only dictionary.</summary>
/// <param name="action">The function to apply to each value of the input dictionary.</param>
/// <param name="source">The input dictionary.</param>
let iterValues action (source: IReadOnlyDictionary<'Key, 'T>) = for KeyValue(_, v) in source do action v

/// <summary>Maps the given function over each value in the read-only dictionary.</summary>
/// <param name="mapper">The mapping function.</param>
/// <param name="source">The input IReadOnlyDictionary.</param>
Expand Down Expand Up @@ -106,13 +116,6 @@ module IReadOnlyDictionary =
dct.Add (k, mapper k v)
dct :> IReadOnlyDictionary<'Key, 'U>

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


/// <summary>Applies a function to each value in a read-only dictionary and then returns
/// a read-only dictionary of entries <c>v</c> where the applied function returned <c>Some(v)</c>.
Expand Down
5 changes: 4 additions & 1 deletion src/FSharpPlus/Extensions/IReadOnlyList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <summary>Applies the given function to each element of the collection.</summary>
/// <param name="action">The function to apply to elements from the input list.</param>
/// <param name="source">The input list.</param>
let iter action (source: IReadOnlyList<'T>) = Seq.iter action source
2 changes: 2 additions & 0 deletions src/FSharpPlus/Extensions/Map.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module Map =
/// <returns>The mapped Map.</returns>
let mapValues f (x: Map<'Key, 'T>) = Map.map (fun _ -> f) x

let iterValues action (source: Map<'Key, 'T>) = Map.iter (fun _ v -> action v) source

/// <summary>Maps values of two Maps.</summary>
/// <remarks>Keys that are not present on both Maps are dropped.</remarks>
/// <param name="f">The mapping function.</param>
Expand Down
8 changes: 8 additions & 0 deletions src/FSharpPlus/Extensions/ResizeArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ module ResizeArray =

ResizeArray (Seq.map mapping source)

/// <summary>Applies the given function to each element of the collection.</summary>
/// <param name="action">The function to apply to elements from the input ResizeArray.</param>
/// <param name="source">The input ResizeArray.</param>
let iter (action: 'T -> 'U) (source: ResizeArray<'T>) =
raiseIfNull (nameof source) source

ResizeArray (Seq.map action source)

/// <summary>Applies a ResizeArray of functions to a ResizeArray of values and concatenates them.</summary>
/// <param name="f">The functions.</param>
/// <param name="ra">The values.</param>
Expand Down

0 comments on commit 960f3c2

Please sign in to comment.