Skip to content

Commit ed91c8d

Browse files
xperiandriclaude
andcommitted
fixup! feat: Queue and IQueue modules with the vocabulary of the FSharp.Core List module
Follows the review of #33 and ADR 0001 (D6, D7, D14): - `sort`, `sortByDescending`, `sortDescending` and `sortWith` of `Queue` and `IQueue` sort with the `Seq` functions of FSharp.Core, which share the stable sort of `List`, so that a comparison or a projection that throws throws what `List` throws. LINQ already wrapped the exception of a comparison in an `InvalidOperationException`, as `Array.Sort` does. `OrderByDescending` differed for the projection, which `List.sortByDescending` calls inside the comparison. `sortBy` and `sortWithComparer` keep LINQ. - `sortDescending` and `sortByDescending` call `Seq.sortWith` with the cached comparer of F# generic comparison, through the new `Queues.sortDescending` and `Queues.sortByDescending`. `Seq.sortDescending` and `Seq.sortByDescending` are inline: inside a generic function they compare with the generic operator, which boxes the elements of a value type in every comparison. For 10 000 integers, they allocate 7.8 MB per call instead of 0.74 MB. - At 10 000 integers, `sortWith` takes about the same time as before and allocates 743 KB per call instead of 440 KB. - `QueueCE.fsi` carries the signatures and the documentation of `QueueCE`, compiled right before it. `queue` has its `val` and its example in `ComputationExpressions.fsi`, after `indexedList`, and only the binding stays in the `.fs`. - `queue { yield! ... }` rejects a null sequence, array or queue with `ArgumentNullException` for the parameter `items`. - The remarks of `IQueue.append` say that the result keeps the implementation of the first queue, which receives the elements (D14), also when that queue is empty. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent c4c9e3e commit ed91c8d

15 files changed

Lines changed: 1435 additions & 471 deletions

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,4 @@ let flatList = FlatListCE ()
77

88
let indexedList = IndexedListCE ()
99

10-
/// <summary>Builds a queue.</summary>
11-
/// <example id="queue-1">
12-
/// <code lang="fsharp">
13-
/// let squares = queue {
14-
/// yield 0
15-
/// for i in 1..3 do
16-
/// yield i * i
17-
/// yield! [ 100; 200 ]
18-
/// }
19-
/// </code>
20-
/// Evaluates to a queue of 0, 1, 4, 9, 100 and 200, from which 0 leaves first.
21-
/// </example>
2210
let queue = QueueCE ()

‎src/FSharp.Collections.Immutable/ComputationExpressions.fsi‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ val flatList : FlatListCE
3333
/// Evaluates to an indexed list of 0, 1, 4, 9, 100 and 200.
3434
/// </example>
3535
val indexedList : IndexedListCE
36+
37+
/// <summary>Builds a queue.</summary>
38+
/// <example id="queue-1">
39+
/// <code lang="fsharp">
40+
/// let squares = queue {
41+
/// yield 0
42+
/// for i in 1..3 do
43+
/// yield i * i
44+
/// yield! [ 100; 200 ]
45+
/// }
46+
/// </code>
47+
/// Evaluates to a queue of 0, 1, 4, 9, 100 and 200, from which 0 leaves first.
48+
/// </example>
49+
val queue : QueueCE

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Queue\IQueueOption.fs" />
8282
<Compile Include="Queue\IQueueTuple.fsi" />
8383
<Compile Include="Queue\IQueueTuple.fs" />
84+
<Compile Include="Queue\QueueCE.fsi" />
8485
<Compile Include="Queue\QueueCE.fs" />
8586
<Compile Include="Stack.fs" />
8687
<Compile Include="Maps.fs" />

‎src/FSharp.Collections.Immutable/Helper/Queues.fs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,36 @@ let lastMatch (predicate : 'T -> bool) (queue : IImmutableQueue<'T>) =
135135
ValueNone
136136
else
137137
ValueSome (struct (index, array[index]))
138+
139+
/// <summary>
140+
/// The elements of a sequence in descending order of F# generic comparison, sorted stably as
141+
/// <see cref="M:Microsoft.FSharp.Collections.ListModule.SortDescending``1(Microsoft.FSharp.Collections.FSharpList{``0})"/>
142+
/// sorts them, when the sequence is read.
143+
/// </summary>
144+
/// <remarks>
145+
/// <see cref="M:Microsoft.FSharp.Collections.SeqModule.SortDescending``1(System.Collections.Generic.IEnumerable{``0})"/>
146+
/// is inline. Compiled into a generic function, it compares with the generic operator, which boxes the elements of a
147+
/// value type in every comparison: at 10 000 integers, it allocates 17 times the memory of this sort. This sort calls
148+
/// <see cref="M:Microsoft.FSharp.Collections.SeqModule.SortWith``1(Microsoft.FSharp.Core.FSharpFunc{``0,Microsoft.FSharp.Core.FSharpFunc{``0,System.Int32}},System.Collections.Generic.IEnumerable{``0})"/>
149+
/// with the comparer of F# generic comparison that <c>DefaultComparer</c> caches, which is the same comparison without
150+
/// the boxes.
151+
/// </remarks>
152+
let sortDescending (source : seq<'T>) =
153+
let comparer = DefaultComparer<'T>.Instance
154+
// The annotations keep the arguments non-nullable: IComparer<'T>.Compare takes nullable ones, and the sequence of
155+
// the result would get that element type
156+
Seq.sortWith (fun (left : 'T) (right : 'T) -> comparer.Compare (right, left)) source
157+
158+
/// <summary>
159+
/// The elements of a sequence in descending order of a projection, sorted stably as
160+
/// <see cref="M:Microsoft.FSharp.Collections.ListModule.SortByDescending``2(Microsoft.FSharp.Core.FSharpFunc{``0,``1},Microsoft.FSharp.Collections.FSharpList{``0})"/>
161+
/// sorts them, when the sequence is read.
162+
/// </summary>
163+
/// <remarks>
164+
/// The projection runs in the comparison, twice for each comparison, as it does in
165+
/// <see cref="M:Microsoft.FSharp.Collections.ListModule.SortByDescending``2(Microsoft.FSharp.Core.FSharpFunc{``0,``1},Microsoft.FSharp.Collections.FSharpList{``0})"/>.
166+
/// The sort avoids the boxing of the keys of a value type for the reason that <c>sortDescending</c> does.
167+
/// </remarks>
168+
let sortByDescending (projection : 'T -> 'Key) (source : seq<'T>) =
169+
let comparer = DefaultComparer<'Key>.Instance
170+
Seq.sortWith (fun left right -> comparer.Compare (projection right, projection left)) source

‎src/FSharp.Collections.Immutable/Queue/IQueue.fs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,14 @@ let truncate count (queue : IQueue<'T>) =
778778

779779
// ---- Sorting ----
780780

781-
// LINQ sorts stably, as List does (ADR 0001, D5); a queue of one element is returned without a call of the projection
782-
// or the comparer, as List returns a list of one element
781+
// The sorts are stable, as the sorts of List are (ADR 0001, D5). The functions that compare the elements, or that call the
782+
// projection in the comparison as List.sortByDescending does, sort with the Seq functions of FSharp.Core, which share the
783+
// stable sort of List: the same comparisons run in Array.Sort, which wraps the exception of a comparison in an
784+
// InvalidOperationException (D6). The descending sorts call Seq.sortWith with the comparer of F# generic comparison
785+
// through Queues, because Seq.sortDescending and Seq.sortByDescending box the elements of a value type in every comparison
786+
// once they are compiled into a generic function. sortBy and sortWithComparer sort with LINQ, which computes the keys
787+
// first, as List.sortBy does, and wraps the exception of a comparison as Array.Sort does. A queue of one element is
788+
// returned without a call of the projection or the comparer, as List returns a list of one element.
783789

784790
[<CompiledName "Sort">]
785791
let sort (queue : IQueue<'T>) =
@@ -788,7 +794,7 @@ let sort (queue : IQueue<'T>) =
788794
if Lengths.queueUpTo 2 queue <= 1 then
789795
queue
790796
else
791-
rebuild queue (queue.Order(DefaultComparer<'T>.Instance).ToArray())
797+
rebuild queue (Seq.sort queue)
792798

793799
[<CompiledName "SortBy">]
794800
let sortBy (projection : 'T -> 'Key) (queue : IQueue<'T>) =
@@ -806,7 +812,7 @@ let sortByDescending (projection : 'T -> 'Key) (queue : IQueue<'T>) =
806812
if Lengths.queueUpTo 2 queue <= 1 then
807813
queue
808814
else
809-
rebuild queue (queue.OrderByDescending(Func<'T, 'Key> projection, DefaultComparer<'Key>.Instance).ToArray())
815+
rebuild queue (Queues.sortByDescending projection queue)
810816

811817
[<CompiledName "SortDescending">]
812818
let sortDescending (queue : IQueue<'T>) =
@@ -815,7 +821,7 @@ let sortDescending (queue : IQueue<'T>) =
815821
if Lengths.queueUpTo 2 queue <= 1 then
816822
queue
817823
else
818-
rebuild queue (queue.OrderDescending(DefaultComparer<'T>.Instance).ToArray())
824+
rebuild queue (Queues.sortDescending queue)
819825

820826
[<CompiledName "SortWith">]
821827
let sortWith (comparer : 'T -> 'T -> int) (queue : IQueue<'T>) =
@@ -824,7 +830,7 @@ let sortWith (comparer : 'T -> 'T -> int) (queue : IQueue<'T>) =
824830
if Lengths.queueUpTo 2 queue <= 1 then
825831
queue
826832
else
827-
rebuild queue (queue.Order(ComparisonIdentity.FromFunction comparer).ToArray())
833+
rebuild queue (Seq.sortWith comparer queue)
828834

829835
[<CompiledName "SortWithComparer">]
830836
let sortWithComparer (comparer : System.Collections.Generic.IComparer<'T>) (queue : IQueue<'T>) =

0 commit comments

Comments
 (0)