I’m not sure if I fully grasp some language features of Antescofo, maybe I’m overlooking something. I admit that the functional paradigm is not always in concordance with the way my brain tends to approach a problem at first. The problem I want to solve is the following:
- I get a flat list where the elements with even index represent consecutive time points and the odd ones loudness values.
- I want to find the maximum loudness value within a given time region.
After abandoning the intuitive loop approach my second idea was to use tab comprehension and predicates but I did not find constructs in the documentation which I think I’d need, most of all to filter a tab (create a sub-tab) by predicate. I finally found a solution by somewhat misusing the @find
function, somewhat because I want to find something but nevertheless misusing because it is not @find
which returns the result (the predicate function always returns false
) but a kind of side-effect. That side-effect is achieved by currying the predicate with a one-element tab as value holder and the arguments for the region.
@fun_def pred_maxinregion($valmax, $tmin, $tmax, $i, $v) {
if ($v[0] >= $tmin && $v[0] < $tmax) {
if ($v[1] > $valmax[0]) {
$valmax[0] := $v[1]
}
}
return false
}
group TestFindMaxInRegion {
@local $valueholder, $maxfound
$valueholder := tab [-100] // initialize with a low value
$tab1 := tab [0, -20, 1, 0, 2, -20, 3, -5, 4, -30, 5, 0] // example input
$tab2 := @reshape($tab1, [@size($tab1) / 2, 2]) // two-dimensional array
@find($tab2, @pred_maxinregion($valueholder, 2, 5)) // iterate it
$maxfound := $valueholder[0] // save the result
}
And yes, this example finds [-5]
. But is there a better way to obtain the result?