Skip to content

Commit 7d7106f

Browse files
committed
net45 fixes for cons and uncons
1 parent 51d7136 commit 7d7106f

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/FSharpPlus/Data/NonEmptyList.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,15 @@ module NonEmptyList =
165165
/// <returns>A tuple with the head and the tail of the original list.</returns>
166166
/// <exception cref="T:System.ArgumentException">Thrown when the input list tail is empty.</exception>
167167
let uncons ({ Head = x; Tail = xs } as list) =
168+
#if !NET45
168169
match xs with
169170
| [] -> invalidArg (nameof(list)) "The input sequence has an empty tail"
170171
| _ -> x, ofList xs
172+
#else
173+
match xs with
174+
| [] -> invalidArg "list" "The input sequence has an empty tail"
175+
| _ -> x, ofList xs
176+
#endif
171177

172178
/// <summary>Splits the list in head and tail.</summary>
173179
/// <param name="list">The input list.</param>
@@ -181,9 +187,15 @@ module NonEmptyList =
181187
/// <exception cref="System.ArgumentException">Thrown when the tail is empty.</exception>
182188
/// <remarks>Throws exception for empty tail</remarks>
183189
let tail ({ Head = _; Tail = xs } as list) =
190+
#if !NET45
184191
match xs with
185192
| [] -> invalidArg (nameof(list)) "The input sequence has an empty tail"
186193
| _ -> ofList xs
194+
#else
195+
match xs with
196+
| [] -> invalidArg "list" "The input sequence has an empty tail"
197+
| _ -> ofList xs
198+
#endif
187199

188200
/// <summary>Returns a new NonEmptyList of the elements trailing the first element or None.</summary>
189201
let tryTail { Head = _; Tail = xs } = tryOfList xs

src/FSharpPlus/Extensions/Array.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,53 @@ module Array =
99
open System
1010
open FSharp.Core.CompilerServices
1111
open FSharpPlus.Internals.Errors
12+
#if NET45 // from FSharp.Core
13+
let insertAt (index: int) (value: 'T) (source: 'T array) : 'T array =
14+
15+
if index < 0 || index > source.Length then
16+
invalidArg "index" "index must be within bounds of the array"
17+
18+
let length = source.Length + 1
19+
let result: 'T array = Array.zeroCreate(length)
20+
21+
if index > 0 then
22+
Array.Copy(source, result, index)
23+
24+
result[index] <- value
25+
26+
if source.Length - index > 0 then
27+
Array.Copy(source, index, result, index + 1, source.Length - index)
28+
29+
result
30+
#endif
1231

1332
/// <summary>Adds an element to the beginning of the given array</summary>
1433
/// <param name="value">The element to add</param>
1534
/// <param name="array">The array to add to</param>
1635
/// <returns>A new array with the element added to the beginning.</returns>
1736
let cons value array =
37+
#if !NET45
1838
raiseIfNull (nameof(array)) array
1939
Array.insertAt 0 value array
40+
#else
41+
raiseIfNull "array" array
42+
insertAt 0 value array
43+
#endif
2044

2145
/// <summary>Splits the array in head and tail.</summary>
2246
/// <param name="array">The input array.</param>
2347
/// <returns>A tuple with the head and the tail of the original array.</returns>
2448
/// <exception cref="T:System.ArgumentException">Thrown when the input array is empty.</exception>
2549
let uncons array =
50+
#if !NET45
2651
raiseIfNull (nameof(array)) array
2752
if Array.isEmpty array then invalidArg (nameof(array)) LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
2853
else array[0], array[1..]
54+
#else
55+
raiseIfNull "array" array
56+
if Array.isEmpty array then invalidArg "array" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
57+
else array[0], array[1..]
58+
#endif
2959

3060
/// <summary>Applies an array of functions to an array of values and concatenates them.</summary>
3161
/// <param name="f">The array of functions.</param>

src/FSharpPlus/Extensions/List.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ module List =
4040
/// <param name="list">The input list.</param>
4141
/// <returns>A tuple with the head and the tail of the original list.</returns>
4242
/// <exception cref="T:System.ArgumentException">Thrown when the input list is empty.</exception>
43+
#if !NET45
4344
let uncons list =
4445
match list with
4546
| [] -> invalidArg (nameof(list)) LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
4647
| x::xs -> x, xs
48+
#else
49+
let uncons list =
50+
match list with
51+
| [] -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
52+
| x::xs -> x, xs
53+
#endif
4754

4855
/// <summary>Applies a list of functions to a list of values and concatenates them</summary>
4956
/// <param name="f">The list of functions.</param>

0 commit comments

Comments
 (0)