Skip to content

Commit 10af18a

Browse files
committed
Add Task related helper functions
1 parent e2f86ae commit 10af18a

25 files changed

Lines changed: 116 additions & 47 deletions

FSharpx.Extras.Tests/FSharpx.Extras.Tests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<OutputType>Library</OutputType>
99
<RootNamespace>FSharpx.Extras.Tests</RootNamespace>
1010
<AssemblyName>FSharpx.Extras.Tests</AssemblyName>
11-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1212
<TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
1313
<Name>FSharpx.Extras.Tests</Name>
1414
<TargetFrameworkProfile />

FSharpx.Extras.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{50709973-279A-4DF0-AF26-8C7535C7C9AE}"
77
ProjectSection(SolutionItems) = preProject
88
paket.dependencies = paket.dependencies
9+
paket.lock = paket.lock
910
EndProjectSection
1011
EndProject
1112
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{722621A6-FA45-4129-8B8D-94880DCD7971}"
@@ -17,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1718
README.md = README.md
1819
src\FSharpx.Extras\FSharpx.Extras.paket.template = src\FSharpx.Extras\FSharpx.Extras.paket.template
1920
src\FSharpx.Text.StructuredFormat\FSharpx.Text.StructuredFormat.paket.template = src\FSharpx.Text.StructuredFormat\FSharpx.Text.StructuredFormat.paket.template
21+
.gitignore = .gitignore
2022
EndProjectSection
2123
EndProject
2224
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpx.Tests", "tests\FSharpx.Tests\FSharpx.Tests.fsproj", "{26D9D3EE-E882-43E6-A79E-5D1D89E92C4F}"

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build_script:
2-
- cmd: build.cmd
2+
- cmd: build.cmd CI
33
test: off
44
version: 0.0.1.{build}
55
artifacts:

build.fsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open System.IO
99

1010
// .NET Frameworks
1111
let net40 = "v4.0"
12+
let net45 = "v4.5"
1213

1314
// directories
1415
let buildDir = "./bin"
@@ -36,7 +37,7 @@ let gitName = "FSharpx.Extras"
3637
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
3738
let release = parseReleaseNotes (File.ReadAllLines "RELEASE_NOTES.md")
3839

39-
let fxVersions = [net40]
40+
let fxVersions = [net45]
4041

4142
let normalizeFrameworkVersion fxVersion =
4243
let v = ("[^\\d]" >=> "") fxVersion
@@ -54,8 +55,12 @@ let nunitPath = packagesDir @@ "NUnit.Runners/tools"
5455

5556

5657
// targets
57-
Target "Clean" (fun _ ->
58-
CleanDirs [buildDir]
58+
Target "Clean" (fun _ ->
59+
for fxVersion in fxVersions do
60+
!! "*.sln"
61+
|> MSBuild (buildDirVer fxVersion) "Clean"(["Configuration","Release"] @ buildLibParams fxVersion)
62+
|> ignore
63+
CleanDirs [buildDir; packagesDir; "docs/output"]
5964
)
6065

6166

@@ -88,12 +93,12 @@ Target "Build" (fun _ ->
8893
for fxVersion in fxVersions do
8994
// Only generate tests for net40
9095
!! "*.sln"
91-
|> MSBuild (buildDirVer fxVersion) "Rebuild" (["Configuration","Release"] @ buildLibParams fxVersion)
96+
|> MSBuild (buildDirVer fxVersion) "Build" (["Configuration","Release"] @ buildLibParams fxVersion)
9297
|> ignore)
9398

9499
Target "Test" (fun _ ->
95100
ActivateFinalTarget "CloseTestRunner"
96-
for fxVersion in [net40] do
101+
for fxVersion in [net45] do
97102
printfn "buildDirVer fxVersion = %s" (buildDirVer fxVersion)
98103
!! (buildDirVer fxVersion @@ "*.Tests.dll")
99104
|> NUnit (fun p ->
@@ -123,7 +128,7 @@ Target "GenerateDocs" (fun _ ->
123128
// Release Scripts
124129

125130
Target "ReleaseDocs" (fun _ ->
126-
let tempDocsDir = "temp/gh-pages"
131+
let tempDocsDir = "bin/gh-pages"
127132
if not (Directory.Exists tempDocsDir) then
128133
Repository.cloneSingleBranch "" (gitHome + "/" + gitName + ".git") "gh-pages" tempDocsDir
129134

@@ -144,8 +149,7 @@ Target "Release" DoNothing
144149
Target "CI" DoNothing
145150

146151
// Build order
147-
"Clean"
148-
==> "AssemblyInfo"
152+
"AssemblyInfo"
149153
==> "Build"
150154
==> "Test"
151155

docs/tools/generate.fsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@ let info =
2222
// For typical project, no changes are needed below
2323
// --------------------------------------------------------------------------------------
2424

25+
#load "../../packages/FSharp.Formatting/FSharp.Formatting.fsx"
2526
#I "../../packages/FAKE/tools/"
26-
#I "../../packages/FSharp.Formatting/lib/net40"
27-
#I "../../packages/RazorEngine/lib/net40"
28-
#I "../../packages/FSharp.Compiler.Service/lib/net40"
29-
#r "../../packages/Microsoft.AspNet.Razor/lib/net40/System.Web.Razor.dll"
30-
#r "../../packages/FAKE/tools/FakeLib.dll"
31-
#r "RazorEngine.dll"
32-
#r "FSharp.Literate.dll"
33-
#r "FSharp.CodeFormat.dll"
34-
#r "FSharp.MetadataFormat.dll"
27+
#r "FakeLib.dll"
3528
open Fake
3629
open System.IO
3730
open Fake.FileHelper
@@ -47,7 +40,7 @@ let root = "file://" + (__SOURCE_DIRECTORY__ @@ "../output")
4740
#endif
4841

4942
// Paths with template/source/output locations
50-
let bin = __SOURCE_DIRECTORY__ @@ "../../bin/v4.0"
43+
let bin = __SOURCE_DIRECTORY__ @@ "../../bin/v4.5"
5144
let content = __SOURCE_DIRECTORY__ @@ "../content"
5245
let output = __SOURCE_DIRECTORY__ @@ "../output"
5346
let files = __SOURCE_DIRECTORY__ @@ "../files"
@@ -77,7 +70,9 @@ let buildReference () =
7770
sourceRepo = githubLink @@ "tree/master",
7871
sourceFolder = __SOURCE_DIRECTORY__ @@ ".." @@ "..",
7972
publicOnly = true,
80-
otherFlags=["-r:" + bin @@ "FSharpx.Collections.dll"] )
73+
otherFlags=["-r:" + bin @@ "FSharpx.Collections.dll"]
74+
, xmlFile = __SOURCE_DIRECTORY__ @@ "../../bin/FSharpx.Extras.xml"
75+
)
8176

8277
// Build documentation from `fsx` and `md` files in `docs/content`
8378
let buildDocumentation () =

packages/repositories.config

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/FSharpx.Extras/CSharpCompat.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ open FSharpx.Collections
99
open FSharpx.Functional
1010
open FSharpx.Control
1111
open FSharpx
12-
12+
open Microsoft.FSharp.Control.WebExtensions
1313

1414
[<assembly:Extension>]
1515
do()

src/FSharpx.Extras/ComputationExpressions/Monad.fs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ module Task =
997997

998998
/// Sequence actions, discarding the value of the second argument.
999999
let inline ( <*) a b = lift2 (fun z _ -> z) a b
1000+
1001+
let foldM f s =
1002+
Seq.fold (fun acc t -> acc >>= (flip f) t) (returnM s)
1003+
1004+
let inline sequence (s:Task<'a> list) =
1005+
let inline cons a b = lift2 List.cons a b
1006+
List.foldBack cons s (returnM [])
1007+
1008+
let inline mapM f x = sequence (List.map f x)
10001009

10011010
type TaskBuilder(?continuationOptions, ?scheduler, ?cancellationToken) =
10021011
let contOptions = defaultArg continuationOptions TaskContinuationOptions.None
@@ -1043,6 +1052,8 @@ module Task =
10431052

10441053
member this.Run (f: unit -> Task<'T>) = f()
10451054

1055+
let task = TaskBuilder()
1056+
10461057
type TaskBuilderWithToken(?continuationOptions, ?scheduler) =
10471058
let contOptions = defaultArg continuationOptions TaskContinuationOptions.None
10481059
let scheduler = defaultArg scheduler TaskScheduler.Default
@@ -1085,3 +1096,47 @@ module Task =
10851096

10861097
member this.Delay f = this.Bind(this.Return (), f)
10871098

1099+
/// Converts a Task into Task<unit>
1100+
let ToTaskUnit (t:Task) =
1101+
let continuation _ = ()
1102+
t.ContinueWith continuation
1103+
1104+
/// Creates a task that runs the given task and ignores its result.
1105+
let inline Ignore t = bind (fun _ -> returnM ()) t
1106+
1107+
/// Creates a task that executes a specified task.
1108+
/// If this task completes successfully, then this function returns Choice1Of2 with the returned value.
1109+
/// If this task raises an exception before it completes then return Choice2Of2 with the raised exception.
1110+
let Catch (t:Task<'a>) =
1111+
task {
1112+
try let! r = t
1113+
return Choice1Of2 r
1114+
with e ->
1115+
return Choice2Of2 e
1116+
}
1117+
1118+
#if !NET40
1119+
/// Creates a task that executes all the given tasks.
1120+
let Parallel (tasks : seq<unit -> Task<'a>>) =
1121+
tasks
1122+
|> Seq.map (fun t -> t())
1123+
|> Array.ofSeq
1124+
|> Task.WhenAll
1125+
1126+
/// Creates a task that executes all the given tasks.
1127+
/// The paralelism is throttled, so that at most `throttle` tasks run at one time.
1128+
let ParallelWithTrottle throttle (tasks : seq<unit -> Task<'a>>) : (Task<'a[]>) =
1129+
let semaphore = new SemaphoreSlim(throttle)
1130+
let throttleTask (t:unit->Task<'a>) () : Task<'a> =
1131+
task {
1132+
do! semaphore.WaitAsync() |> ToTaskUnit
1133+
let! result = Catch <| t()
1134+
semaphore.Release() |> ignore
1135+
return match result with
1136+
| Choice1Of2 r -> r
1137+
| Choice2Of2 e -> raise e
1138+
}
1139+
tasks
1140+
|> Seq.map throttleTask
1141+
|> Parallel
1142+
#endif

src/FSharpx.Extras/FSharpx.Extras.fsproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<OutputType>Library</OutputType>
1010
<RootNamespace>FSharpx</RootNamespace>
1111
<AssemblyName>FSharpx.Extras</AssemblyName>
12-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
1414
<Name>FSharpx.Extras</Name>
1515
<TargetFrameworkProfile>
@@ -55,6 +55,8 @@
5555
</PropertyGroup>
5656
<Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />
5757
<ItemGroup>
58+
<Content Include="paket.references" />
59+
<Content Include="app.config" />
5860
<Compile Include="AssemblyInfo.fs" />
5961
<Compile Include="Prelude.fs" />
6062
<Compile Include="Pluralizer.fs" />
@@ -74,8 +76,6 @@
7476
<Compile Include="Stm.fs" />
7577
<Compile Include="Iteratee.fs" />
7678
<Compile Include="CSharpCompat.fs" />
77-
<Content Include="paket.references" />
78-
<Content Include="app.config" />
7979
<Compile Include="Net.fs" />
8080
<Compile Include="Conneg.fs" />
8181
</ItemGroup>

src/FSharpx.Extras/FSharpx.Extras.paket.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ dependencies
2929
FSharpx.Async ~> LOCKEDVERSION
3030
FSharpx.Collections ~> LOCKEDVERSION
3131
files
32-
../../bin/v4.0/FSharpx.Extras.dll ==> lib/40
33-
../../bin/FSharpx.Extras.xml ==> lib/40
32+
../../bin/v4.5/FSharpx.Extras.dll ==> lib/net45
33+
../../bin/FSharpx.Extras.xml ==> lib/net45
3434
../../LICENSE.md ==> .

0 commit comments

Comments
 (0)