1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
predicate = ()
# Detect when a predicate becomes true.
# @category Liquidsoap
# @param ~init Detect at beginning.
# @param p Predicate.
def predicate.activates(~init=false, p)
last = ref(not init)
fun () -> begin
cur = p()
ans = (not !last) and cur
last := cur
ans
end
end
# Become true once every time a predicate is true.
# @category Liquidsoap
# @param p Predicate.
def predicate.once(p)
predicate.activates(init=true, p)
end
# Limit the number of times a predicate is true is a row.
# @param n Number of times the predicate is allowed to be true.
# @param p Predicate.
def predicate.at_most(n, p)
k = ref(0)
fun () -> begin
if p() then
ref.incr(k)
!k <= n
else
k := 0
false
end
end
end
# Detect when a predicate changes.
# @category Liquidsoap
# @param p Predicate.
def predicate.changes(p)
last = ref(p())
fun () -> begin
cur = p()
ans = (cur != !last)
last := cur
ans
end
end
# First occurrence of a predicate.
# @category Liquidsoap
# @param p Predicate.
def predicate.first(p)
done = ref(false)
fun () -> begin
if !done then
false
else
if p() then
done := true
true
else
false
end
end
end
end
# Predicate which is true when a signal is sent. The returned predicate has a
# method `signal` to send the signal.
# @category Liquidsoap
# @method signal Send a signal.
def predicate.signal()
state = ref(false)
def signal()
state := true
end
def p ()
if !state then
state := false
true
else
false
end
end
p.{ signal=signal }
end
|