Skip to content

Commit b31126c

Browse files
committed
add part5
1 parent bfb0532 commit b31126c

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
62.8 KB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<!-- Enable the restore command to run before builds -->
6+
<RestorePackages Condition=" '$(RestorePackages)' == '' ">true</RestorePackages>
7+
<PaketToolsPath>$(MSBuildThisFileDirectory)</PaketToolsPath>
8+
<PaketRootPath>$(MSBuildThisFileDirectory)..\</PaketRootPath>
9+
<PaketLockFilePath>$(PaketRootPath)paket.lock</PaketLockFilePath>
10+
<PaketRestoreCacheFile>$(PaketRootPath)paket-files\paket.restore.cached</PaketRestoreCacheFile>
11+
<MonoPath Condition="'$(MonoPath)' == '' And Exists('/Library/Frameworks/Mono.framework/Commands/mono')">/Library/Frameworks/Mono.framework/Commands/mono</MonoPath>
12+
<MonoPath Condition="'$(MonoPath)' == ''">mono</MonoPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<!-- Paket command -->
17+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND Exists('$(PaketRootPath)paket.exe')">$(PaketRootPath)paket.exe</PaketExePath>
18+
<PaketExePath Condition=" '$(PaketExePath)' == '' ">$(PaketToolsPath)paket.exe</PaketExePath>
19+
<PaketCommand Condition=" '$(OS)' == 'Windows_NT'">"$(PaketExePath)"</PaketCommand>
20+
<PaketCommand Condition=" '$(OS)' != 'Windows_NT' ">$(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)"</PaketCommand>
21+
</PropertyGroup>
22+
23+
<Choose> <!-- MyProject.fsproj.paket.references has the highest precedence -->
24+
<When Condition="Exists('$(MSBuildProjectFullPath).paket.references')">
25+
<PropertyGroup>
26+
<PaketReferences>$(MSBuildProjectFullPath).paket.references</PaketReferences>
27+
</PropertyGroup>
28+
</When> <!-- MyProject.paket.references -->
29+
<When Condition="Exists('$(MSBuildProjectDirectory)\$(MSBuildProjectName).paket.references')">
30+
<PropertyGroup>
31+
<PaketReferences>$(MSBuildProjectDirectory)\$(MSBuildProjectName).paket.references</PaketReferences>
32+
</PropertyGroup>
33+
</When> <!-- paket.references -->
34+
<When Condition="Exists('$(MSBuildProjectDirectory)\paket.references')">
35+
<PropertyGroup>
36+
<PaketReferences>$(MSBuildProjectDirectory)\paket.references</PaketReferences>
37+
</PropertyGroup>
38+
</When> <!-- Set to empty if a reference file isn't found matching one of the 3 format options -->
39+
<Otherwise>
40+
<PropertyGroup>
41+
<PaketReferences></PaketReferences>
42+
</PropertyGroup>
43+
</Otherwise>
44+
</Choose>
45+
46+
<PropertyGroup>
47+
<!-- Commands -->
48+
<RestoreCommand>$(PaketCommand) restore --references-file "$(PaketReferences)"</RestoreCommand>
49+
<!-- We need to ensure packages are restored prior to assembly resolve -->
50+
<BuildDependsOn Condition="$(RestorePackages) == 'true'">RestorePackages; $(BuildDependsOn);</BuildDependsOn>
51+
</PropertyGroup>
52+
<Target Name="RestorePackages">
53+
<PropertyGroup>
54+
<PaketRestoreRequired>true</PaketRestoreRequired>
55+
</PropertyGroup>
56+
57+
<PropertyGroup Condition="Exists('$(PaketRestoreCacheFile)') ">
58+
<PaketRestoreCachedHash>$([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)'))</PaketRestoreCachedHash>
59+
<PaketRestoreLockFileHash>$([System.IO.File]::ReadAllText('$(PaketLockFilePath)'))</PaketRestoreLockFileHash>
60+
<PaketRestoreRequired>true</PaketRestoreRequired>
61+
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '$(PaketRestoreCachedHash)' ">false</PaketRestoreRequired>
62+
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '' ">true</PaketRestoreRequired>
63+
</PropertyGroup>
64+
65+
<Exec Command="$(RestoreCommand)"
66+
IgnoreStandardErrorWarningFormat="true"
67+
WorkingDirectory="$(PaketRootPath)"
68+
ContinueOnError="false"
69+
Condition=" '$(PaketRestoreRequired)' == 'true' AND Exists('$(PaketReferences)') AND '$(PaketReferences)' != '' "
70+
/>
71+
</Target>
72+
</Project>

fsharp/HopacSeries/Part5/ivar.fsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#r "packages/Hopac/lib/net45/Hopac.Core.dll"
2+
#r "packages/Hopac/lib/net45/Hopac.Platform.dll"
3+
#r "packages/Hopac/lib/net45/Hopac.dll"
4+
5+
open Hopac
6+
7+
let fillAndRead iVar value =
8+
IVar.fill iVar value
9+
|> Job.bind (fun _ -> IVar.read iVar)
10+
|> Job.map (printfn "%A")
11+
|> run
12+
13+
fillAndRead (IVar<bool>()) true
14+
fillAndRead (IVar<bool>()) false
15+
16+
let intIVar = IVar<int>()
17+
fillAndRead intIVar 42
18+
fillAndRead intIVar 42
19+
20+
let tryFillAndRead iVar value =
21+
IVar.tryFill iVar value
22+
|> Job.bind (fun _ -> IVar.read iVar)
23+
|> Job.map (printfn "%A")
24+
|> run
25+
26+
let anotherIntIVar = IVar<int>()
27+
tryFillAndRead anotherIntIVar 42
28+
tryFillAndRead anotherIntIVar 10
29+
30+
open Hopac.Infixes
31+
32+
let readOrTimeout delayInMillis iVar =
33+
let timeOutAlt =
34+
timeOutMillis delayInMillis
35+
|> Alt.afterFun (fun _ -> printfn "time out!")
36+
let readAlt =
37+
IVar.read iVar
38+
|> Alt.afterFun (printfn "%A")
39+
timeOutAlt <|> readAlt
40+
|> run
41+
42+
let yetAnotherIntIVar = IVar<int>()
43+
tryFillAndRead yetAnotherIntIVar 10
44+
readOrTimeout 1000 intIVar
45+
46+
#time "on"
47+
readOrTimeout 2000 (IVar<unit>())
48+
#time "off"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source https://www.nuget.org/api/v2
2+
framework: net461
3+
nuget Hopac
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RESTRICTION: == net461
2+
NUGET
3+
remote: https://www.nuget.org/api/v2
4+
FSharp.Core (4.3.4)
5+
Hopac (0.3.23)
6+
FSharp.Core (>= 3.1.2.5)

fsharp/HopacSeries/Part5/timer.fsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#r "packages/Hopac/lib/net45/Hopac.Core.dll"
2+
#r "packages/Hopac/lib/net45/Hopac.Platform.dll"
3+
#r "packages/Hopac/lib/net45/Hopac.dll"
4+
5+
open Hopac
6+
open Hopac.Infixes
7+
open System
8+
9+
type Ticker (timeSpan : TimeSpan) =
10+
let tickCh = Ch<DateTimeOffset>()
11+
let cancelled = IVar()
12+
13+
let tick () =
14+
Ch.give tickCh DateTimeOffset.Now
15+
16+
let rec loop () =
17+
let tickerLoop =
18+
timeOut timeSpan
19+
|> Alt.afterJob tick
20+
|> Alt.afterJob loop
21+
tickerLoop <|> IVar.read cancelled
22+
23+
do start (loop())
24+
25+
member __.Stop() =
26+
IVar.tryFill cancelled () |> start
27+
member __.C
28+
with get() = tickCh
29+
30+
let useCase1 nTimes (xSeconds : int) f =
31+
let timeSpan =
32+
xSeconds |> float |> TimeSpan.FromSeconds
33+
let ticker = new Ticker(timeSpan)
34+
let onTick = f
35+
ticker.C
36+
|> Alt.afterFun onTick
37+
|> Job.forN nTimes
38+
|> run
39+
ticker.Stop()
40+
41+
// useCase1 5 2 (printfn "%A")
42+
43+
let ticker seconds =
44+
seconds
45+
|> float
46+
|> TimeSpan.FromSeconds
47+
|> Ticker
48+
49+
let useCase2 t1Interval t2Interval xMillis =
50+
let ticker1 = ticker t1Interval
51+
let ticker2 = ticker t2Interval
52+
53+
let onTick tCh name loop =
54+
tCh
55+
|> Alt.afterFun (fun t -> printfn "[%s] at %A" name t)
56+
|> Alt.afterJob loop
57+
58+
let rec loop () =
59+
onTick ticker1.C "T1" loop <|> onTick ticker2.C "T2" loop
60+
61+
printfn "Starts at %A" (DateTimeOffset.Now)
62+
start (loop())
63+
64+
let onTimeOut _ =
65+
ticker1.Stop()
66+
ticker2.Stop()
67+
68+
timeOutMillis xMillis
69+
|> Alt.afterFun onTimeOut
70+
|> run
71+
printfn "Ends at %A" (DateTimeOffset.Now)
72+

0 commit comments

Comments
 (0)