Skip to content
Open
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
18 changes: 8 additions & 10 deletions doc/commands/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/ids.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 16 additions & 27 deletions src/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down