-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.3-fibonacci-series.fsx
executable file
·69 lines (51 loc) · 1.4 KB
/
3.3-fibonacci-series.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env fsharpi
#I "../../../.nuget/packages/hopac/0.3.21/lib/net45/"
#I "../../../.nuget/packages/hopac.extras/0.3.1/lib/net45"
#r "Hopac.Extras"
#r "Hopac.Platform"
#r "Hopac.Core"
#r "Hopac"
open System
open Hopac.Job
open Hopac
open Hopac.Infixes
open Hopac.Extensions
let rec loop f s = f s >>= delayWith (loop f)
let forever (init:'a) (f:'a->Job<'a>) =
start (loop f init)
let add inCh1 inCh2 outCh =
forever () (fun () -> job {
let! a = Ch.take inCh1
let! b = Ch.take inCh2
return! outCh *<- (a + b) })
let delay init (inCh, outCh) =
let none () =
Ch.take inCh ^=>
delayWith (fun x -> Job.result (Some x))
let some x =
outCh *<- x ^=>.
delay (fun _ -> Job.result None)
forever init (function None -> upcast (none ()) | Some x -> upcast (some x))
let copy inCh outCh1 outCh2 =
let send x =
outCh1 *<- x ^=>.
outCh2 *<- x
let take () =
Ch.take inCh >>=
send
forever () take
let mkFibNetwork () =
let outCh = Ch()
let (c1, c2, c3, c4, c5) = (Ch(), Ch(), Ch(), Ch(), Ch())
let start = 0I
delay (Some start) (c4, c5)
copy c2 c3 c4
add c3 c5 c1
copy c1 c2 outCh
run (c1 *<- 1I)
outCh
let printResult x = job { printfn "%A" x }
let length = 1000
// communication buffer
let fib = mkFibNetwork ()
run <| Job.forNIgnore length (Ch.take fib >>= printResult)