@@ -7,13 +7,11 @@ open FSharpPlus
77// DList from FSharpx.Collections
88//This implementation adds an additional parameter to allow O(1) retrieval of the list length.
99
10-
1110type DListData < 'T > =
1211 | Nil
1312 | Unit of 'T
1413 | Join of DListData < 'T > * DListData < 'T >
1514
16-
1715/// DList is an ordered linear structure implementing the List signature (head, tail, cons),
1816/// end-insertion (add), and O(1) append. Ordering is by insertion history.
1917/// DList is an implementation of [ John Hughes' append list] ( http://dl.acm.org/citation.cfm?id=8475 ) .
@@ -25,11 +23,11 @@ type DList<'T> (length: int, data: DListData<'T>) =
2523
2624 static member ofSeq ( s : seq < 'T >) =
2725 DList ( Seq.fold ( fun ( i , state ) x ->
28- ( i+ 1 ,
26+ ( i + 1 ,
2927 match state with
30- | Nil -> Unit x
31- | Unit _ -> Join ( state, Unit x)
32- | Join(_,_) -> Join ( state, Unit x))) ( 0 , Nil) s)
28+ | Nil -> Unit x
29+ | Unit _ -> Join ( state, Unit x)
30+ | Join(_, _) -> Join ( state, Unit x))) ( 0 , Nil) s)
3331
3432 override this.GetHashCode () =
3533 match hashCode with
@@ -44,14 +42,14 @@ type DList<'T> (length: int, data: DListData<'T>) =
4442 override this.Equals other =
4543 #if FABLE_ COMPILER
4644 let y = other :?> DList< 'T>
47- if this.Length <> y.Length then false
45+ if this.Length <> y.Length then false
4846 else
4947 if hash this <> hash y then false
5048 else Seq.forall2 Unchecked.equals this y
5149 #else
5250 match other with
53- | :? DList< 'T> as y ->
54- if this.Length <> y.Length then false
51+ | :? DList< 'T> as y ->
52+ if this.Length <> y.Length then false
5553 else
5654 if this.GetHashCode () <> y.GetHashCode () then false
5755 else Seq.forall2 Unchecked.equals this y
@@ -65,7 +63,7 @@ type DList<'T> (length: int, data: DListData<'T>) =
6563 // Called a "fold" in the article processes the linear representation from right to left
6664 // and so is more appropriately implemented under the foldBack signature
6765 // See http://stackoverflow.com/questions/5324623/functional-o1-append-and-on-iteration-from-first-element-list-data-structure/5334068#5334068
68- static member foldBack ( f : 'T -> 'State -> 'State ) ( l : DList < 'T >) ( state : 'State ) =
66+ static member foldBack ( f : 'T -> 'State -> 'State ) ( l : DList < 'T >) ( state : 'State ) =
6967 let rec walk lefts l xs =
7068 match l with
7169 | Nil -> finish lefts xs
@@ -78,37 +76,35 @@ type DList<'T> (length: int, data: DListData<'T>) =
7876 walk [] l.dc state
7977
8078 // making only a small adjustment to Ramsey's algorithm we get a left to right fold
81- static member fold ( f : 'State -> 'T -> 'State ) ( state : 'State ) ( l : DList < 'T >) =
82- let f = OptimizedClosures.FSharpFunc<_,_, _>. Adapt f
79+ static member fold ( f : 'State -> 'T -> 'State ) ( state : 'State ) ( l : DList < 'T >) =
80+ let f = OptimizedClosures.FSharpFunc<_, _, _>. Adapt f
8381 let rec walk rights l xs =
8482 match l with
85- | Nil -> finish rights xs
86- | Unit x -> finish rights <| f.Invoke ( xs, x)
87- | Join( x, y) -> walk ( y:: rights) x xs
83+ | Nil -> finish rights xs
84+ | Unit x -> finish rights <| f.Invoke ( xs, x)
85+ | Join ( x, y) -> walk ( y:: rights) x xs
8886 and finish rights xs =
8987 match rights with
9088 | [] -> xs
9189 | t:: ts -> walk ts t xs
9290 walk [] l.dc state
9391
94- static member private tryFindi ( f : ( int -> 'T -> bool )) ( l : DList < 'T >) =
95- let f = OptimizedClosures.FSharpFunc<_,_, _>. Adapt f
92+ static member private tryFindi ( f : ( int -> 'T -> bool )) ( l : DList < 'T >) =
93+ let f = OptimizedClosures.FSharpFunc<_, _, _>. Adapt f
9694 let rec walk rights l i =
9795 match l with
98- | Nil -> finish rights i
99- | Unit x ->
100- if f.Invoke ( i, x) then
101- Some x
102- else
103- finish rights ( i+ 1 )
104- | Join( x, y) -> walk ( y:: rights) x i
96+ | Nil -> finish rights i
97+ | Unit x ->
98+ if f.Invoke ( i, x) then Some x
99+ else finish rights ( i + 1 )
100+ | Join ( x, y) -> walk ( y:: rights) x i
105101 and finish rights xs =
106102 match rights with
107103 | [] -> None
108104 | t:: ts -> walk ts t xs
109105 walk [] l.dc 0
110106 static member private findi ( f : ( int -> 'T -> bool )) ( l : DList < 'T >) =
111- match DList.tryFindi f l with | Some v -> v | None -> raise ( System.Collections.Generic. KeyNotFoundException ())
107+ match DList.tryFindi f l with Some v -> v | None -> raise ( KeyNotFoundException ())
112108
113109 static member append ( left , right ) =
114110 match left, right with
@@ -161,40 +157,40 @@ type DList<'T> (length: int, data: DListData<'T>) =
161157 member this.TryTail =
162158 let rec step ( xs : DListData < 'T >) ( acc : DListData < 'T >) =
163159 match xs with
164- | Nil -> acc | Unit _ -> acc
165- | Join ( x, y) -> step x ( DList< 'T>. append ( y, acc))
160+ | Nil | Unit _ -> acc
161+ | Join ( x, y) -> step x ( DList< 'T>. append ( y, acc))
166162 if this.IsEmpty then None
167163 else Some ( DList ( length - 1 , step data Nil))
168164
169165 /// O(log n). Returns the first element and tail.
170- member this.Uncons = ( DList< 'T>. head data, this.Tail)
166+ member this.Uncons = DList< 'T>. head data, this.Tail
171167
172168 /// O(log n). Returns option first element and tail.
173169 member this.TryUncons =
174170 match DList< 'T>. tryHead data with
175171 | Some x -> Some ( x, this.Tail)
176172 | None -> None
177173
178- member s.Item with get ( index : int ) =
179- let withIndex i _ = ( i = index)
180- if index < 0 || index >= s.Length then raise ( System.IndexOutOfRangeException ())
181- DList.findi withIndex s
174+ member s.Item
175+ with get ( index : int ) =
176+ let withIndex i _ = ( i = index)
177+ if index < 0 || index >= s.Length then raise ( System.IndexOutOfRangeException ())
178+ DList.findi withIndex s
182179
183180 member _.toSeq () =
184181 //adaptation of right-hand side of Norman Ramsey's "fold"
185182 let rec walk rights l = seq {
186183 match l with
187184 | Nil ->
188185 match rights with
189- | [] -> ()
186+ | [] -> ()
190187 | t:: ts -> yield ! walk ts t
191188 | Unit x ->
192189 yield x
193190 match rights with
194191 | [] -> ()
195192 | t:: ts -> yield ! walk ts t
196- | Join ( x, y) -> yield ! walk ( y:: rights) x}
197-
193+ | Join ( x, y) -> yield ! walk ( y:: rights) x }
198194 ( walk [] data) .GetEnumerator ()
199195
200196 interface IEnumerable< 'T> with
@@ -207,13 +203,13 @@ type DList<'T> (length: int, data: DListData<'T>) =
207203 member s.Item with get index = s.Item index
208204
209205 interface System.Collections.IEnumerable with
210- override s.GetEnumerator () = ( s.toSeq () :> System.Collections.IEnumerator)
206+ override s.GetEnumerator () = ( s.toSeq () :> System.Collections.IEnumerator)
211207
212208
213209[<CompilationRepresentation( CompilationRepresentationFlags.ModuleSuffix) >]
214210module DList =
215211 /// O(1). Returns a new DList of two lists.
216- let append left right = DList< 'T>. appendLists( left, right)
212+ let append left right = DList< 'T>. appendLists ( left, right)
217213
218214 /// O(1). Returns a new DList with the element added to the beginning.
219215 let cons hd ( l : DList < 'T >) =
@@ -272,7 +268,7 @@ module DList =
272268
273269 // additions to fit F#+ :
274270 let inline map f ( x : DList < _ >) = DList.foldBack ( cons << f ) x empty
275- let concat x = DList.fold append empty x
271+ let concat x = DList.fold append empty x
276272 let inline ap f x = concat <| map ( fun y -> map ((|>) y) f) x
277273 let inline bind m k = DList.foldBack ( append << k) empty m
278274
@@ -283,13 +279,13 @@ type DList<'T> with
283279 static member (<|>) ( x : DList < _ >, y : DList < _ >) = DList.append x y
284280
285281 [<EditorBrowsable( EditorBrowsableState.Never) >]
286- static member ToSeq x = DList.toSeq x
282+ static member ToSeq x = DList.toSeq x
287283
288284 [<EditorBrowsable( EditorBrowsableState.Never) >]
289285 static member ToList x = DList.toList x
290286
291287 [<EditorBrowsable( EditorBrowsableState.Never) >]
292- static member OfSeq x = DList.ofSeq x
288+ static member OfSeq x = DList.ofSeq x
293289
294290 [<EditorBrowsable( EditorBrowsableState.Never) >]
295291 static member Fold ( x , f , z ) = DList.fold f x z
@@ -301,4 +297,4 @@ type DList<'T> with
301297 static member Map ( x , f ) = DList.map f x
302298
303299 static member (<*>) ( f , x ) = DList.ap f x
304- static member (>>= ) ( x, f) = DList.bind x f
300+ static member (>>= ) ( x, f) = DList.bind x f
0 commit comments