5
5
module ResizeArray =
6
6
7
7
open System
8
+ open FSharpPlus.Internals .Errors
8
9
9
10
/// <summary>Builds a new ResizeArray whose elements are the results of applying the given function
10
11
/// to each of the elements of the ResizeArray.</summary>
@@ -15,11 +16,14 @@ module ResizeArray =
15
16
/// <returns>The result ResizeArray.</returns>
16
17
///
17
18
/// <exception cref="System.ArgumentNullException">Thrown when the input ResizeArray is null.</exception>
18
- let map ( mapping : 'T -> 'U ) ( source : ResizeArray < 'T >) = ResizeArray ( Seq.map mapping source)
19
+ let map ( mapping : 'T -> 'U ) ( source : ResizeArray < 'T >) =
20
+ raiseIfNull ( nameof source) source
21
+
22
+ ResizeArray ( Seq.map mapping source)
19
23
20
24
/// <summary>Applies a ResizeArray of functions to a ResizeArray of values and concatenates them.</summary>
21
25
/// <param name="f">The functions.</param>
22
- /// <param name="x ">The values.</param>
26
+ /// <param name="ra ">The values.</param>
23
27
/// <returns>A concatenated list of the resulting ResizeArray after applying each function to each value.</returns>
24
28
///
25
29
/// <example>
@@ -28,32 +32,57 @@ module ResizeArray =
28
32
/// val it : int list = [2; 4; 6; 3; 6; 9]
29
33
/// </code>
30
34
/// </example>
31
- let apply ( f : ResizeArray < 'T -> 'U >) ( x : ResizeArray < 'T >) = ResizeArray ( Seq.apply f x)
35
+ let apply ( f : ResizeArray < 'T -> 'U >) ( ra : ResizeArray < 'T >) =
36
+ raiseIfNull ( nameof ra) ra
37
+
38
+ ResizeArray ( Seq.apply f ra)
32
39
33
40
/// Combines all values from the first ResizeArray with the second, using the supplied mapping function.
34
- let lift2 mapping ( x1 : ResizeArray < 'T >) ( x2 : ResizeArray < 'U >) = ResizeArray ( Seq.lift2 mapping x1 x2)
41
+ let lift2 mapping ( ra1 : ResizeArray < 'T >) ( ra2 : ResizeArray < 'U >) =
42
+ raiseIfNull ( nameof ra1) ra1
43
+ raiseIfNull ( nameof ra2) ra2
44
+
45
+ ResizeArray ( Seq.lift2 mapping ra1 ra2)
35
46
36
47
/// <summary>Combines values from three ResizeArrays and calls a mapping function on this combination.</summary>
37
48
/// <param name="mapping">Mapping function taking three element combination as input.</param>
38
- /// <param name="x1 ">First ResizeArray.</param>
39
- /// <param name="x2 ">Second ResizeArray.</param>
40
- /// <param name="x3 ">Third ResizeArray.</param>
49
+ /// <param name="ra1 ">First ResizeArray.</param>
50
+ /// <param name="ra2 ">Second ResizeArray.</param>
51
+ /// <param name="ra3 ">Third ResizeArray.</param>
41
52
///
42
53
/// <returns>ResizeArray with values returned from mapping function.</returns>
43
- let lift3 mapping ( x1 : ResizeArray < 'T >) ( x2 : ResizeArray < 'U >) ( x3 : ResizeArray < 'V >) =
44
- ResizeArray ( Seq.lift3 mapping x1 x2 x3)
54
+ let lift3 mapping ( ra1 : ResizeArray < 'T >) ( ra2 : ResizeArray < 'U >) ( ra3 : ResizeArray < 'V >) =
55
+ raiseIfNull ( nameof ra1) ra1
56
+ raiseIfNull ( nameof ra2) ra2
57
+ raiseIfNull ( nameof ra3) ra3
58
+
59
+ ResizeArray ( Seq.lift3 mapping ra1 ra2 ra3)
45
60
46
61
/// Concatenates all elements, using the specified separator between each element.
47
- let intercalate ( separator : _ []) ( source : seq < _ []>) = source |> Seq.intercalate separator |> Seq.toArray
62
+ let intercalate ( separator : _ []) ( source : seq < _ []>) =
63
+ raiseIfNull ( nameof separator) separator
64
+ raiseIfNull ( nameof source) source
65
+
66
+ source |> Seq.intercalate separator |> Seq.toArray
48
67
49
68
/// Inserts a separator element between each element in the source ResizeArray.
50
- let intersperse element source = source |> Array.toSeq |> Seq.intersperse element |> Seq.toArray : 'T []
69
+ let intersperse element source =
70
+ raiseIfNull ( nameof element) element
71
+ raiseIfNull ( nameof source) source
72
+
73
+ source |> Array.toSeq |> Seq.intersperse element |> Seq.toArray : 'T []
51
74
52
75
/// Creates a sequence of arrays by splitting the source array on any of the given separators.
53
- let split ( separators : seq < _ []>) ( source : _ []) = source |> Array.toSeq |> Seq.split separators |> Seq.map Seq.toArray
76
+ let split ( separators : seq < _ []>) ( source : _ []) =
77
+ raiseIfNull ( nameof separators) separators
78
+ raiseIfNull ( nameof source) source
79
+ source |> Array.toSeq |> Seq.split separators |> Seq.map Seq.toArray
54
80
55
81
/// Replaces a subsequence of the source array with the given replacement array.
56
- let replace ( oldValue : _ []) ( newValue : _ []) source = source |> Array.toSeq |> Seq.replace oldValue newValue |> Seq.toArray : 'T []
82
+ let replace ( oldValue : _ []) ( newValue : _ []) source =
83
+ raiseIfNull ( nameof oldValue) oldValue
84
+ raiseIfNull ( nameof source) source
85
+ source |> Array.toSeq |> Seq.replace oldValue newValue |> Seq.toArray : 'T []
57
86
58
87
#if ! FABLE_ COMPILER
59
88
@@ -67,6 +96,9 @@ module ResizeArray =
67
96
/// The index of the slice.
68
97
/// </returns>
69
98
let findSliceIndex ( slice : _ []) ( source : _ []) =
99
+ raiseIfNull ( nameof slice) slice
100
+ raiseIfNull ( nameof source) source
101
+
70
102
let index = Internals.FindSliceIndex.arrayImpl slice source
71
103
if index = - 1 then
72
104
ArgumentException( " The specified slice was not found in the sequence." ) |> raise
@@ -81,6 +113,9 @@ module ResizeArray =
81
113
/// The index of the slice or <c>None</c>.
82
114
/// </returns>
83
115
let tryFindSliceIndex ( slice : _ []) ( source : _ []) =
116
+ raiseIfNull ( nameof slice) slice
117
+ raiseIfNull ( nameof source) source
118
+
84
119
let index = Internals.FindSliceIndex.arrayImpl slice source
85
120
if index = - 1 then None else Some index
86
121
@@ -120,6 +155,8 @@ module ResizeArray =
120
155
/// A tuple with both resulting arrays.
121
156
/// </returns>
122
157
let partitionMap ( mapper : 'T -> Choice < 'T1 , 'T2 >) ( source : array < 'T >) =
158
+ raiseIfNull ( nameof source) source
159
+
123
160
let ( x , y ) = ResizeArray (), ResizeArray ()
124
161
Array.iter ( mapper >> function Choice1Of2 e -> x.Add e | Choice2Of2 e -> y.Add e) source
125
162
x.ToArray (), y.ToArray ()
@@ -128,10 +165,13 @@ module ResizeArray =
128
165
/// to each of the elements of the two ResizeArrays pairwise.</summary>
129
166
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
130
167
let map2Shortest f ( a1 : ResizeArray < _ >) ( a2 : ResizeArray < _ >) =
168
+ raiseIfNull ( nameof a1) a1
169
+ raiseIfNull ( nameof a2) a2
170
+
131
171
let len = min a1.Count a2.Count
132
172
let ra = ResizeArray( len)
133
173
for i in 0 ..( len-1 ) do
134
- ra.Add ( f a1. [ i] a2. [ i])
174
+ ra.Add ( f a1[ i] a2[ i])
135
175
ra
136
176
137
177
/// <summary>Safely build a new ResizeArray whose elements are the results of applying the given function
@@ -151,10 +191,13 @@ module ResizeArray =
151
191
/// <param name="a2">Second input ResizeArray.</param>
152
192
/// <returns>ResizeArray with corresponding pairs of input ResizeArrays.</returns>
153
193
let zipShortest ( a1 : ResizeArray < 'T1 >) ( a2 : ResizeArray < 'T2 >) =
194
+ raiseIfNull ( nameof a1) a1
195
+ raiseIfNull ( nameof a2) a2
196
+
154
197
let len = min a1.Count a2.Count
155
198
let ra = ResizeArray( len)
156
199
for i in 0 ..( len-1 ) do
157
- ra.Add ( a1. [ i], a2. [ i])
200
+ ra.Add ( a1[ i], a2[ i])
158
201
ra
159
202
160
203
/// <summary>
0 commit comments