Skip to content

Commit 9a8128d

Browse files
forkiKevinRansom
authored andcommitted
Cleanup list.fs (#6506)
* Cleanup list.fs * feedback * feedback
1 parent fc74c3f commit 9a8128d

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

src/fsharp/FSharp.Core/list.fs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ namespace Microsoft.FSharp.Collections
1818
module List =
1919

2020
let inline checkNonNull argName arg =
21-
match box arg with
22-
| null -> nullArg argName
23-
| _ -> ()
21+
if isNull arg then
22+
nullArg argName
2423

2524
let inline indexNotFound() = raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
2625

@@ -386,10 +385,16 @@ namespace Microsoft.FSharp.Collections
386385
exists2aux f list1 list2
387386

388387
[<CompiledName("Find")>]
389-
let rec find predicate list = match list with [] -> indexNotFound() | h :: t -> if predicate h then h else find predicate t
388+
let rec find predicate list =
389+
match list with
390+
| [] -> indexNotFound()
391+
| h :: t -> if predicate h then h else find predicate t
390392

391393
[<CompiledName("TryFind")>]
392-
let rec tryFind predicate list = match list with [] -> None | h :: t -> if predicate h then Some h else tryFind predicate t
394+
let rec tryFind predicate list =
395+
match list with
396+
| [] -> None
397+
| h :: t -> if predicate h then Some h else tryFind predicate t
393398

394399
[<CompiledName("FindBack")>]
395400
let findBack predicate list = list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.findBack predicate
@@ -534,12 +539,20 @@ namespace Microsoft.FSharp.Collections
534539

535540
[<CompiledName("FindIndex")>]
536541
let findIndex predicate list =
537-
let rec loop n = function[] -> indexNotFound() | h :: t -> if predicate h then n else loop (n+1) t
542+
let rec loop n list =
543+
match list with
544+
| [] -> indexNotFound()
545+
| h :: t -> if predicate h then n else loop (n + 1) t
546+
538547
loop 0 list
539548

540549
[<CompiledName("TryFindIndex")>]
541550
let tryFindIndex predicate list =
542-
let rec loop n = function[] -> None | h :: t -> if predicate h then Some n else loop (n+1) t
551+
let rec loop n list =
552+
match list with
553+
| [] -> None
554+
| h :: t -> if predicate h then Some n else loop (n + 1) t
555+
543556
loop 0 list
544557

545558
[<CompiledName("FindIndexBack")>]
@@ -549,27 +562,27 @@ namespace Microsoft.FSharp.Collections
549562
let tryFindIndexBack predicate list = list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.tryFindIndexBack predicate
550563

551564
[<CompiledName("Sum")>]
552-
let inline sum (list:list<'T>) =
565+
let inline sum (list:list<'T>) =
553566
match list with
554-
| [] -> LanguagePrimitives.GenericZero< 'T >
567+
| [] -> LanguagePrimitives.GenericZero<'T>
555568
| t ->
556-
let mutable acc = LanguagePrimitives.GenericZero< 'T >
569+
let mutable acc = LanguagePrimitives.GenericZero<'T>
557570
for x in t do
558571
acc <- Checked.(+) acc x
559572
acc
560573

561574
[<CompiledName("SumBy")>]
562-
let inline sumBy (projection: 'T -> 'U) (list:list<'T>) =
575+
let inline sumBy (projection: 'T -> 'U) (list:list<'T>) =
563576
match list with
564-
| [] -> LanguagePrimitives.GenericZero< 'U >
577+
| [] -> LanguagePrimitives.GenericZero<'U>
565578
| t ->
566-
let mutable acc = LanguagePrimitives.GenericZero< 'U >
579+
let mutable acc = LanguagePrimitives.GenericZero<'U>
567580
for x in t do
568581
acc <- Checked.(+) acc (projection x)
569582
acc
570583

571584
[<CompiledName("Max")>]
572-
let inline max (list:list<_>) =
585+
let inline max (list:list<_>) =
573586
match list with
574587
| [] -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
575588
| h :: t ->
@@ -594,7 +607,7 @@ namespace Microsoft.FSharp.Collections
594607
acc
595608

596609
[<CompiledName("Min")>]
597-
let inline min (list:list<_>) =
610+
let inline min (list:list<_>) =
598611
match list with
599612
| [] -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
600613
| h :: t ->
@@ -619,11 +632,11 @@ namespace Microsoft.FSharp.Collections
619632
acc
620633

621634
[<CompiledName("Average")>]
622-
let inline average (list:list<'T>) =
635+
let inline average (list:list<'T>) =
623636
match list with
624637
| [] -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
625638
| xs ->
626-
let mutable sum = LanguagePrimitives.GenericZero< 'T >
639+
let mutable sum = LanguagePrimitives.GenericZero<'T>
627640
let mutable count = 0
628641
for x in xs do
629642
sum <- Checked.(+) sum x
@@ -635,7 +648,7 @@ namespace Microsoft.FSharp.Collections
635648
match list with
636649
| [] -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
637650
| xs ->
638-
let mutable sum = LanguagePrimitives.GenericZero< 'U >
651+
let mutable sum = LanguagePrimitives.GenericZero<'U>
639652
let mutable count = 0
640653
for x in xs do
641654
sum <- Checked.(+) sum (projection x)

0 commit comments

Comments
 (0)