apply: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "\|game([0-9])= *([^ ]+) * +\|version([0-9])= *([^ ]+) * " to "|game$1=$2 |version$3=$4 ") |
(Corrected some stuff) |
||
Line 5: | Line 5: | ||
|gr1= Arrays | |gr1= Arrays | ||
|gr2= HashMap | |||
|descr= Applies given code to each element of the | |descr= Applies the given code to each element of the given data structure and returns the result as an array. | ||
|s1= array [[apply]] | |s1= array [[apply]] code | ||
|p1= array: [[Array]] - Array of [[Anything]] | |p1= array: [[Array]] - Array of [[Anything]] | ||
|p2= code: [[Code]] - Code to be executed on each element of the array. | |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]]. | ||
|r1= [[Array]] - Resulting array | |r1= [[Array]] - Resulting array | ||
|s2= hashmap [[apply]] | |s2= hashmap [[apply]] code | ||
|s2since= arma3 2.04 | |||
|p21= hashmap: [[HashMap]] | |p21= hashmap: [[HashMap]] | ||
|p22= code: [[Code]] - Code to be executed on each | |p22= code: [[Code]] - Code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable variable [[Magic Variables#x|_x]], the corresponding value is stored in [[Magic Variables#y|_y]]. | ||
|r2= [[Array]] - Resulting array | |r2= [[Array]] - Resulting array | ||
|x1= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {[1,0] [[select]] (_x % 2 == 0)}; | |x1= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {[1,0] [[select]] (_x % 2 == 0)}; {{cc|[1,0,1,0,1,0,1,0,1,0]}}</code> | ||
|x2= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {_x ^ _x}; | |x2= <code>_arr = [1,2,3,4,5,6,7,8,9,0] [[apply]] {_x ^ _x}; {{cc|[1,4,27,256,3125,46656,823543,16777216,387420480,1]}}</code> | ||
|x3= <code>_arr1 = []; | |x3= <code>_arr1 = []; | ||
_arr1 [[resize]] 20; | _arr1 [[resize]] 20; | ||
_arr2 = _arr1 [[apply]] {0}; | _arr2 = _arr1 [[apply]] {0}; {{cc|[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}}</code> | ||
|x4= <code>[0,1,2,3,4] [[apply]] {[[str]] _x}; {{cc| | |x4= <code>[0,1,2,3,4] [[apply]] {[[str]] _x}; {{cc|["0","1","2","3","4"]}}</code> | ||
|x5= <code>_hashmap = [[createHashMapFromArray]] | |x5= <code>_hashmap = [[createHashMapFromArray]] [["Key 1", "Value 1"], ["Key 2", "Value 2"]]; | ||
_array = _hashmap [[apply]] {_y}; | _array = _hashmap [[apply]] {_y [[+]] " Test"}; {{cc|["Value 2 Test","Value 1 Test"]}}</code> | ||
|seealso= [[set]] | |seealso= [[set]] [[resize]] [[pushBack]] [[pushBackUnique]] [[select]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[arrayIntersect]] | ||
}} | }} | ||
Revision as of 13:46, 14 August 2021
Description
- Description:
- Applies the given code to each element of the given data structure and returns the result as 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 variable _x, the corresponding value is stored in _y.
- Return Value:
- Array - Resulting array
Examples
- Example 1:
_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:
_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:
_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:
_hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]]; _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
- Posted on February 18, 2016 - 11:03 (UTC)
- Fusselwurm
- (to anyone else wondering, I took a minute to get it): This is Array.map() in JavaScript
- Posted on February 11, 2018 - 23:02 (UTC)
- Lou Montana
- 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).