Skip to content

Commit 3e7a328

Browse files
committed
+ map3Shortest and zip3shortest to Array and ResizeArray
1 parent 5bbec2f commit 3e7a328

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/FSharpPlus/Extensions/Array.fs

+22
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ module Array =
238238

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

241+
/// <summary>Safely build a new array whose elements are the results of applying the given function
242+
/// to each of the elements of the three arrays pairwise.</summary>
243+
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
244+
let map3Shortest f (a1: 'T1 []) (a2: 'T2 []) (a3: 'T3 []) =
245+
raiseIfNull (nameof a1) a1
246+
raiseIfNull (nameof a2) a2
247+
raiseIfNull (nameof a3) a3
248+
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> f a1.[i] a2.[i] a3.[i])
249+
241250
/// <summary>
242251
/// Zip safely two arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
243252
/// </summary>
@@ -250,6 +259,19 @@ module Array =
250259

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

262+
/// <summary>
263+
/// Zip safely three arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
264+
/// </summary>
265+
/// <param name="a1">First input array.</param>
266+
/// <param name="a2">Second input array.</param>
267+
/// <param name="a3">Third input array.</param>
268+
/// <returns>Array with corresponding tuple of input arrays.</returns>
269+
let zip3Shortest (a1: array<'T1>) (a2: array<'T2>) (a3: array<'T3>) =
270+
raiseIfNull (nameof a1) a1
271+
raiseIfNull (nameof a2) a2
272+
raiseIfNull (nameof a3) a3
273+
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> a1.[i], a2.[i], a3.[i])
274+
253275
/// <summary>Same as choose but with access to the index.</summary>
254276
/// <param name="mapping">The mapping function, taking index and element as parameters.</param>
255277
/// <param name="source">The input array.</param>

src/FSharpPlus/Extensions/ResizeArray.fs

+24
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ module ResizeArray =
134134
ra.Add (f a1.[i] a2.[i])
135135
ra
136136

137+
/// <summary>Safely build a new ResizeArray whose elements are the results of applying the given function
138+
/// to each of the elements of the three ResizeArrays pairwise.</summary>
139+
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
140+
let map3Shortest f (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) (a3: ResizeArray<'T3>) =
141+
let len = min a1.Count a2.Count |> min a3.Count
142+
let ra = ResizeArray len
143+
for i in 0..(len-1) do
144+
ra.Add (f a1.[i] a2.[i] a3.[i])
145+
ra
146+
137147
/// <summary>
138148
/// Zip safely two ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
139149
/// </summary>
@@ -146,3 +156,17 @@ module ResizeArray =
146156
for i in 0..(len-1) do
147157
ra.Add (a1.[i], a2.[i])
148158
ra
159+
160+
/// <summary>
161+
/// Zip safely three ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
162+
/// </summary>
163+
/// <param name="a1">First input ResizeArray.</param>
164+
/// <param name="a2">Second input ResizeArray.</param>
165+
/// <param name="a3">Third input ResizeArray.</param>
166+
/// <returns>ResizeArray with corresponding pairs of input ResizeArrays.</returns>
167+
let zip3Shortest (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) (a3: ResizeArray<'T3>) =
168+
let len = min a1.Count a2.Count |> min a3.Count
169+
let ra = ResizeArray len
170+
for i in 0..(len-1) do
171+
ra.Add (a1.[i], a2.[i], a3.[i])
172+
ra

0 commit comments

Comments
 (0)