Skip to content

Commit

Permalink
Implement duration filters
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jan 26, 2024
1 parent 7bdb382 commit 3beb895
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ interface GeneralOps<V: IntervalLike<V>, T: GeneralOps<V, T>>: Timeline<V, T> {
fun mapIntervals(boundsTransformer: BoundsTransformer, truncate: Boolean, f: (V) -> Interval) = map(boundsTransformer, truncate) { v -> v.changeInterval(f(v)) }

fun filter(f: (V) -> Boolean) = BaseTimeline(ctor) { bounds -> collect(bounds).filter(f) }.specialize()

fun filterByDuration(min: Duration = Duration.MIN_VALUE, max: Duration = Duration.MAX_VALUE) =
filter { v ->
val duration = v.interval.duration()
duration.longerThan(min) && duration.shorterThan(max)
}
fun filterShorterThan(dur: Duration) = filter { v -> v.interval.duration().shorterThan(dur) }
fun filterLongerThan(dur: Duration) = filter { v -> v.interval.duration().longerThan(dur) }

fun shiftBy(dur: Duration) = map(BoundsTransformer.shift(dur), false) { v -> v.changeInterval(v.interval.shiftBy(dur)) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ interface SerialBooleanOps<P: GeneralOps<Segment<Boolean>, P>>: SerialOps<Boolea
if (t.value) t.interval.shiftBy(shiftRising, shiftFalling)
else t.interval.shiftBy(shiftFalling, shiftRising)
}

fun falsifyByDuration(min: Duration = Duration.MIN_VALUE, max: Duration = Duration.MAX_VALUE) =
mapValues { s ->
if (s.value) {
val duration = s.interval.duration()
duration.longerThan(min) && duration.shorterThan(max)
} else {
false
}
}

fun falsifyShorterThan(dur: Duration) = falsifyByDuration(min = dur)
fun falsifyLongerThan(dur: Duration) = falsifyByDuration(max = dur)
}

0 comments on commit 3beb895

Please sign in to comment.