Skip to content

Commit 654e54c

Browse files
committed
Add toSeq to several collections' module
Heap, Queue, RandomAccessList, Vector
1 parent ce5fb95 commit 654e54c

13 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/FSharpx.Core/Collections/Deque2.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module Deque =
139139
///O(1) amortized, O(n), worst case. Returns the first element and tail.
140140
val inline uncons : Deque<'T> -> 'T * Deque<'T>
141141

142-
///O(n). Returns a seq of the deque elements.
142+
///O(n). Views the given deque as a sequence.
143143
val inline toSeq : Deque<'T> -> seq<'T>
144144

145145
///O(1) amortized, O(n), worst case. Returns option first element and tail.

src/FSharpx.Core/Collections/Heap.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ module Heap =
207207

208208
let inline tryTail (xs: Heap<'T>) = xs.TryTail()
209209

210+
let inline toSeq (xs: Heap<'T>) = xs :> seq<'T>
211+
210212
let inline uncons (xs: Heap<'T>) = xs.Uncons()
211213

212214
let inline tryUncons (xs: Heap<'T>) = xs.TryUncons()

src/FSharpx.Core/Collections/Heap.fsi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module Heap =
8585
///O(log n) amortized time. Returns heap option from merging two heaps.
8686
val inline tryMerge : Heap<'T> -> Heap<'T> -> Heap<'T> option
8787

88-
///O(n). Returns heap from the sequence.
88+
///O(n). Returns heap, bool isDescending, from the sequence.
8989
val ofSeq : bool -> seq<'T> -> Heap<'T>
9090

9191
///O(n). Returns heap reversed.
@@ -97,6 +97,9 @@ module Heap =
9797
///O(log n) amortized time. Returns option heap of the elements trailing the head.
9898
val inline tryTail : Heap<'T> -> Heap<'T> option
9999

100+
///O(n). Views the given heap as a sequence.
101+
val inline toSeq : Heap<'T> -> seq<'T>
102+
100103
///O(log n) amortized time. Returns the head element and tail.
101104
val inline uncons : Heap<'T> -> 'T * Heap<'T>
102105

src/FSharpx.Core/Collections/Queue.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ module Queue =
127127

128128
let inline tryTail (q : Queue<'T>) = q.TryTail
129129

130+
let inline toSeq (q: Queue<'T>) = q :> seq<'T>
131+
130132
let inline uncons (q : Queue<'T>) = q.Uncons
131133

132134
let inline tryUncons (q : Queue<'T>) = q.TryUncons

src/FSharpx.Core/Collections/Queue.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ module Queue =
8989
///O(1) amortized, O(n) worst-case. Returns option queue of the elements trailing the first element
9090
val inline tryTail : Queue<'T> -> Queue<'T> option
9191

92+
///O(n). Views the given queue as a sequence.
93+
val inline toSeq : Queue<'T> -> seq<'T>
94+
9295
///O(1) amortized, O(n) worst-case. Returns the first element and tail.
9396
val inline uncons : Queue<'T> -> 'T * Queue<'T>
9497

src/FSharpx.Core/Collections/RandomAccessList.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ module RandomAccessList =
390390

391391
let inline tryTail (randomAccessList :'T RandomAccessList) = randomAccessList.TryTail
392392

393+
let inline toSeq (randomAccessList: 'T RandomAccessList) = randomAccessList :> seq<'T>
394+
393395
let inline uncons (randomAccessList :'T RandomAccessList) = randomAccessList.Uncons
394396

395397
let inline tryUncons (randomAccessList :'T RandomAccessList) = randomAccessList.TryUncons

src/FSharpx.Core/Collections/RandomAccessList.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ module RandomAccessList =
103103
/// O(n). Returns option random access list without the last item.
104104
val inline tryTail : RandomAccessList<'T> -> RandomAccessList<'T> option
105105

106+
///O(n). Views the given random access list as a sequence.
107+
val inline toSeq : RandomAccessList<'T> -> seq<'T>
108+
106109
/// O(1). Returns tuple last element and random access list without last item
107110
val inline uncons : RandomAccessList<'T> -> 'T * RandomAccessList<'T>
108111

src/FSharpx.Core/Collections/Vector.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ module Vector =
443443

444444
let inline singleton (x : 'T) = empty |> conj x
445445

446+
let inline toSeq (vector: Vector<'T>) = vector :> seq<'T>
447+
446448
let inline unconj (vector: Vector<'T>) = vector.Unconj
447449

448450
let inline tryUnconj (vector: Vector<'T>) = vector.TryUnconj

src/FSharpx.Core/Collections/Vector.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ module Vector =
119119
/// O(1). Returns a new vector of one element.
120120
val inline singleton : 'T -> Vector<'T>
121121

122+
///O(n). Views the given vector as a sequence.
123+
val inline toSeq : Vector<'T> -> seq<'T>
124+
122125
/// O(1). Returns tuple last element and vector without last item
123126
val inline unconj : Vector<'T> -> Vector<'T> * 'T
124127

tests/FSharpx.Collections.Tests/HeapTest.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ let ``structural equality``() =
211211

212212
l1 = l3 |> should equal false
213213

214+
[<Test>]
215+
let ``toSeq to list``() =
216+
let l = ["f";"e";"d";"c";"b";"a"]
217+
let h = ofSeq true l
218+
219+
h|> toSeq |> List.ofSeq |> should equal l
220+
214221
[<Test>]
215222
[<TestCaseSource("intGensStart2")>]
216223
let ``tryUncons 1 element``(x : obj) =

0 commit comments

Comments
 (0)