Skip to content

Commit 0f11f85

Browse files
xperiandriclaude
andcommitted
feat: FlatList.Parallel module with the vocabulary of the FSharp.Core Array.Parallel module (#27)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 58be5f7 commit 0f11f85

14 files changed

Lines changed: 2389 additions & 1 deletion

‎.github/copilot-instructions.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module's `.fsi` and the `.fs` on its `.fs`, so that Visual Studio shows them as
2929
│ │ ├── ErrorStrings.fs – the exception messages of FSharp.Core and the .NET libraries
3030
│ │ ├── Raise.fs – the exceptions of the FSharp.Core DetailedExceptions module
3131
│ │ ├── ArrayChecks.fs – the checks that the functions of several arrays share
32-
│ │ └── Arrays.fs – the algorithms over arrays that the default and Tuple modules share
32+
│ │ ├── Arrays.fs – the algorithms over arrays that the default and Tuple modules share
33+
│ │ └── ParallelArrays.fs – the parallel algorithms that the default and Tuple modules share
3334
│ ├── FlatList/ – ImmutableArray API surface and builders
3435
│ │ ├── FlatListTypes.fs – the FlatList abbreviation
3536
│ │ ├── FlatList.fs / FlatList.fsi – includes the nested Parallel module

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
change the builder return it. `replace` and `replaceWith` leave the builder as it is when no element equals the old
2727
value, as `ImmutableArray<'T>.Builder.Replace` does, where `FlatList.replace` throws `ArgumentException`.
2828
* `flatList { }` builds a flat list.
29+
* `FlatList.Parallel` has every function of the FSharp.Core `Array.Parallel` module except the in-place sorts, with its
30+
semantics and exceptions. Its `tryFind`, `tryFindIndex` and `tryPick` return `voption`, `choose` and `tryPick` take
31+
functions that return `voption`, and `groupBy`, `partition`, `partitionWith` and `zip` return struct tuples; the
32+
`Parallel` modules of the `Option` and `Tuple` namespaces repeat them with options and reference tuples.
2933

3034
### Changed
3135

‎src/FSharp.Collections.Immutable/FSharp.Collections.Immutable.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="Helper\Raise.fs" />
3333
<Compile Include="Helper\ArrayChecks.fs" />
3434
<Compile Include="Helper\Arrays.fs" />
35+
<Compile Include="Helper\ParallelArrays.fs" />
3536
<Compile Include="FlatList\FlatListTypes.fs" />
3637
<Compile Include="FlatList\FlatList.fsi" />
3738
<Compile Include="FlatList\FlatList.fs" />

‎src/FSharp.Collections.Immutable/FlatList/FlatList.fs‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,201 @@ let isDefault (array : FlatList<'T>) = array.IsDefault
10121012

10131013
[<CompiledName "IsDefaultOrEmpty">]
10141014
let 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

Comments
 (0)