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

flatMap implementation #61

Open
Yarikx opened this issue Feb 1, 2013 · 6 comments
Open

flatMap implementation #61

Yarikx opened this issue Feb 1, 2013 · 6 comments

Comments

@Yarikx
Copy link

Yarikx commented Feb 1, 2013

Without ability to ask directly, just create an issue.

Why implementation of flatMap(f) just 'swich' between streams provided by f?
If trying to think about EventStreams as about collections, than flatMap(f) should return EventStream that will fire all events from all streams provided by f.

I understand that current functionality is necessary, but may be it should be called 'swich'.
As for me current implementation don't fit in the for comprehension.\

p.s. please close that issue, as it isn't actually an issue.

@Yarikx
Copy link
Author

Yarikx commented Feb 1, 2013

for example that code could handle double click but it don't work with current implementation of flatMap

val doubleClicks = for{
          d1 <- downs
          t1 = System.currentTimeMillis()
          d2 <- downs.once
          t2 = System.currentTimeMillis()
          if t2-t1 <500
        } yield d2

@nafg
Copy link
Owner

nafg commented Feb 5, 2013

Hi, can you ask this on scala-user? This way we can get opinions from
people who are more familiar with the design of monads, which is what
flatMap is all about.

Thanks.

On Friday, February 1, 2013, Yaroslav wrote:

Without ability to ask directly, just create an issue.

Why implementation of flatMap(f) just 'swich' between streams provided by
f?
If trying to think about EventStreams as about collections, than
flatMap(f) should return EventStream that will fire all events from all
streams provided by f.

I understand that current functionality is necessary, but may be it should
be called 'swich'.
As for me current implementation don't fit in the for comprehension.\

p.s. please close that issue, as it isn't actually an issue.


Reply to this email directly or view it on GitHubhttps://github.com//issues/61.

@Yarikx
Copy link
Author

Yarikx commented Feb 5, 2013

Ok, thanks. I'll notify you if would see good answers.
On Feb 5, 2013 5:05 AM, "nafg" notifications@github.com wrote:

Hi, can you ask this on scala-user? This way we can get opinions from
people who are more familiar with the design of monads, which is what
flatMap is all about.

Thanks.

On Friday, February 1, 2013, Yaroslav wrote:

Without ability to ask directly, just create an issue.

Why implementation of flatMap(f) just 'swich' between streams provided by
f?
If trying to think about EventStreams as about collections, than
flatMap(f) should return EventStream that will fire all events from all
streams provided by f.

I understand that current functionality is necessary, but may be it
should
be called 'swich'.
As for me current implementation don't fit in the for comprehension.\

p.s. please close that issue, as it isn't actually an issue.


Reply to this email directly or view it on GitHub<
https://github.com/nafg/reactive/issues/61>.


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

@dylemma
Copy link
Contributor

dylemma commented May 6, 2013

I'm currently liking the idea of renaming the current flatMap functionality to switch. Syntactically, I like the look of

mux switch {
  case 1 => onesStream
  case 2 => twosStream
  case n => otherNumbersStream
}

And in this particular example mux is short for "multiplexer," which seems to be exactly what this feature implements.

@dylemma
Copy link
Contributor

dylemma commented May 6, 2013

Also, to handle the "double click" example, I propose 2 new functions

/** Same functionality as `sliding` in the standard library */
def sliding(size: Int): EventStream[List[A]]

/** Fires events paired with the current time */
def zipWithTime: EventStream[(A, Long)]

So if you have a clicks stream, a "double clicks" stream would look something like

downs.zipWithTime.sliding(2).collect{
  case List( (d1, t1), (d2, t2) ) if t2-t1 < 500 => DoubleClick(d2.position)
}

P.S. The zipWithTime function could make use of a Time typeclass like this:

trait Time[T] { def currentTime: T }
trait EventStream[A] {
  ...
  def zipWithTime[T](implicit time: Time[T]): EventStream[(A, T)] = this.map {
    case e => e -> time.currentTime
  }
}

And that would allow the "time" value to be more extensible and receptive to people who hate using System.currentTimeMillis, or prefer JodaTime or whatever else happens to exist.

@Yarikx
Copy link
Author

Yarikx commented May 6, 2013

Wow. Couple of day ago I was thinking about implementing sliding function for things like double click. =)
You catch this first.
I'm glad you also support the idea that flatMap should be like map + flatten.

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

3 participants