Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object timeunits extends App {
- // sealed trait + N * case (class|object) == ADT
- sealed trait Timeunit
- case class Millis (x: Long) extends Timeunit
- case class Seconds(x: Long) extends Timeunit
- case class Minutes(x: Long) extends Timeunit
- case class Hours (x: Long) extends Timeunit
- implicit class DotComposition[R, T1](f: T1 => R) {
- def ∘[A](g: A => T1) = f compose g
- }
- def inMillis = identity(_: Long)
- def inSeconds = inMillis ∘ ((_: Long) * 1000)
- def inMinutes = inSeconds ∘ ((_: Long) * 60)
- def inHours = inMinutes ∘ ((_: Long) * 60)
- def toMillis = (_: Timeunit) match {
- case Millis(x) => inMillis(x)
- case Seconds(x) => inSeconds(x)
- case Minutes(x) => inMinutes(x)
- case Hours(x) => inHours(x)
- }
- // reaalselt kasutuses
- implicit class LongWithTimeunits(x: Long) {
- def millis = Millis(x); def milli = millis
- def seconds = Seconds(x); def second = seconds
- def minutes = Minutes(x); def minute = minutes
- def hours = Hours(x); def hour = hours
- }
- def sleep1(t: Timeunit) = Thread.sleep(toMillis(t))
- sleep1(2.seconds)
- // üks võimalik vaste Haskelli versioonile
- def sleep2(t: Long, unit: Long => Timeunit) = Thread.sleep(toMillis(unit(t)))
- sleep2(500, Millis)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement