apply: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (note edit with better results)
m (Text replacement - "(\|[pr][0-9]+ *= *[^-]+) *- *C([a-eg-z])" to "$1 - c$2")
 
(55 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


|arma3|= Game name
|game1= arma3
|version1= 1.56


|1.56|= Game version
|gr1= Arrays
|gr2= HashMap


____________________________________________________________________________________________
|descr= Applies the given code to each element of the given data structure and collects the results in an array.


| Applies given code to each element of the array and returns resulting array. The value of the current array element, to which the code will be applied, is stored in variable [[_x]].|= Description
|s1= array [[apply]] code
____________________________________________________________________________________________


| array '''apply''' code|= Syntax
|p1= array: [[Array]] - array of [[Anything]]


|p1= array: [[Array]] - array of [[Anything]] |= Parameter 1
|p2= code: [[Code]] - code to be executed on each element of the array. The current element value is stored in the magic variable [[Magic Variables#x|_x]].


|p2= code: [[Code]] - code to be executed on each element of the array. Current element value is stored in variable [[_x]]|= Parameter 2
|r1= [[Array]] - resulting array


| [[Array]] - resulting array|= Return value
|s2= hashmap [[apply]] code
____________________________________________________________________________________________
 
|x1= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {[1,0] [[select]] (_x % 2 == 0)}; //[1,0,1,0,1,0,1,0,1,0]</code> |= Example 1
|x2= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {_x ^ _x}; //[1,4,27,256,3125,46656,823543,16777216,387420480,1]</code> |= Example 2
|x3= <code>_arr1 = [];
_arr1 [[resize]] 20;
_arr2 = _arr1 [[apply]] {0}; //[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</code>|=
____________________________________________________________________________________________


| [[set]], [[resize]], [[pushBack]], [[pushBackUnique]], [[select]], [[reverse]], [[count]], [[find]], [[in]], [[forEach]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[arrayIntersect]], [[BIS_fnc_arrayPushStack]], [[BIS_fnc_arrayPush]] |= See also
|s2since= arma3 2.04


}}
|p21= hashmap: [[HashMap]]
 
|p22= code: [[Code]] - code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable [[Magic Variables#x|_x]], the corresponding value is stored in [[Magic Variables#y|_y]].
 
|r2= [[Array]] - resulting array
 
|x1= <sqf>private _arr = [1,2,3,4,5,6,7,8,9,0] apply { [1,0] select (_x % 2 == 0) }; // [1,0,1,0,1,0,1,0,1,0]</sqf>
 
|x2= <sqf>private _arr = [1,2,3,4,5,6,7,8,9,0] apply { _x ^ _x }; // [1,4,27,256,3125,46656,823543,16777216,387420480,1]</sqf>


<h3 style="display:none">Notes</h3>
|x3= <sqf>
<dl class="command_description">
private _arr1 = [];
</dl>
_arr1 resize 20;
_arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
</sqf>


<h3 style="display:none">Bottom Section</h3>
|x4= <sqf>[0,1,2,3,4] apply { str _x }; // ["0","1","2","3","4"]</sqf>


|x5= <sqf>
private _hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]];
private _array = _hashmap apply { _y + " Test" }; // ["Value 2 Test","Value 1 Test"]
</sqf>


[[Category:Arma_3:_New_Scripting_Commands_List|{{uc:{{PAGENAME}}}}]]
|seealso= [[set]] [[resize]] [[pushBack]] [[pushBackUnique]] [[select]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[arrayIntersect]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
}}
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= Fusselwurm
<dd class="notedate">Posted on February 18, 2016 - 11:03 (UTC)</dd>
|timestamp= 20160218110300
<dt class="note">[[User:Fusselwurm|Fusselwurm]]</dt>
|text= (to anyone else wondering, I took a minute to get it) this is {{Link|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map|Array.map()}} in JavaScript
<dd class="note">
}}
(to anyone else wondering, I took a minute to get it): This is Array.map() is JavaScript
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= Lou Montana
<dd class="notedate">Posted on February 11, 2018 - 23:02 (UTC)</dd>
|timestamp= 20180211230200
<dt class="note">[[User:Lou Montana|Lou Montana]]</dt>
|text= if performance really is an issue, [[apply]] seems to be (very) slightly faster than [[forEach]] (by more or less one percent, 0.7-1.5% in my tests to be precise).
<dd class="note">
}}
if performance really is an issue, [[apply]] seems to be (very) slightly faster than [[forEach]] (by more or less one percent, 0.7-1.5% in my tests to be precise).
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 14:00, 8 November 2023

Hover & click on the images for description

Description

Description:
Applies the given code to each element of the given data structure and collects the results in an array.
Groups:
ArraysHashMap

Syntax

Syntax:
array apply code
Parameters:
array: Array - array of Anything
code: Code - code to be executed on each element of the array. The current element value is stored in the magic variable _x.
Return Value:
Array - resulting array

Alternative Syntax

Syntax:
hashmap apply code
Parameters:
hashmap: HashMap
code: Code - code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable _x, the corresponding value is stored in _y.
Return Value:
Array - resulting array

Examples

Example 1:
private _arr = [1,2,3,4,5,6,7,8,9,0] apply { [1,0] select (_x % 2 == 0) }; // [1,0,1,0,1,0,1,0,1,0]
Example 2:
private _arr = [1,2,3,4,5,6,7,8,9,0] apply { _x ^ _x }; // [1,4,27,256,3125,46656,823543,16777216,387420480,1]
Example 3:
private _arr1 = []; _arr1 resize 20; _arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Example 4:
[0,1,2,3,4] apply { str _x }; // ["0","1","2","3","4"]
Example 5:
private _hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]]; private _array = _hashmap apply { _y + " Test" }; // ["Value 2 Test","Value 1 Test"]

Additional Information

See also:
set resize pushBack pushBackUnique select reverse count find in forEach deleteAt deleteRange append sort arrayIntersect

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Fusselwurm - c
Posted on Feb 18, 2016 - 11:03 (UTC)
(to anyone else wondering, I took a minute to get it) this is Array.map() in JavaScript
Lou Montana - c
Posted on Feb 11, 2018 - 23:02 (UTC)
if performance really is an issue, apply seems to be (very) slightly faster than forEach (by more or less one percent, 0.7-1.5% in my tests to be precise).