reactive icon indicating copy to clipboard operation
reactive copied to clipboard

flatMap implementation

Open Yarikx opened this issue 12 years ago • 6 comments

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 avatar Feb 01 '13 16:02 Yarikx

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

Yarikx avatar Feb 01 '13 16:02 Yarikx

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/nafg/reactive/issues/61.

nafg avatar Feb 05 '13 03:02 nafg

Ok, thanks. I'll notify you if would see good answers. On Feb 5, 2013 5:05 AM, "nafg" [email protected] 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/nafg/reactive/issues/61#issuecomment-13112526.

Yarikx avatar Feb 05 '13 06:02 Yarikx

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 avatar May 06 '13 19:05 dylemma

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.

dylemma avatar May 06 '13 19:05 dylemma

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.

Yarikx avatar May 06 '13 20:05 Yarikx