Skip to content

Commit 2e2f432

Browse files
committed
+ pmap2 and pmap3 to Async, Task and ValueTask
1 parent 3e7a328 commit 2e2f432

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/FSharpPlus/Extensions/Async.fs

+37
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,43 @@ module Async =
3131
let! c = z
3232
return f a b c}
3333

34+
/// <summary>Creates an async workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
35+
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
36+
/// <param name="f">The mapping function.</param>
37+
/// <param name="x">First async workflow.</param>
38+
/// <param name="y">Second async workflow.</param>
39+
#if FABLE_COMPILER
40+
let pmap2 f x y = map2 f x y
41+
#else
42+
let pmap2 f x y = async {
43+
let! ct = Async.CancellationToken
44+
let x = Async.StartImmediateAsTask (x, ct)
45+
let y = Async.StartImmediateAsTask (y, ct)
46+
let! x' = Async.AwaitTask x
47+
let! y' = Async.AwaitTask y
48+
return f x' y' }
49+
#endif
50+
51+
/// <summary>Creates an async workflow from three workflows 'x', 'y' and 'z', mapping its results with 'f'.</summary>
52+
/// <remarks>Similar to map3 but workflows are run in parallel.</remarks>
53+
/// <param name="f">The mapping function.</param>
54+
/// <param name="x">First async workflow.</param>
55+
/// <param name="y">Second async workflow.</param>
56+
/// <param name="z">third async workflow.</param>
57+
#if FABLE_COMPILER
58+
let pmap3 f x y z = map3 f x y z
59+
#else
60+
let pmap3 f x y z = async {
61+
let! ct = Async.CancellationToken
62+
let x = Async.StartImmediateAsTask (x, ct)
63+
let y = Async.StartImmediateAsTask (y, ct)
64+
let z = Async.StartImmediateAsTask (z, ct)
65+
let! x' = Async.AwaitTask x
66+
let! y' = Async.AwaitTask y
67+
let! z' = Async.AwaitTask z
68+
return f x' y' z' }
69+
#endif
70+
3471
/// <summary>Creates an async workflow from two workflows 'x' and 'y', tupling its results.</summary>
3572
let zip x y = async {
3673
let! a = x

src/FSharpPlus/Extensions/Task.fs

+22
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ module Task =
134134
) |> ignore) |> ignore) |> ignore
135135
tcs.Task
136136

137+
/// <summary>Creates a task workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
138+
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
139+
/// <param name="f">The mapping function.</param>
140+
/// <param name="x">First task workflow.</param>
141+
/// <param name="y">Second task workflow.</param>
142+
let pmap2 f x y = task {
143+
let! x' = x
144+
let! y' = y
145+
return f x' y' }
146+
147+
/// <summary>Creates a task workflow from three workflows 'x', 'y' and z, mapping its results with 'f'.</summary>
148+
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
149+
/// <param name="f">The mapping function.</param>
150+
/// <param name="x">First task workflow.</param>
151+
/// <param name="y">Second task workflow.</param>
152+
/// <param name="z">Third task workflow.</param>
153+
let pmap3 f x y z = task {
154+
let! x' = x
155+
let! y' = y
156+
let! z' = z
157+
return f x' y' z' }
158+
137159
/// <summary>Creates a task workflow that is the result of applying the resulting function of a task workflow
138160
/// to the resulting value of another task workflow</summary>
139161
/// <param name="f">Task workflow returning a function</param>

src/FSharpPlus/Extensions/ValueTask.fs

+29
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ module ValueTask =
6262
with e -> tcs.SetException e)))
6363
tcs.Task |> ValueTask<'W>
6464

65+
/// <summary>Creates a task workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
66+
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
67+
/// <param name="f">The mapping function.</param>
68+
/// <param name="x">First ValueTask workflow.</param>
69+
/// <param name="y">Second ValueTask workflow.</param>
70+
/// <param name="z">Third ValueTask workflow.</param>
71+
let pmap2 (f: 'T -> 'U -> 'V) (x: ValueTask<'T>) (y: ValueTask<'U>) : ValueTask<'V> =
72+
task {
73+
let! x' = x
74+
let! y' = y
75+
return f x' y'
76+
}
77+
|> ValueTask<'V>
78+
79+
/// <summary>Creates a ValueTask workflow from three workflows 'x', 'y' and z, mapping its results with 'f'.</summary>
80+
/// <remarks>Similar to map3 but workflows are run in parallel.</remarks>
81+
/// <param name="f">The mapping function.</param>
82+
/// <param name="x">First ValueTask workflow.</param>
83+
/// <param name="y">Second ValueTask workflow.</param>
84+
/// <param name="z">Third ValueTask workflow.</param>
85+
let pmap3 (f: 'T -> 'U -> 'V -> 'W) (x: ValueTask<'T>) (y: ValueTask<'U>) (z: ValueTask<'V>) : ValueTask<'W> =
86+
task {
87+
let! x' = x
88+
let! y' = y
89+
let! z' = z
90+
return f x' y' z'
91+
}
92+
|> ValueTask<'W>
93+
6594
/// <summary>Creates a ValueTask workflow that is the result of applying the resulting function of a ValueTask workflow
6695
/// to the resulting value of another ValueTask workflow</summary>
6796
/// <param name="f">ValueTask workflow returning a function</param>

0 commit comments

Comments
 (0)