initial commit
This commit is contained in:
47
source/Utils.mc
Normal file
47
source/Utils.mc
Normal file
@ -0,0 +1,47 @@
|
||||
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 = 0; 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]];
|
||||
}
|
||||
Reference in New Issue
Block a user