Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/commands/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ at the deepest level in the stack. This is the opposite of [→List](#tolist). T

`{ A B ... }` ▶ `A` `B` ... `Count`


## Head

Return the first element of a list, or an `Invalid dimension` error if the list
Expand All @@ -44,8 +45,15 @@ 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) ... }`


The operation applies recursively to inner lists.

```rpl
Expand Down
1 change: 1 addition & 0 deletions src/ids.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ FLAG(NumericalIntegration, SymbolicIntegration)
FLAG(TVMPayAtBeginningOfPeriod, TVMPayAtEndOfPeriod)
FLAG(TruthLogicForIntegers, BitwiseLogicForIntegers)
FLAG(LaxArrayResizing, StrictArrayResizing)
FLAG(MapOneLevel, MapRecursive)

ALIAS(HardwareFloatingPoint, "HFP")
ALIAS(HardwareFloatingPoint, "HardFP")
Expand Down
43 changes: 16 additions & 27 deletions src/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,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)
Expand Down Expand Up @@ -1550,7 +1550,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;
Expand Down Expand Up @@ -1634,31 +1634,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;
}
Expand Down Expand Up @@ -1721,7 +1710,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;
Expand Down Expand Up @@ -1759,7 +1748,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;
Expand Down Expand Up @@ -1797,7 +1786,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;
Expand Down