From 00c4867720717a469c1b86b7dfdcdfc7cf9af893 Mon Sep 17 00:00:00 2001 From: Gustavo Leon <1261319+gusty@users.noreply.github.com> Date: Mon, 15 Jan 2024 21:23:25 +0100 Subject: [PATCH] Use ListCollector --- src/FSharpPlus/Data/DList.fs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/FSharpPlus/Data/DList.fs b/src/FSharpPlus/Data/DList.fs index 0e03a0a10..0d62e0c1b 100644 --- a/src/FSharpPlus/Data/DList.fs +++ b/src/FSharpPlus/Data/DList.fs @@ -183,6 +183,25 @@ type DList<'T> (length: int, data: DListData<'T>) = | Join (x, y) -> yield! walk (y::rights) x } (walk [] data).GetEnumerator () + member internal this.toList () = + #if FABLE_COMPILER + DList<'T>.foldBack List.cons this [] + #else + let mutable coll = new ListCollector<_> () + let rec walk rights = function + | Nil -> + match rights with + | [] -> () + | t::ts -> walk ts t + | Unit x -> + coll.Add x + match rights with + | [] -> () + | t::ts -> walk ts t + | Join (x, y) -> walk (y::rights) x + walk [] data + coll.Close () + interface IEquatable> with member this.Equals(y: DList<'T>) = if this.Length <> y.Length then false @@ -251,7 +270,7 @@ module DList = let ofSeq s = DList<'T>.ofSeq s /// O(n). Returns a list of the DList elements. - let inline toList l = foldBack List.cons l [] + let toList (l: DList<'T>) = l.toList () /// O(n). Returns a seq of the DList elements. let inline toSeq (l: DList<'T>) = l :> seq<'T>