Skip to content

Commit

Permalink
+ DList.iter
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Jan 17, 2024
1 parent deb8474 commit 71c8e17
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/FSharpPlus/Data/DList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,6 @@ 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 ()
#endif

interface IEquatable<DList<'T>> with
member this.Equals(y: DList<'T>) =
if this.Length <> y.Length then false
Expand Down Expand Up @@ -271,8 +251,30 @@ module DList =
/// O(n). Returns a DList of the seq.
let ofSeq s = DList<'T>.ofSeq s

/// O(n). Returns a list of the DList elements.
let toList (l: DList<'T>) = l.toList ()
/// Iterates over each element of the list.
let iter action (source: DList<'T>) =
let rec walk rights = function
| Nil ->
match rights with
| [] -> ()
| t::ts -> walk ts t
| Unit x ->
action x
match rights with
| [] -> ()
| t::ts -> walk ts t
| Join (x, y) -> walk (y::rights) x
walk [] source.dc

/// Returns a list of the DList elements.
let toList (source: DList<'T>) =
#if FABLE_COMPILER
DList<'T>.foldBack List.cons source []
#else
let mutable coll = new ListCollector<_> ()
iter coll.Add source
coll.Close ()
#endif

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

0 comments on commit 71c8e17

Please sign in to comment.