diff --git a/doc/commands/lists.md b/doc/commands/lists.md index 741c4123..83fba2ad 100755 --- a/doc/commands/lists.md +++ b/doc/commands/lists.md @@ -26,16 +26,6 @@ at the deepest level in the stack. This is the opposite of [→List](#tolist). T `{ A B ... }` ▶ `A` `B` ... `Count` -## List→ - -Expand a list on the stack and return the number of elements. After executing -the command, level 1 contains the number of elements, and a corresponding number -of stack levels contain individual elements of the list, the first element being -at the deepest level in the stack. This is the opposite of [→List](#tolist). The -[Obj→](#fromobj) command performs the same operation when applied to a list. - -`{ A B ... }` ▶ `A` `B` ... `Count` - ## Head Return the first element of a list, or an `Invalid dimension` error if the list @@ -55,8 +45,16 @@ the list is empty. Apply an operation on all elements in a list or array. The operation on the first level of the stack should take one argument and return a single value. +If the list or array contains (nested) lists or arrays then these are recursively processed, +depending on the setting of the MapOneLevel / MapRecursive flag. +On the HP 50g the command works recursively (See the "HP 50g advanced user's reference manual"). +With DB48x rpl the default is MapRecursive also. +It can be set to MapOneLevel to get the behaviour of most programming languages. + `{ A B ... }` `F` ▶ `{ F(A) F(B) ... }` + + ## Reduce Apply a cumulative pairwise operation on all elements in a list or array. diff --git a/src/ids.tbl b/src/ids.tbl index 19928b14..2a6c6ed5 100644 --- a/src/ids.tbl +++ b/src/ids.tbl @@ -1049,6 +1049,7 @@ FLAG(NumericalIntegration, SymbolicIntegration) FLAG(TVMPayAtBeginningOfPeriod, TVMPayAtEndOfPeriod) FLAG(TruthLogicForIntegers, BitwiseLogicForIntegers) FLAG(LaxArrayResizing, StrictArrayResizing) +FLAG(MapOneLevel, MapRecursive) ALIAS(HardwareFloatingPoint, "HFP") ALIAS(HardwareFloatingPoint, "HardFP") diff --git a/src/list.cc b/src/list.cc index 58d85497..ae006241 100644 --- a/src/list.cc +++ b/src/list.cc @@ -786,7 +786,7 @@ bool list::expand() const bool list::expand_deep(uint32_t which) const // ---------------------------------------------------------------------------- -// Expand list content, expending inner expressions/programs/lists +// Expand list content, expanding inner expressions/programs/lists // ---------------------------------------------------------------------------- { for (object_p obj : *this) @@ -1537,7 +1537,7 @@ list_p list::map(object_p prgobj) const for (object_p obj : *this) { id oty = obj->type(); - if (is_array_or_list(oty)) + if (is_array_or_list(oty) && Settings.MapRecursive()) { list_g sub = list_p(obj)->map(prg); obj = +sub; @@ -1621,31 +1621,20 @@ list_p list::filter(object_p prgobj) const scribble scr; for (object_g obj : *this) { - id oty = obj->type(); bool keep = false; - if (is_array_or_list(oty)) - { - object_g sub = list_p(+obj)->filter(prg); - obj = +sub; - keep = true; - } - else + if (!rt.push(obj)) + goto error; + if (program::run(prg, true) != OK) + goto error; + if (rt.depth() != depth + 1) { - if (!rt.push(obj)) - goto error; - if (program::run(prg, true) != OK) - goto error; - if (rt.depth() != depth + 1) - { - rt.misbehaving_program_error(); - goto error; - } - object_p test = rt.pop(); - keep = test->as_truth(true); - if (rt.error()) - goto error; + rt.misbehaving_program_error(); + goto error; } - + object_p test = rt.pop(); + keep = test->as_truth(true); + if (rt.error()) + goto error; if (keep && !rt.append(obj)) goto error; } @@ -1708,7 +1697,7 @@ list_p list::map(algebraic_fn fn) const for (object_p obj : *this) { id oty = obj->type(); - if (is_array_or_list(oty)) + if (is_array_or_list(oty) && Settings.MapRecursive()) { list_g sub = list_p(obj)->map(fn); obj = +sub; @@ -1746,7 +1735,7 @@ list_p list::map(arithmetic_fn fn, algebraic_r y) const for (object_p obj : *this) { id oty = obj->type(); - if (is_array_or_list(oty)) + if (is_array_or_list(oty) && Settings.MapRecursive()) { list_g sub = list_p(obj)->map(fn, y); obj = +sub; @@ -1784,7 +1773,7 @@ list_p list::map(algebraic_r x, arithmetic_fn fn) const for (object_p obj : *this) { id oty = obj->type(); - if (is_array_or_list(oty)) + if (is_array_or_list(oty) && Settings.MapRecursive()) { list_g sub = list_p(obj)->map(x, fn); obj = +sub;