RosettaCodeData/Task/Rate-counter/E/rate-counter-1.e
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

24 lines
672 B
Text

def makeLamportSlot := <import:org.erights.e.elib.slot.makeLamportSlot>
The rate counter:
/** Returns a function to call to report the event being counted, and an
EverReporter slot containing the current rate, as a float64 in units of
events per millisecond. */
def makeRateCounter(timer, reportPeriod) {
var count := 0
var start := timer.now()
def &rate := makeLamportSlot(nullOk[float64], null)
def signal() {
def time := timer.now()
count += 1
if (time >= start + reportPeriod) {
rate := count / (time - start)
start := time
count := 0
}
}
return [signal, &rate]
}