File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed
Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,20 @@ module DList =
266266 /// O(n). Returns a seq of the DList elements.
267267 let inline toSeq ( l : DList < 'T >) = l :> seq< 'T>
268268
269+ let pairwise ( source : DList < 'T >) =
270+ let (| Cons | Nil |) ( l : DList < 'T >) = match l.TryUncons with Some ( a, b) -> Cons ( a, b) | None -> Nil
271+ let rec pairWiseDListData cons lastvalue = function
272+ | Nil -> cons
273+ | Cons ( x, Nil) -> Join ( cons, Unit ( lastvalue, x))
274+ | Cons ( x, rest) -> pairWiseDListData ( Join ( cons, Unit ( lastvalue, x))) x rest
275+ let dlistData =
276+ match source with
277+ | Nil | Cons (_, Nil) -> Nil
278+ | Cons ( x, ( Cons ( y, rest))) -> pairWiseDListData ( Unit ( x, y)) y rest
279+ match source.Length with
280+ | 0 -> DList ( 0 , Nil)
281+ | _ -> DList ( source.Length - 1 , dlistData)
282+
269283 // additions to fit F#+ :
270284 let inline map f ( x : DList < _ >) = DList.foldBack ( cons << f ) x empty
271285 let concat x = DList.fold append empty x
You can’t perform that action at this time.
0 commit comments