Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

unzip and uneither #59

Open
ziggystar opened this issue Sep 30, 2012 · 6 comments
Open

unzip and uneither #59

ziggystar opened this issue Sep 30, 2012 · 6 comments

Comments

@ziggystar
Copy link

For my own project, I had to implement those two methods. I did it on EventStream by the code below. I'm very new to reactive and don't know where to put it in the library, otherwise I would file a pull request.

  implicit def eventStreamRich[T](es: EventStream[T]) = new {

    def unzip[A,B](implicit ev: T <:< (A,B)): (EventStream[A],EventStream[B]) =
      (es.map(t => ev(t)._1), es.map(t => ev(t)._2))

    def uneither[A,B](implicit ev: T <:< Either[A,B]): (EventStream[A],EventStream[B]) = (
        es.collect{case t if(ev(t).isLeft) => ev(t).left.get},
        es.collect{case t if(ev(t).isRight) => ev(t).right.get}
      )

  }
@nafg
Copy link
Owner

nafg commented Sep 30, 2012

Nice!
The place to put it is in trait EventStream, in reactive-core/src/main/scala/reactive/EventStream.scala. Even though most of its methods are abstract, and implemented in EventSource, in this case it's clearly defined in terms of other methods, so it belongs in EventStream.
Please include scaladoc comments, and add a section in reactive-web-demo/src/main/webapp/core/EventStream.html too.
Bonus points if you do unzip for Signal too (I think it currently lacks filter, thus collect as well, so that rules out uneither --- unless you want to contribute those too!).

@dylemma
Copy link
Contributor

dylemma commented Apr 18, 2013

I would like to contribute these, but first two questions:

  • What is the desired behavior for a filtered Signal? I assume that it would simply not change values when its parent signal changes to a value that the filter does not accept. Maybe other people have other ideas though.
  • I currently have an open pull request for an unrelated thing. Should I just continue pushing onto my fork (and thereby adding to that pull request) or is there some other thing I should do instead?

@nafg
Copy link
Owner

nafg commented Apr 18, 2013

On Wed, Apr 17, 2013 at 8:13 PM, Dylan Halperin [email protected]:

I would like to contribute these, but first two questions:

  • What is the desired behavior for a filtered Signal? I assume that it
    would simply not change values when its parent signal changes to a value
    that the filter does not accept. Maybe other people have other ideas though.

That's one possibility. Another is to use an architecture similar to
bacon.js, where behind the scenes (lower-level API) events are not raw
values but an ADT of Event[A]. Then a Signal could not have a value. I want
to adopt that, but I think in either case filter should do what you
described.

  • I currently have an open pull request for an unrelated thing. Should
    I just continue pushing onto my fork (and thereby adding to that pull
    request) or is there some other thing I should do instead?

Why don't you create a new branch on your fork?


Reply to this email directly or view it on GitHubhttps://github.com//issues/59#issuecomment-16543724
.

@ritschwumm
Copy link

as long as a signal always has a value, you cannot filter it.
what if the predicate doesn't apply to the initial value?

@dylemma
Copy link
Contributor

dylemma commented Apr 18, 2013

A few possibilities arise from this.

Filtered stream ignores unaccepted changes

[parent] 1---2---3---4---5---
[evens ] ----2-------4-------

But this is bad because a) it leaves the filtered stream with the possibility of having no initial value and b) it doesn't make a ton of sense in terms of signals.

Filtering a stream results in a Signalof Options

def filter(f: T => Boolean): Signal[Option[T]]

This kind of works, but it's different from the usual semantics of filter, and that could be confusing.

Signal's value is always optional
Where there exists some ADT for the signal's now, along the lines of

sealed trait State[+T]
case object Undefined extends State[Nothing]
case class Defined[T](value: T) extends State[T]

And where a filtered signal's parent changed to a value that its filter didn't accept, its own value would go to Undefined

dylemma added a commit to dylemma/reactive that referenced this issue Apr 25, 2013
Also added some documentation of them in the EventStream page of the
demo.
@nafg
Copy link
Owner

nafg commented Jun 5, 2013

Is this issue superseded by #67?
@dylemma, is the discussion about Signal#filter captured somewhere else? It should be on its own issue.
Thanks.

nafg pushed a commit that referenced this issue Jun 5, 2013
Also added some documentation of them in the EventStream page of the
demo.
nafg added a commit that referenced this issue Jun 5, 2013
* v0.3.2:
  Bump version to 0.3.2
  demo: Fix instructions in readme
  Update getting started instructions.
  Added `unzip` and `uneither` methods to EventStream (Issue #59). Also added some documentation of them in the EventStream page of the demo.
  ignore .cache files that get generated by sbt
  Fixed scaladoc's source paths/github links
  web: Ajax requests should not use RequestVar or lift's ajax mechanism
  Remove refernce to old contact methods
  Remove deprecated Reactions.initComet
  web: Make Reactions a trait (singleton now extends it)
  Set version to 0.3.2-SNAPSHOT
@dylemma dylemma mentioned this issue Jun 5, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants