MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
| 2 | mixed * map(mixed *arg, string func, string|object ob, mixed extra...) |
| 3 | mixed * map(mixed *arg, closure cl, mixed extra...) |
| 4 | mixed * map(mixed *arg, mapping m [, int idx]) |
| 5 | |
| 6 | mixed * map(struct arg, string func, string|object ob, mixed extra...) |
| 7 | mixed * map(struct arg, closure cl, mixed extra...) |
| 8 | mixed * map(struct arg, mapping m [, int idx]) |
| 9 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 10 | mapping map(mapping arg, string func, string|object ob |
| 11 | , mixed extra...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | mapping map(mapping arg, closure cl, mixed extra...) |
| 13 | |
| 14 | string map(string arg, string func, string|object ob, mixed extra...) |
| 15 | string map(string arg, closure cl, mixed extra...) |
| 16 | string map(string arg, mapping m [, int idx]) |
| 17 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 18 | DESCRIPTION |
| 19 | Call the function <ob>-><func>() resp. the closure <cl> for every |
| 20 | element of the string, array, struct or mapping <arg>, and return a |
| 21 | result made up from the returned values. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 22 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 23 | If <ob> is omitted, or neither a string nor an object, it |
| 24 | defaults to this_object(). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 25 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 26 | If <arg> is an string, array or struct, the function will be called |
| 27 | with each of the array/struct elements as first parameter, followed |
| 28 | by the <extra> arguments. The result of the efun is an array/struct |
| 29 | with all the results returned from the function calls. Thus the |
| 30 | operation could be described as: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 31 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 32 | foreach(index) result[index] = ob->func(arg[index], extra...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 33 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 34 | If <arg> is an string/array/struct, and a mapping is specified |
| 35 | instead of a function, the result is an array/struct with the |
| 36 | values found in the mapping for the original values, resp. with the |
| 37 | original values for which no mapping entry exists. If a column index |
| 38 | <idx> is given, that column of the mapping is used. In other words: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 39 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 40 | foreach(index) |
| 41 | if (arg[index] exists as key in map) |
| 42 | result[index] = map[arg[index]] or map[arg[index], idx] |
| 43 | else |
| 44 | result[index] = arg[index] |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 45 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 46 | If <arg> is a string, the only allowed replacement values are |
| 47 | numbers, of which only the lower 8 bit will be considered. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 48 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 49 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 50 | If <arg> is a mapping, the function will be called with |
| 51 | each of the mapping keys as first, and (if existing) the |
| 52 | associated values as second parameter, followed by the <extra> |
| 53 | arguments (these must not be protected references like &(i[0]). The |
| 54 | result of the efun is a mapping of all results of the function calls, |
| 55 | stored under their corresponding key. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 56 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 57 | Depending on the width of the mapping <arg>, the operation can |
| 58 | be described as: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 59 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 60 | foreach (key in arg) |
| 61 | switch (widthof(arg)) |
| 62 | case 0: |
| 63 | result[key] = ob->func(key, 0, extra...) |
| 64 | case 1: |
| 65 | result[key] = ob->func(key, arg[key], extra...) |
| 66 | else : |
| 67 | result[key] = ob->func( key |
| 68 | , ({ arg[key,0] ...arg[key,width-1] }) |
| 69 | , extra...) |
| 70 | |
| 71 | The advantage of this approach is that the two types of |
| 72 | multi-dimensional mappings (mappings with multiple values |
| 73 | per key, and mappings of arrays) can be treated in the same way. |
| 74 | |
| 75 | Note however that the resulting mapping always has one value |
| 76 | per key. |
| 77 | |
| 78 | Historical Note: map() used with arrays behaves like map_array(), |
| 79 | but used with mappings generalises map_indices()! |
| 80 | |
| 81 | |
| 82 | EXAMPLES |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 83 | arr = ({ 1, 2, 3, 4 }); |
| 84 | m = ([ 1:-1, 3:-3 ]); |
| 85 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 86 | map(arr, #'%, 2) --> returns ({ 1, 0, 1, 0 }) |
| 87 | map(arr, m) --> returns ({ -1, 2, -3, 4 }) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 88 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 89 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 90 | HISTORY |
| 91 | Introduced in LDMud 3.2.6, obsoletes map_array(). |
| 92 | LDMud 3.2.8 added the feature of mapping an array through a mapping. |
| 93 | LDMud 3.3.439 added mapping of strings. |
| 94 | LDMud 3.3.719 added the <idx> parameter for mapping through mappings. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 95 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 96 | SEE ALSO |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 97 | filter(E), filter_indices(E), map_indices(E), map_objects(E) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 98 | |