Files
wf/source/Utils.mc
2025-12-04 17:38:38 +02:00

48 lines
1.2 KiB
MonkeyC

import Toybox.Lang;
import Toybox.Graphics;
// generic function for arrays
function find(source as Array, searchFunction as Method(left, right) as Boolean) {
if (source == null || source.size() == 0) {
return null;
}
var result = source[0];
for (var i = 1; i < source.size(); ++i) {
if (searchFunction.invoke(result, source[i])) {
result = source[i];
}
}
return result;
}
// no types here, because this is generic, which are not supported by language
function getOrElse(value, defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
function _max(left as Numeric, right as Numeric) as Boolean {
return left < right;
}
function max(source as Array<Numeric>) as Numeric {
return find(source, new Method($, :_max));
}
function _min(left as Numeric, right as Numeric) as Boolean {
return left > right;
}
function min(source as Array<Numeric>) as Numeric {
return find(source, new Method($, :_min));
}
function getCenter(dc as Dc, shift as Point2D) as Point2D {
return [dc.getWidth() / 2.0 + shift[0], dc.getHeight() / 2.0 + shift[1]];
}