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

Signal.mapMaybe : (a -> Maybe b) -> Signal a -> Signal b #204

Closed
jhftrifork opened this Issue Mar 30, 2015 · 10 comments

Comments

Projects
None yet
5 participants
@jhftrifork
Contributor

jhftrifork commented Mar 30, 2015

I have a type Action which represents user inputs. These appear in a signal of type Signal Action. Some user inputs should result in HTTP requests being sent. For this I want to use Http.send : Signal (Request a) -> Signal (Response String). So, given my Signal Action, I want to create a Signal (Request String). Since only some actions should produce a request, a map is not appropriate. Since the actions that produce a request must be transformed into requests, a filter is not appropriate either. I need a little of both, which I would call

Signal.mapMaybe : (a -> Maybe b) -> Signal a -> Signal b

(This is modelled after Data.Maybe.mapMaybe.)

I am not aware of how to implement this myself using the Signal API. The most general function available is foldp, but I don't know how to use this. It demands an "initial state", which doesn't make sense for a signal of HTTP requests. (Elm doesn't seem to make the behavior/event distinction that some libraries do.)

Could someone with some experience help me out?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Mar 31, 2015

Contributor

You might be looking for filterMap in @Apanatshka's Signal.Extralibrary:

http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/latest/Signal-Extra#filterMap

Also, it might make its way into the 0.15 version of Elm in some form, which is currently being discussed to indeed introduce something like the behavior/event distinction you mention.

Contributor

jvoigtlaender commented Mar 31, 2015

You might be looking for filterMap in @Apanatshka's Signal.Extralibrary:

http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/latest/Signal-Extra#filterMap

Also, it might make its way into the 0.15 version of Elm in some form, which is currently being discussed to indeed introduce something like the behavior/event distinction you mention.

@jhftrifork

This comment has been minimized.

Show comment
Hide comment
@jhftrifork

jhftrifork Mar 31, 2015

Contributor

Ah, thanks, that library could be useful ... however, all of those functions still require me to provide an "initial value" for the resulting Signal. I genuinely don't know what the initial value for a signal of type Signal (Request String) should be, or could be. I've had the same problem with my Signal Action value, but I was able to hackily "solve" it by creating a NoAction : Action value, which I could use as the initial value.

In Elm currently, Http.send appears to send an initial HTTP request in situations which to me are unexpected. The lack of a behavior/event distinction appears to be the cause. Take this program:

import Signal
import Mouse
import Text
import Http

main = Signal.map Text.asText (Http.send (Signal.map (\click -> Http.get "http://elm-lang.org/") Mouse.clicks))

The behavior of this program is to send a GET request to http://elm-lang.org/ on initial page load, and every time that the mouse is clicked. The fact that it sends a request on initial page load is unexpected, at least to me. I don't actually know how to fix the bug in that program. Could someone show me how?

Out of interest, could you point me to the discussion on introducing the behavior/event distinction in Elm? Chalk me up as very much in favor of that.

Contributor

jhftrifork commented Mar 31, 2015

Ah, thanks, that library could be useful ... however, all of those functions still require me to provide an "initial value" for the resulting Signal. I genuinely don't know what the initial value for a signal of type Signal (Request String) should be, or could be. I've had the same problem with my Signal Action value, but I was able to hackily "solve" it by creating a NoAction : Action value, which I could use as the initial value.

In Elm currently, Http.send appears to send an initial HTTP request in situations which to me are unexpected. The lack of a behavior/event distinction appears to be the cause. Take this program:

import Signal
import Mouse
import Text
import Http

main = Signal.map Text.asText (Http.send (Signal.map (\click -> Http.get "http://elm-lang.org/") Mouse.clicks))

The behavior of this program is to send a GET request to http://elm-lang.org/ on initial page load, and every time that the mouse is clicked. The fact that it sends a request on initial page load is unexpected, at least to me. I don't actually know how to fix the bug in that program. Could someone show me how?

Out of interest, could you point me to the discussion on introducing the behavior/event distinction in Elm? Chalk me up as very much in favor of that.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Mar 31, 2015

Contributor

About the potential introduction of a behavior/event distinction: Everything on the mailing list last month mentioning "stream" and/or "varying" would be relevant. There is a lot of discussion back and forth, about naming and about semantics. At the moment, the pendulum seems to be swinging back to a situation where the distinction is not made prominent, only an optional thing for this release, but potentially introduced for real with the next release. But see for yourself:

Contributor

jvoigtlaender commented Mar 31, 2015

About the potential introduction of a behavior/event distinction: Everything on the mailing list last month mentioning "stream" and/or "varying" would be relevant. There is a lot of discussion back and forth, about naming and about semantics. At the moment, the pendulum seems to be swinging back to a situation where the distinction is not made prominent, only an optional thing for this release, but potentially introduced for real with the next release. But see for yourself:

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 31, 2015

Member

@jvoigtlaender and @Apanatshka, how come no one has made a library for this? Has someone? I think we all know how it would be done, right?

Member

evancz commented Mar 31, 2015

@jvoigtlaender and @Apanatshka, how come no one has made a library for this? Has someone? I think we all know how it would be done, right?

@Apanatshka

This comment has been minimized.

Show comment
Hide comment
@Apanatshka

Apanatshka Mar 31, 2015

Made a library for what? Signal utility functions are already in elm-signal-extra.

Apanatshka commented Mar 31, 2015

Made a library for what? Signal utility functions are already in elm-signal-extra.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 31, 2015

Member

For streams/events. We all know that we can model a thing with no default value with Signal (Maybe a) where it starts as Nothing and then is Just on every event. Why has no one made a library that exposes an API for streams that uses this fact? I guess I have not been encouraging of such things?

Member

evancz commented Mar 31, 2015

For streams/events. We all know that we can model a thing with no default value with Signal (Maybe a) where it starts as Nothing and then is Just on every event. Why has no one made a library that exposes an API for streams that uses this fact? I guess I have not been encouraging of such things?

@Apanatshka

This comment has been minimized.

Show comment
Hide comment
@Apanatshka

Apanatshka Mar 31, 2015

I had that planned for elm-signal-extra. But @rehno-lindeque beat me to it with his "deferred" signals experiment. I don't think it was fully developed though, so I can probably add a simple steam-ish thing to elm-signal-extra.

Apanatshka commented Mar 31, 2015

I had that planned for elm-signal-extra. But @rehno-lindeque beat me to it with his "deferred" signals experiment. I don't think it was fully developed though, so I can probably add a simple steam-ish thing to elm-signal-extra.

@jhftrifork

This comment has been minimized.

Show comment
Hide comment
@jhftrifork

jhftrifork Mar 31, 2015

Contributor

@jvoigtlaender thanks for the links. Interesting. It looks like you guys are serious about introducing the behavior/event distinction. (Btw, I prefer the "varying" terminology. "Behavior" is a terrible choice.) I peeked at the newer HTTP library; it looks much more mature. I hope it becomes usable ASAP, given that HTTP is fundamental to most web applications (!).

Contributor

jhftrifork commented Mar 31, 2015

@jvoigtlaender thanks for the links. Interesting. It looks like you guys are serious about introducing the behavior/event distinction. (Btw, I prefer the "varying" terminology. "Behavior" is a terrible choice.) I peeked at the newer HTTP library; it looks much more mature. I hope it becomes usable ASAP, given that HTTP is fundamental to most web applications (!).

@rehno-lindeque

This comment has been minimized.

Show comment
Hide comment
@rehno-lindeque

rehno-lindeque Apr 1, 2015

Contributor

Oh by the way, please don't use the deferred signals library :) I'd like to remove it from elm-packages now that elm's semantics are progressing.

I haven't been able to follow any of the work that is going on very closely (so, sorry if this is a naive response), but the opinion I settled on the last time that I thought about this was that "foldl" semantics everywhere with Maybe's for events would be the best thing. https://groups.google.com/d/msg/elm-discuss/o12cuZbEv4M/uuyBmfpOOMIJ

I came to this because I felt that there's almost always special behavior (spinners on the screen etc) associated with the "uninitiated / unloaded" state of something in bigger apps. I suspect that counting mouse clicks, for example, is actually not representative of what people will use signals for.

Contributor

rehno-lindeque commented Apr 1, 2015

Oh by the way, please don't use the deferred signals library :) I'd like to remove it from elm-packages now that elm's semantics are progressing.

I haven't been able to follow any of the work that is going on very closely (so, sorry if this is a naive response), but the opinion I settled on the last time that I thought about this was that "foldl" semantics everywhere with Maybe's for events would be the best thing. https://groups.google.com/d/msg/elm-discuss/o12cuZbEv4M/uuyBmfpOOMIJ

I came to this because I felt that there's almost always special behavior (spinners on the screen etc) associated with the "uninitiated / unloaded" state of something in bigger apps. I suspect that counting mouse clicks, for example, is actually not representative of what people will use signals for.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Feb 4, 2016

Contributor

I'm closing this issue, since there seems to be nothing actionable to it anymore. (Interesting discussion, but that has served its purpose.)

Contributor

jvoigtlaender commented Feb 4, 2016

I'm closing this issue, since there seems to be nothing actionable to it anymore. (Interesting discussion, but that has served its purpose.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment