Skip to content

Instantly share code, notes, and snippets.

@emdagon
Last active January 3, 2016 04:49
Show Gist options
  • Save emdagon/8411341 to your computer and use it in GitHub Desktop.
Save emdagon/8411341 to your computer and use it in GitHub Desktop.
This is a simple approach to create what I call a "Punctured Number", the idea is to assign a lifetime to each addition or subtraction operation over a number, and once that lifetime passed, the changes of those operation are discarded. So the number will always go back its initial value.
import scala.concurrent.duration._
import PuncturedInt
val p = new PuncturedInt(0, 5 seconds)
p += 20 // Mon Jan 13 22:42:15
p -= 3 // Mon Jan 13 22:42:18
p() // Mon Jan 13 22:42:19 -> returns 17
p() // Mon Jan 13 22:42:21 -> returns -3
p += 11 // Mon Jan 13 22:42:22
p() // Mon Jan 13 22:42:22 -> returns 8
p() // Mon Jan 13 22:42:24 -> returns 11
p() // Mon Jan 13 22:42:28 -> returns 0
// if you want to use "p" as a common integer operand, you need to import the implicits:
import PuncturedImplicits._
// and now you can...
val x = y + p
// the value of "x" depends on the time the operation is performed =)
// The MIT License (MIT)
// Copyright (c) 2014 Emilio Daniel González
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import scala.concurrent.duration.FiniteDuration
import scala.collection.mutable
class PuncturedInt(val initial: Int = 0, val rate: FiniteDuration) {
private val valuesMap: mutable.Map[Long, Int] = mutable.Map()
def apply(): Int = {
valuesMap --= valuesMap.filter(_._1 < System.nanoTime - rate.toNanos).keys
valuesMap.values.sum + initial
}
def +=(n: Int): Int = {
valuesMap += (System.nanoTime -> n)
apply()
}
def -=(n: Int): Int = {
valuesMap += (System.nanoTime -> -n)
apply()
}
override def toString = valuesMap.toString
}
object PuncturedImplicits {
implicit def PuncturedInt2Int(value: PuncturedInt): Int = value()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment