-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David James
committed
Jul 22, 2014
1 parent
21bd7ed
commit 2a5e916
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns kria.polling) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(defn poll | ||
"Returns true if function `f` returns value `v` within `max-i` | ||
iterations. Returns false otherwise. Each iteration waits `i-delay` | ||
milliseconds." | ||
[v f max-i i-delay] | ||
(loop [i 0] | ||
(if (>= i max-i) | ||
false | ||
(let [fv (f)] | ||
(if (= v fv) | ||
true | ||
(do | ||
(Thread/sleep i-delay) | ||
(recur (inc i)))))))) | ||
|
||
(defn poll+ | ||
"Returns true if `poll` function returns true and predicate | ||
function remains true after an additional time delay. This is | ||
intended to confirm that the function does not change after | ||
becoming true." | ||
[v f max-i i-delay] | ||
(when (poll v f max-i i-delay) | ||
(Thread/sleep (* 2 i-delay)) | ||
(= v (f)))) |