@@ -1012,3 +1012,201 @@ let isDefault (array : FlatList<'T>) = array.IsDefault
10121012
10131013[<CompiledName " IsDefaultOrEmpty" >]
10141014let isDefaultOrEmpty ( array : FlatList < 'T >) = array.IsDefaultOrEmpty
1015+
1016+ // Most functions unwrap the underlying array without a copy, call the Array.Parallel function, and wrap its fresh result
1017+ // without a copy (ADR 0001, D11), so that they have the semantics and the exceptions of Array.Parallel. Where the
1018+ // Array.Parallel function takes or returns options or builds a tuple per element or per group (choose, tryFind,
1019+ // tryFindIndex, tryPick, zip, groupBy), an algorithm of ParallelArrays with value options and struct tuples runs instead.
1020+ // Array.Parallel sorts a copy of its input, so the shared array is never changed.
1021+ [<RequireQualifiedAccess>]
1022+ module Parallel =
1023+
1024+ // ---- Creation ----
1025+
1026+ [<CompiledName " Initialize" >]
1027+ let init count ( initializer : int -> 'T ) = Array.Parallel.init count initializer |> wrapArray
1028+
1029+ // ---- Search ----
1030+
1031+ [<CompiledName " Exists" >]
1032+ let exists predicate ( array : FlatList < 'T >) = Array.Parallel.exists predicate ( unwrapArray ( nameof array) array)
1033+
1034+ [<CompiledName " ForAll" >]
1035+ let forall predicate ( array : FlatList < 'T >) = Array.Parallel.forall predicate ( unwrapArray ( nameof array) array)
1036+
1037+ [<CompiledName " TryFind" >]
1038+ let tryFind predicate ( array : FlatList < 'T >) =
1039+ let source = unwrapArray ( nameof array) array
1040+
1041+ match ParallelArrays.tryFindIndex predicate source with
1042+ | ValueSome index -> ValueSome source[ index]
1043+ | ValueNone -> ValueNone
1044+
1045+ [<CompiledName " TryFindIndex" >]
1046+ let tryFindIndex predicate ( array : FlatList < 'T >) = ParallelArrays.tryFindIndex predicate ( unwrapArray ( nameof array) array)
1047+
1048+ [<CompiledName " TryPick" >]
1049+ let tryPick ( chooser : 'T -> 'U voption ) ( array : FlatList < 'T >) =
1050+ ParallelArrays.tryPick chooser ( unwrapArray ( nameof array) array)
1051+
1052+ // ---- Transformation ----
1053+
1054+ [<CompiledName " Choose" >]
1055+ let choose ( chooser : 'T -> 'U voption ) ( array : FlatList < 'T >) =
1056+ ParallelArrays.choose chooser ( unwrapArray ( nameof array) array)
1057+ |> wrapArray
1058+
1059+ [<CompiledName " Collect" >]
1060+ let collect ( mapping : 'T -> FlatList < 'U >) ( array : FlatList < 'T >) =
1061+ // As in Array.Parallel.collect, the mapping runs in parallel, and the results are copied in their order into one
1062+ // array of their total length. Array.Parallel.collect dereferences a null result; a default flat list from the
1063+ // mapping throws ArgumentNullException instead, as in the sequential collect (ADR 0001, D6).
1064+ let results = Array.Parallel.map mapping ( unwrapArray ( nameof array) array)
1065+ let mutable length = 0
1066+
1067+ for result in results do
1068+ checkNotDefault ( nameof mapping) result
1069+ length <- length + result.Length
1070+
1071+ let builder = ImmutableArray.CreateBuilder< 'U> length
1072+
1073+ for result in results do
1074+ builder.AddRange result
1075+
1076+ builder.MoveToImmutable ()
1077+
1078+ [<CompiledName " Filter" >]
1079+ let filter predicate ( array : FlatList < 'T >) =
1080+ Array.Parallel.filter predicate ( unwrapArray ( nameof array) array)
1081+ |> wrapArray
1082+
1083+ [<CompiledName " Map" >]
1084+ let map ( mapping : 'T -> 'U ) ( array : FlatList < 'T >) =
1085+ Array.Parallel.map mapping ( unwrapArray ( nameof array) array)
1086+ |> wrapArray
1087+
1088+ [<CompiledName " MapIndexed" >]
1089+ let mapi ( mapping : int -> 'T -> 'U ) ( array : FlatList < 'T >) =
1090+ Array.Parallel.mapi mapping ( unwrapArray ( nameof array) array)
1091+ |> wrapArray
1092+
1093+ [<CompiledName " Zip" >]
1094+ let zip ( array1 : FlatList < 'T1 >) ( array2 : FlatList < 'T2 >) =
1095+ let source1 = unwrapArray ( nameof array1) array1
1096+ let source2 = unwrapArray ( nameof array2) array2
1097+ ArrayChecks.sameLengths ( nameof array1) source1 ( nameof array2) source2
1098+ ParallelArrays.zip structPair source1 source2 |> wrapArray
1099+
1100+ // ---- Folding ----
1101+
1102+ // The inline functions can use public members only, so they check and unwrap the array themselves
1103+ [<CompiledName " Average" >]
1104+ let inline average ( array : FlatList < ^T >) : ^T =
1105+ if array.IsDefault then
1106+ nullArg ( nameof array)
1107+
1108+ Array.Parallel.average (
1109+ ImmutableCollectionsMarshal.AsArray array
1110+ |> Unchecked.nonNull
1111+ )
1112+
1113+ [<CompiledName " AverageBy" >]
1114+ let inline averageBy ( [<InlineIfLambda>] projection : 'T -> ^U ) ( array : FlatList < 'T >) : ^U =
1115+ if array.IsDefault then
1116+ nullArg ( nameof array)
1117+
1118+ Array.Parallel.averageBy
1119+ projection
1120+ ( ImmutableCollectionsMarshal.AsArray array
1121+ |> Unchecked.nonNull)
1122+
1123+ [<CompiledName " Iterate" >]
1124+ let iter action ( array : FlatList < 'T >) = Array.Parallel.iter action ( unwrapArray ( nameof array) array)
1125+
1126+ [<CompiledName " IterateIndexed" >]
1127+ let iteri action ( array : FlatList < 'T >) = Array.Parallel.iteri action ( unwrapArray ( nameof array) array)
1128+
1129+ [<CompiledName " Max" >]
1130+ let max ( array : FlatList < 'T >) = Array.Parallel.max ( unwrapArray ( nameof array) array)
1131+
1132+ [<CompiledName " MaxBy" >]
1133+ let maxBy ( projection : 'T -> 'U ) ( array : FlatList < 'T >) = Array.Parallel.maxBy projection ( unwrapArray ( nameof array) array)
1134+
1135+ [<CompiledName " Min" >]
1136+ let min ( array : FlatList < 'T >) = Array.Parallel.min ( unwrapArray ( nameof array) array)
1137+
1138+ [<CompiledName " MinBy" >]
1139+ let minBy ( projection : 'T -> 'U ) ( array : FlatList < 'T >) = Array.Parallel.minBy projection ( unwrapArray ( nameof array) array)
1140+
1141+ [<CompiledName " Reduce" >]
1142+ let reduce reduction ( array : FlatList < 'T >) = Array.Parallel.reduce reduction ( unwrapArray ( nameof array) array)
1143+
1144+ [<CompiledName " ReduceBy" >]
1145+ let reduceBy ( projection : 'T -> 'U ) reduction ( array : FlatList < 'T >) =
1146+ Array.Parallel.reduceBy projection reduction ( unwrapArray ( nameof array) array)
1147+
1148+ // Array.Parallel.sum dereferences a null array without a check; a default flat list throws ArgumentNullException,
1149+ // which its documentation promises
1150+ [<CompiledName " Sum" >]
1151+ let inline sum ( array : FlatList < ^T >) : ^T =
1152+ if array.IsDefault then
1153+ nullArg ( nameof array)
1154+
1155+ Array.Parallel.sum (
1156+ ImmutableCollectionsMarshal.AsArray array
1157+ |> Unchecked.nonNull
1158+ )
1159+
1160+ [<CompiledName " SumBy" >]
1161+ let inline sumBy ( [<InlineIfLambda>] projection : 'T -> ^U ) ( array : FlatList < 'T >) : ^U =
1162+ if array.IsDefault then
1163+ nullArg ( nameof array)
1164+
1165+ Array.Parallel.sumBy
1166+ projection
1167+ ( ImmutableCollectionsMarshal.AsArray array
1168+ |> Unchecked.nonNull)
1169+
1170+ // ---- Partitioning and grouping ----
1171+
1172+ [<CompiledName " GroupBy" >]
1173+ let groupBy ( projection : 'T -> 'Key ) ( array : FlatList < 'T >) =
1174+ ParallelArrays.groupBy ( fun key items -> struct ( key, wrapArray items)) projection ( unwrapArray ( nameof array) array)
1175+ |> wrapArray
1176+
1177+ [<CompiledName " Partition" >]
1178+ let partition predicate ( array : FlatList < 'T >) =
1179+ let accepted , rejected = Array.Parallel.partition predicate ( unwrapArray ( nameof array) array)
1180+ struct ( wrapArray accepted, wrapArray rejected)
1181+
1182+ [<CompiledName " PartitionWith" >]
1183+ let partitionWith ( partitioner : 'T -> Choice < 'T1 , 'T2 >) ( array : FlatList < 'T >) =
1184+ let first , second = Array.Parallel.partitionWith partitioner ( unwrapArray ( nameof array) array)
1185+ struct ( wrapArray first, wrapArray second)
1186+
1187+ // ---- Sorting ----
1188+
1189+ [<CompiledName " Sort" >]
1190+ let sort ( array : FlatList < 'T >) =
1191+ Array.Parallel.sort ( unwrapArray ( nameof array) array)
1192+ |> wrapArray
1193+
1194+ [<CompiledName " SortBy" >]
1195+ let sortBy ( projection : 'T -> 'Key ) ( array : FlatList < 'T >) =
1196+ Array.Parallel.sortBy projection ( unwrapArray ( nameof array) array)
1197+ |> wrapArray
1198+
1199+ [<CompiledName " SortByDescending" >]
1200+ let sortByDescending ( projection : 'T -> 'Key ) ( array : FlatList < 'T >) =
1201+ Array.Parallel.sortByDescending projection ( unwrapArray ( nameof array) array)
1202+ |> wrapArray
1203+
1204+ [<CompiledName " SortDescending" >]
1205+ let sortDescending ( array : FlatList < 'T >) =
1206+ Array.Parallel.sortDescending ( unwrapArray ( nameof array) array)
1207+ |> wrapArray
1208+
1209+ [<CompiledName " SortWith" >]
1210+ let sortWith comparer ( array : FlatList < 'T >) =
1211+ Array.Parallel.sortWith comparer ( unwrapArray ( nameof array) array)
1212+ |> wrapArray
0 commit comments