fsharp / fsharp/fslang-suggestions
Add MergeSources and/or BindN to AsyncBuilder
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Add `MergeSources` and/or `BindN` to `AsyncBuilder`
I propose we add `MergeSources` and/or `BindN` to `AsyncBuilder`.
The existing way of approaching this problem in F# is to add it yourself.
```F#
module Async =
let map f x =
async.Bind (x, f >> async.Return)
let zip (x : Async<'a>) (y : Async<'b>) : Async<'a * 'b> =
[| map box x; map box y |]
|> Async.Parallel
|> map (fun xy -> unbox xy.[0], unbox xy.[1])
type AsyncBuilder with
member _.MergeSources (x, y) = Async.zip x y
```
## Pros and Cons
The advantage of making this adjustment to F# is that
```F#
async {
let! _ = Async.Sleep 10_000
and! _ = Async.Sleep 10_000
return () }
```
runs in half the time of
```F#
async {
let! _ = Async.Sleep 10_000
let! _ = Async.Sleep 10_000
return () }
```
and making it official gives us a more compelling example of applicative CE usage, in my opinion, than the one in the release announcement.
The disadvantage of making this adjustment to F# is that some code will be added, especially if we think it's worth doing better than the simple implementation above (perhaps for an optimized implementation of `map`/`BindReturn`).
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions:
https://github.com/fsharp/fslang-suggestions/issues/859
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.