From 3e7a328db725ae2958d4df050f37df315deaa311 Mon Sep 17 00:00:00 2001
From: gusty <1261319+gusty@users.noreply.github.com>
Date: Sat, 13 Jan 2024 07:29:13 +0100
Subject: [PATCH] + map3Shortest and zip3shortest to Array and ResizeArray
---
src/FSharpPlus/Extensions/Array.fs | 22 ++++++++++++++++++++++
src/FSharpPlus/Extensions/ResizeArray.fs | 24 ++++++++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/src/FSharpPlus/Extensions/Array.fs b/src/FSharpPlus/Extensions/Array.fs
index 26a13ab0a..5113cbd70 100644
--- a/src/FSharpPlus/Extensions/Array.fs
+++ b/src/FSharpPlus/Extensions/Array.fs
@@ -238,6 +238,15 @@ module Array =
Array.init (min a1.Length a2.Length) (fun i -> f a1.[i] a2.[i])
+ /// 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.
+ /// If one array is shorter, excess elements are discarded from the right end of the longer array.
+ 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])
+
///
/// Zip safely two arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
///
@@ -250,6 +259,19 @@ module Array =
Array.init (min a1.Length a2.Length) (fun i -> a1.[i], a2.[i])
+ ///
+ /// Zip safely three arrays. If one array is shorter, excess elements are discarded from the right end of the longer array.
+ ///
+ /// First input array.
+ /// Second input array.
+ /// Third input array.
+ /// Array with corresponding tuple of input arrays.
+ 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])
+
/// Same as choose but with access to the index.
/// The mapping function, taking index and element as parameters.
/// The input array.
diff --git a/src/FSharpPlus/Extensions/ResizeArray.fs b/src/FSharpPlus/Extensions/ResizeArray.fs
index 330b9b630..72de31931 100644
--- a/src/FSharpPlus/Extensions/ResizeArray.fs
+++ b/src/FSharpPlus/Extensions/ResizeArray.fs
@@ -134,6 +134,16 @@ module ResizeArray =
ra.Add (f a1.[i] a2.[i])
ra
+ /// 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.
+ /// If one array is shorter, excess elements are discarded from the right end of the longer array.
+ 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
+
///
/// Zip safely two ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
///
@@ -146,3 +156,17 @@ module ResizeArray =
for i in 0..(len-1) do
ra.Add (a1.[i], a2.[i])
ra
+
+ ///
+ /// Zip safely three ResizeArrays. If one ResizeArray is shorter, excess elements are discarded from the right end of the longer ResizeArray.
+ ///
+ /// First input ResizeArray.
+ /// Second input ResizeArray.
+ /// Third input ResizeArray.
+ /// ResizeArray with corresponding pairs of input ResizeArrays.
+ 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