Skip to content

Commit

Permalink
Implementation without Option
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-khritonenko committed Nov 12, 2016
1 parent 51b7eda commit 8a69b63
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions Libs/Hopac/Stream.fs
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,20 @@ module Stream =

let ambAll (xxs: Stream<#Stream<_>>) = joinWith amb' xxs

let rec mergeAll' (streams: Stream<'x> option) (producer: Stream<Stream<'x>> option) : Stream<'x> =
match streams, producer with
| None, Some p ->
p ^=> function Nil -> mergeAll' None None
| Cons (x, xs) -> mergeAll' (Some x) (Some xs)
|> memo
| Some s, None ->
s ^=> function Cons (x, xs) -> consj x (mergeAll' (Some xs) producer)
| Nil -> mergeAll' None producer :> _
|> memo
| Some s, Some p ->
Alt.choose [ s ^=> function Cons (x, xs) -> consj x (mergeAll' (Some xs) producer)
| Nil -> mergeAll' None producer :> _
p ^=> function Cons (x, xs) -> mergeAll' (merge s x |> Some) (Some xs)
| Nil -> mergeAll' streams None ] |> memo
| None, None -> memo nilj

let mergeAll producer = mergeAll' None (Some producer)
let rec mergeAll' (combined: Stream<'x>) (producer: Stream<Stream<'x>>) : Stream<'x> =
combined ^=> function
| Nil -> producer >>= function
| Nil -> nilj
| Cons (x, xs) -> mergeAll' x xs :> _
| Cons (v, vs) -> consj v (mergeAll' vs producer)
<|> producer ^=> function
| Nil -> combined >>= function
| Nil -> nilj
| Cons (v, vs) -> consj v (mergeAll' vs nil)
| Cons (x, xs) -> mergeAll' (merge combined x) xs :> _
|> memo

let mergeAll producer = mergeAll' nil producer


let appendAll (xxs: Stream<#Stream<_>>) = joinWith append' xxs
Expand Down

0 comments on commit 8a69b63

Please sign in to comment.