Skip to content

Commit 1ffcb23

Browse files
committed
+ DList.pairwise
1 parent 0613076 commit 1ffcb23

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/FSharpPlus/Data/DList.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)