Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Commit

Permalink
Time.now is pure (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolov authored Aug 28, 2018
1 parent ffedc81 commit 56a4288
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/scalaz/reactive/Time.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,7 @@ package scalaz.reactive
import scalaz.Order
import scalaz.Ordering.{ EQ, GT, LT }
import scalaz.reactive.Time.{ NegInf, PosInf, T }
import scalaz.zio.IO

sealed trait Time

Expand All @@ -14,7 15,7 @@ object Time extends TimeInstances0 {

case object PosInf extends Time

def now = T(System.currentTimeMillis())
def now = IO.sync(T(System.currentTimeMillis()))
}

trait TimeInstances0 {
Expand Down
20 changes: 16 additions & 4 deletions examples/src/main/scala/scalaz/reactive/examples/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 24,16 @@ object Synthesizer extends App {
case class Note(octave: Octave, pitch: Pitch)

val table =
Map('a' -> PA, 'b' -> PB, 'b' -> PB, 'c' -> PC, 'd' -> PD, 'e' -> PE, 'f' -> PF, 'g' -> PG)
Map(
'a' -> PA,
'b' -> PB,
'b' -> PB,
'c' -> PC,
'd' -> PD,
'e' -> PE,
'f' -> PF,
'g' -> PG
)

def build(eKey: Event[Char]) = {
// val ePitch: Event[Pitch] = Event.joinMaybes(
Expand Down Expand Up @@ -68,8 77,8 @@ object Synthesizer extends App {
StdIn.readChar()
}

Event(nextChar.map { c =>
(Time.now, Reactive(c, eKey))
Event(nextChar.flatMap { c =>
Time.now.map((_, Reactive(c, eKey)))
})
}

Expand All @@ -79,7 88,10 @@ object Synthesizer extends App {

Sink.sinkB(
toSink,
(tn: TimeFun[Octave]) => IO.sync { println(s"Octave is ${tn.apply(Time.now)}") }
(tn: TimeFun[Octave]) =>
Time.now.map { t =>
println(s"Octave is ${tn.apply(t)}")
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 15,8 @@ object TwoTickers extends App {
case class Tick(name: String)

def ticks(interval: Duration, name: String): Event[Tick] =
Event(IO.point {
(Time.now, Reactive(Tick(name), ticks(interval, name).delay(interval)))
Event(Time.now.map { t =>
(t, Reactive(Tick(name), ticks(interval, name).delay(interval)))
})

def myAppLogic: IO[Void, Unit] = {
Expand Down
24 changes: 12 additions & 12 deletions examples/src/main/scala/scalaz/reactive/examples/awt/AwtApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 46,22 @@ abstract class AwtApp(val name: String)
private val actionQueue = new LinkedBlockingDeque[ActionEvent]()

private def keyEvent: Event[KeyEvent] =
Event(
IO.point(keyQueue.take())
.map(ke => (Time.now, Reactive(ke, keyEvent)))
)
Event(for {
t <- Time.now
e <- IO.sync(keyQueue.take())
} yield (t, Reactive(e, keyEvent)))

private def mouseEvent: Event[MouseEvent] =
Event(
IO.point(mouseQueue.take())
.map(ke => (Time.now, Reactive(ke, mouseEvent)))
)
Event(for {
t <- Time.now
e <- IO.sync(mouseQueue.take())
} yield (t, Reactive(e, mouseEvent)))

private def actionEvent: Event[ActionEvent] =
Event(
IO.point(actionQueue.take())
.map(ke => (Time.now, Reactive(ke, actionEvent)))
)
Event(for {
t <- Time.now
e <- IO.sync(actionQueue.take())
} yield (t, Reactive(e, actionEvent)))

private val allEvent = mouseEvent.merge(keyEvent).merge(actionEvent)

Expand Down

0 comments on commit 56a4288

Please sign in to comment.