Skip to content

Commit 46405f1

Browse files
committed
Use ListCollector for DList.toList
1 parent 37bf287 commit 46405f1

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/FSharpPlus/Data/DList.fs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
open System
44
open System.Collections.Generic
55
open System.ComponentModel
6+
open FSharp.Core.CompilerServices
67
open FSharpPlus
78

89
// DList from FSharpx.Collections
@@ -183,6 +184,26 @@ type DList<'T> (length: int, data: DListData<'T>) =
183184
| Join (x, y) -> yield! walk (y::rights) x }
184185
(walk [] data).GetEnumerator ()
185186

187+
member internal this.toList () =
188+
#if FABLE_COMPILER
189+
DList<'T>.foldBack List.cons this []
190+
#else
191+
let mutable coll = new ListCollector<_> ()
192+
let rec walk rights = function
193+
| Nil ->
194+
match rights with
195+
| [] -> ()
196+
| t::ts -> walk ts t
197+
| Unit x ->
198+
coll.Add x
199+
match rights with
200+
| [] -> ()
201+
| t::ts -> walk ts t
202+
| Join (x, y) -> walk (y::rights) x
203+
walk [] data
204+
coll.Close ()
205+
#endif
206+
186207
interface IEquatable<DList<'T>> with
187208
member this.Equals(y: DList<'T>) =
188209
if this.Length <> y.Length then false
@@ -251,7 +272,7 @@ module DList =
251272
let ofSeq s = DList<'T>.ofSeq s
252273

253274
/// O(n). Returns a list of the DList elements.
254-
let inline toList l = foldBack List.cons l []
275+
let toList (l: DList<'T>) = l.toList ()
255276

256277
/// O(n). Returns a seq of the DList elements.
257278
let inline toSeq (l: DList<'T>) = l :> seq<'T>

0 commit comments

Comments
 (0)