From fb24da2555a1284610a6a642ac73264094ae4d8d Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Tue, 22 Oct 2019 20:55:18 -0500 Subject: [PATCH 1/2] CBA_fnc_selectBest --- addons/arrays/CfgFunctions.hpp | 1 + addons/arrays/fnc_selectBest.sqf | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 addons/arrays/fnc_selectBest.sqf diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index b9ea0c675..69ce3dc72 100644 --- a/addons/arrays/CfgFunctions.hpp +++ b/addons/arrays/CfgFunctions.hpp @@ -15,6 +15,7 @@ class CfgFunctions { PATHTO_FNC(join); PATHTO_FNC(reject); PATHTO_FNC(select); + PATHTO_FNC(selectBest); PATHTO_FNC(selectRandomArray); PATHTO_FNC(shuffle); PATHTO_FNC(sortNestedArray); diff --git a/addons/arrays/fnc_selectBest.sqf b/addons/arrays/fnc_selectBest.sqf new file mode 100644 index 000000000..d22c55d86 --- /dev/null +++ b/addons/arrays/fnc_selectBest.sqf @@ -0,0 +1,40 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_selectBest + +Description: + Select best element from an array + +Parameters: + _array - Input Array [Array] + _criteria - Code that is passed element and should return an integer value [Code] + _return - default return value if array is empty (optional, default nil) [Any] + +Returns: + Element that scores the highest [Any] + +Example: + (begin example) + _result = [[2, -6, 4], {abs _x}] call CBA_fnc_selectBest; + // _result => -6 + _result = [[q1, q2], {-(t1 distance _x)}, objNull] call CBA_fnc_selectBest; // selects closest target (negative distance) + // _result => q1 + (end) + +Author: + PabstMirror +---------------------------------------------------------------------------- */ +SCRIPT(selectBest); + +params ["_array", "_criteria", "_return"]; + +private _bestScore = -1e99; +{ + private _xScore = _x call _criteria; + if (_xScore > _bestScore) then { + _return = _x; + _bestScore = _xScore; + }; +} forEach _array; + +_return From 0be0b06aaf0a4ac1792c281a12aeb7b0a3f6f203 Mon Sep 17 00:00:00 2001 From: commy2 Date: Wed, 6 Nov 2019 18:51:09 +0100 Subject: [PATCH 2/2] Update addons/arrays/fnc_selectBest.sqf --- addons/arrays/fnc_selectBest.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/fnc_selectBest.sqf b/addons/arrays/fnc_selectBest.sqf index d22c55d86..89cd93988 100644 --- a/addons/arrays/fnc_selectBest.sqf +++ b/addons/arrays/fnc_selectBest.sqf @@ -37,4 +37,4 @@ private _bestScore = -1e99; }; } forEach _array; -_return +RETNIL(_return)