-
Notifications
You must be signed in to change notification settings - Fork 85
Suggestion: Cmd.flatten : Cmd<Cmd<'a>> -> Cmd<'a> #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this something that can be found in elm? In many cases this lib is
following elm and if it's already there then you have a good argument to
add it here.
John Wostenberg <notifications@github.com> schrieb am Mi., 29. Juli 2020,
07:37:
… Description
May I suggest that Elmish have a Cmd.flatten function, which is the
classic monadic flatten operation. Here is a complete implementation:
let flatten (cmd: Cmd<Cmd<'a>>) : Cmd<'a> =
[
for (sub: ((Cmd<'a> -> unit) -> unit)) in cmd do
(fun (dispatch: ('a -> unit)) ->
let dispatchCmd : Dispatch<Cmd<'a>> = fun (msg: Cmd<'a>) ->
for sub in msg do
sub dispatch
sub dispatchCmd
)
]
Most recently, it let me mix and match various layers of Cmds and asyncs:
let someAsyncFunction x = async { return x + 1 }
type Msg = | SomeMsg of int | AnotherMsg
let makeCmd x : Cmd<Msg> =
let work = async {
let! result = someAsyncFunction x
return Cmd.ofSub (fun dispatch ->
dispatch (SomeMsg result)
dispatch AnotherMsg
)
}
work |> Cmd.OfAsync.result |> Cmd.flatten
I can easily submit a PR for this with the above code; I just wanted to
know if you would be likely to take it.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#208>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAOANEQZ6UJFIZYMT527U3R56YQ7ANCNFSM4PLHDBDA>
.
|
@forki I don't think Elm has this kind of helpers in it. If I understand correctly your example. You are generating new I think the standard approach is in general, to make this happen in the I am mentioning it because I don't know if creating |
@MangelMaxime you are correct -- however for the case that I based the example off of, it's really more of a situation where I wanted to dispatch multiple messages from inside the async. I could have alternatively written it manually like so (haven't checked this code in a compiler but it should be mostly right): let someAsyncFunction x = async { return x + 1 }
type Msg = | SomeMsg of int | AnotherMsg
let makeCmd x : Cmd<Msg> =
Cmd.ofSub (fun dispatch ->
Async.Start <| async {
let! result = someAsyncFunction x
dispatch (SomeMsg result)
AnotherMsg
}
) But I didn't really like the explicit |
@jwosty Can you see if this can be reimplemented with |
Description
May I suggest that Elmish have a Cmd.flatten function, which is the classic monadic flatten operation. Here is a complete implementation:
Most recently, it let me mix and match various layers of Cmds and asyncs:
I can easily submit a PR for this with the above code; I just wanted to know if you would be likely to take it.
The text was updated successfully, but these errors were encountered: