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 2aeab30
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/FSharpPlus/Control/Functor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
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
5 changes: 5 additions & 0 deletions src/FSharpPlus/Extensions/Map.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module Map =
/// </remarks>
let values (source: Map<'Key, 'T>) = Seq.map (fun (KeyValue(_, v)) -> v) source

/// <summary>Applies the given function to each value of the Map.</summary>
/// <param name="action">The function to apply to each value of the input Map.</param>
/// <param name="source">The input Map.</param>
let iterValues action (source: Map<'Key, 'T>) = Map.iter (fun _ v -> action v) source

/// <summary>Maps the values of the original Map.</summary>
/// <remarks>
/// The core `Map.map` function maps over values too, but it passes both
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 2aeab30

Please sign in to comment.