blob: 5c6c47d42ade7a446934fb6bf67ae02c02e8089f [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001SYNOPSIS
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
Zesstra715ec202025-07-09 22:18:31 +020010 mapping map(mapping arg, string func, string|object ob
11 , mixed extra...)
MG Mud User88f12472016-06-24 23:31:02 +020012 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
Zesstra715ec202025-07-09 22:18:31 +020018DESCRIPTION
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 User88f12472016-06-24 23:31:02 +020022
Zesstra715ec202025-07-09 22:18:31 +020023 If <ob> is omitted, or neither a string nor an object, it
24 defaults to this_object().
MG Mud User88f12472016-06-24 23:31:02 +020025
Zesstra715ec202025-07-09 22:18:31 +020026 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 User88f12472016-06-24 23:31:02 +020031
Zesstra715ec202025-07-09 22:18:31 +020032 foreach(index) result[index] = ob->func(arg[index], extra...)
MG Mud User88f12472016-06-24 23:31:02 +020033
Zesstra715ec202025-07-09 22:18:31 +020034 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 User88f12472016-06-24 23:31:02 +020039
Zesstra715ec202025-07-09 22:18:31 +020040 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 User88f12472016-06-24 23:31:02 +020045
Zesstra715ec202025-07-09 22:18:31 +020046 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 User88f12472016-06-24 23:31:02 +020048
MG Mud User88f12472016-06-24 23:31:02 +020049
Zesstra715ec202025-07-09 22:18:31 +020050 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 User88f12472016-06-24 23:31:02 +020056
Zesstra715ec202025-07-09 22:18:31 +020057 Depending on the width of the mapping <arg>, the operation can
58 be described as:
MG Mud User88f12472016-06-24 23:31:02 +020059
Zesstra715ec202025-07-09 22:18:31 +020060 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
82EXAMPLES
MG Mud User88f12472016-06-24 23:31:02 +020083 arr = ({ 1, 2, 3, 4 });
84 m = ([ 1:-1, 3:-3 ]);
85
Zesstra715ec202025-07-09 22:18:31 +020086 map(arr, #'%, 2) --> returns ({ 1, 0, 1, 0 })
87 map(arr, m) --> returns ({ -1, 2, -3, 4 })
MG Mud User88f12472016-06-24 23:31:02 +020088
MG Mud User88f12472016-06-24 23:31:02 +020089
Zesstra715ec202025-07-09 22:18:31 +020090HISTORY
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 User88f12472016-06-24 23:31:02 +020095
Zesstra715ec202025-07-09 22:18:31 +020096SEE ALSO
MG Mud User88f12472016-06-24 23:31:02 +020097 filter(E), filter_indices(E), map_indices(E), map_objects(E)
Zesstra715ec202025-07-09 22:18:31 +020098