Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created October 1, 2022 06:14
Show Gist options
  • Save meysampg/30be5369e24c5e881904fda92e9b6e84 to your computer and use it in GitHub Desktop.
Save meysampg/30be5369e24c5e881904fda92e9b6e84 to your computer and use it in GitHub Desktop.
import scala.language.postfixOps
class LamportClock(private var count: Int = 0) {
// increment
def ++ = LamportClock(count + 1)
// merge
def +(other: LamportClock) = new LamportClock(math.max(count, other.count) + 1)
override def toString: String = s"LamportClock($count)"
}
object LamportClock {
def apply(count: Int = 0): LamportClock = new LamportClock(count)
}
val a = LamportClock(1)
val b = LamportClock(4)
println(a)
println(b)
println(a++)
println(a + b)
@meysampg
Copy link
Author

meysampg commented Oct 1, 2022

a: LamportClock = LamportClock(1)
b: LamportClock = LamportClock(4)

LamportClock(1)
LamportClock(4)
LamportClock(2)
LamportClock(5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment