Skip to content

Commit

Permalink
+ pmap2 and pmap3 to Async, Task and ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Jan 13, 2024
1 parent 3e7a328 commit 2e2f432
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/FSharpPlus/Extensions/Async.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,43 @@ module Async =
let! c = z
return f a b c}

/// <summary>Creates an async workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First async workflow.</param>
/// <param name="y">Second async workflow.</param>
#if FABLE_COMPILER
let pmap2 f x y = map2 f x y
#else
let pmap2 f x y = async {
let! ct = Async.CancellationToken
let x = Async.StartImmediateAsTask (x, ct)
let y = Async.StartImmediateAsTask (y, ct)
let! x' = Async.AwaitTask x
let! y' = Async.AwaitTask y
return f x' y' }
#endif

/// <summary>Creates an async workflow from three workflows 'x', 'y' and 'z', mapping its results with 'f'.</summary>
/// <remarks>Similar to map3 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First async workflow.</param>
/// <param name="y">Second async workflow.</param>
/// <param name="z">third async workflow.</param>
#if FABLE_COMPILER
let pmap3 f x y z = map3 f x y z
#else
let pmap3 f x y z = async {
let! ct = Async.CancellationToken
let x = Async.StartImmediateAsTask (x, ct)
let y = Async.StartImmediateAsTask (y, ct)
let z = Async.StartImmediateAsTask (z, ct)
let! x' = Async.AwaitTask x
let! y' = Async.AwaitTask y
let! z' = Async.AwaitTask z
return f x' y' z' }
#endif

/// <summary>Creates an async workflow from two workflows 'x' and 'y', tupling its results.</summary>
let zip x y = async {
let! a = x
Expand Down
22 changes: 22 additions & 0 deletions src/FSharpPlus/Extensions/Task.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ module Task =
) |> ignore) |> ignore) |> ignore
tcs.Task

/// <summary>Creates a task workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First task workflow.</param>
/// <param name="y">Second task workflow.</param>
let pmap2 f x y = task {
let! x' = x
let! y' = y
return f x' y' }

/// <summary>Creates a task workflow from three workflows 'x', 'y' and z, mapping its results with 'f'.</summary>
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First task workflow.</param>
/// <param name="y">Second task workflow.</param>
/// <param name="z">Third task workflow.</param>
let pmap3 f x y z = task {
let! x' = x
let! y' = y
let! z' = z
return f x' y' z' }

/// <summary>Creates a task workflow that is the result of applying the resulting function of a task workflow
/// to the resulting value of another task workflow</summary>
/// <param name="f">Task workflow returning a function</param>
Expand Down
29 changes: 29 additions & 0 deletions src/FSharpPlus/Extensions/ValueTask.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ module ValueTask =
with e -> tcs.SetException e)))
tcs.Task |> ValueTask<'W>

/// <summary>Creates a task workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
/// <remarks>Similar to map2 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First ValueTask workflow.</param>
/// <param name="y">Second ValueTask workflow.</param>
/// <param name="z">Third ValueTask workflow.</param>
let pmap2 (f: 'T -> 'U -> 'V) (x: ValueTask<'T>) (y: ValueTask<'U>) : ValueTask<'V> =
task {
let! x' = x
let! y' = y
return f x' y'
}
|> ValueTask<'V>

/// <summary>Creates a ValueTask workflow from three workflows 'x', 'y' and z, mapping its results with 'f'.</summary>
/// <remarks>Similar to map3 but workflows are run in parallel.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="x">First ValueTask workflow.</param>
/// <param name="y">Second ValueTask workflow.</param>
/// <param name="z">Third ValueTask workflow.</param>
let pmap3 (f: 'T -> 'U -> 'V -> 'W) (x: ValueTask<'T>) (y: ValueTask<'U>) (z: ValueTask<'V>) : ValueTask<'W> =
task {
let! x' = x
let! y' = y
let! z' = z
return f x' y' z'
}
|> ValueTask<'W>

/// <summary>Creates a ValueTask workflow that is the result of applying the resulting function of a ValueTask workflow
/// to the resulting value of another ValueTask workflow</summary>
/// <param name="f">ValueTask workflow returning a function</param>
Expand Down

0 comments on commit 2e2f432

Please sign in to comment.