Skip to content

Commit

Permalink
+ map3Shortest and zip3shortest to Array and ResizeArray
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Jan 13, 2024
1 parent 5bbec2f commit 3e7a328
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/FSharpPlus/Extensions/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ module Array =

Array.init (min a1.Length a2.Length) (fun i -> f a1.[i] a2.[i])

/// <summary>Safely build a new array whose elements are the results of applying the given function
/// to each of the elements of the three arrays pairwise.</summary>
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
let map3Shortest f (a1: 'T1 []) (a2: 'T2 []) (a3: 'T3 []) =
raiseIfNull (nameof a1) a1
raiseIfNull (nameof a2) a2
raiseIfNull (nameof a3) a3
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> f a1.[i] a2.[i] a3.[i])

/// <summary>
/// Zip safely two arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
/// </summary>
Expand All @@ -250,6 +259,19 @@ module Array =

Array.init (min a1.Length a2.Length) (fun i -> a1.[i], a2.[i])

/// <summary>
/// Zip safely three arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
/// </summary>
/// <param name="a1">First input array.</param>
/// <param name="a2">Second input array.</param>
/// <param name="a3">Third input array.</param>
/// <returns>Array with corresponding tuple of input arrays.</returns>
let zip3Shortest (a1: array<'T1>) (a2: array<'T2>) (a3: array<'T3>) =
raiseIfNull (nameof a1) a1
raiseIfNull (nameof a2) a2
raiseIfNull (nameof a3) a3
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> a1.[i], a2.[i], a3.[i])

/// <summary>Same as choose but with access to the index.</summary>
/// <param name="mapping">The mapping function, taking index and element as parameters.</param>
/// <param name="source">The input array.</param>
Expand Down
24 changes: 24 additions & 0 deletions src/FSharpPlus/Extensions/ResizeArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ module ResizeArray =
ra.Add (f a1.[i] a2.[i])
ra

/// <summary>Safely build a new ResizeArray whose elements are the results of applying the given function
/// to each of the elements of the three ResizeArrays pairwise.</summary>
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
let map3Shortest f (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) (a3: ResizeArray<'T3>) =
let len = min a1.Count a2.Count |> min a3.Count
let ra = ResizeArray len
for i in 0..(len-1) do
ra.Add (f a1.[i] a2.[i] a3.[i])
ra

/// <summary>
/// Zip safely two ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
/// </summary>
Expand All @@ -146,3 +156,17 @@ module ResizeArray =
for i in 0..(len-1) do
ra.Add (a1.[i], a2.[i])
ra

/// <summary>
/// Zip safely three ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
/// </summary>
/// <param name="a1">First input ResizeArray.</param>
/// <param name="a2">Second input ResizeArray.</param>
/// <param name="a3">Third input ResizeArray.</param>
/// <returns>ResizeArray with corresponding pairs of input ResizeArrays.</returns>
let zip3Shortest (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) (a3: ResizeArray<'T3>) =
let len = min a1.Count a2.Count |> min a3.Count
let ra = ResizeArray len
for i in 0..(len-1) do
ra.Add (a1.[i], a2.[i], a3.[i])
ra

0 comments on commit 3e7a328

Please sign in to comment.