Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct Take[] of 0th element... #1227

Merged
merged 1 commit into from
Dec 17, 2024
Merged
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
58 changes: 51 additions & 7 deletions mathics/builtin/list/eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,26 @@ def eval_ls_n(self, items, pattern, levelspec, n, evaluation):
"DeleteCases",
"innf",
Integer4,
Expression(SymbolDeleteCases, items, pattern, levelspec, n),
Expression(
SymbolDeleteCases,
items,
pattern,
*from_python(levelspec),
Integer(n),
),
)
else:
evaluation.message(
"DeleteCases",
"innf",
Expression(SymbolDeleteCases, items, pattern, levelspec, n),
Integer4,
Expression(
SymbolDeleteCases,
items,
pattern,
*from_python(levelspec),
Integer(n),
),
)
return SymbolNull

Expand Down Expand Up @@ -518,7 +531,7 @@ class Drop(Builtin):
<dd>returns $list$ with elements $m$ though $n$ removed.
</dl>

Drop up intil the 3rd item from the beginning of a list:
Drop up until the third item from the beginning of a list:

>> Drop[{a, b, c, d}, 3]
= {d}
Expand All @@ -537,7 +550,7 @@ class Drop(Builtin):
>> Drop[A, {2, 3}, {2, 3}]
= {{11, 14}, {41, 44}}

Dropping the 0th element does nothing and returns the list unmodified:
Dropping the 0th element does nothing, and returns the list unmodified:

>> Drop[{a, b, c, d}, 0]
= {a, b, c, d}
Expand All @@ -546,6 +559,10 @@ class Drop(Builtin):

>> Drop[{}, 0]
= {}

See also <url>
:'Take':
/doc/reference-of-built-in-symbols/list-functions/elements-of-lists/take/</url>.
"""

messages = {
Expand Down Expand Up @@ -750,7 +767,13 @@ class FirstPosition(Builtin):
summary_text = "position of the first element matching a pattern"

def eval(
self, expr, pattern, evaluation, default=None, minLevel=None, maxLevel=None
self,
expr,
pattern,
evaluation: Evaluation,
default=None,
minLevel=None,
maxLevel=None,
):
"FirstPosition[expr_, pattern_]"

Expand Down Expand Up @@ -973,8 +996,8 @@ class Length(Builtin):

summary_text = "number of elements in a list or expression"

def eval(self, expr, evaluation):
"Length[expr_]"
def eval(self, expr, evaluation: Evaluation):
"""Length[expr_]"""

if isinstance(expr, Atom):
return Integer0
Expand Down Expand Up @@ -1611,12 +1634,21 @@ class Take(Builtin):
<dl>
<dt>'Take[$expr$, $n$]'
<dd>returns $expr$ with all but the first $n$ elements removed.
<dt>'Take[$list$, -$n$]'
<dd>returns last $n$ elements of $list$.
<dt>'Take[$list$, {$m$, $n$}]'
<dd>returns elements $m$ through $n$ of $list$.
</dl>

Get the first three elements:
>> Take[{a, b, c, d}, 3]
= {a, b, c}

Get the last two elements:
>> Take[{a, b, c, d}, -2]
= {c, d}

Get the elements from the second element through the next to last element:
>> Take[{a, b, c, d, e}, {2, -2}]
= {b, c, d}

Expand All @@ -1628,13 +1660,25 @@ class Take(Builtin):
Take a single column:
>> Take[A, All, {2}]
= {{b}, {e}}

Taking the 0th element does nothing, and returns an empty list:

>> Take[{a, b, c, d}, 0]
= {}

See also <url>
:'Drop':
/doc/reference-of-built-in-symbols/list-functions/elements-of-lists/drop/</url>.
"""

summary_text = "pick a range of elements"

def eval(self, items, seqs, evaluation):
"Take[items_, seqs___]"

if seqs is Integer0:
return ListExpression()

seqs = seqs.get_sequence()

if isinstance(items, Atom):
Expand Down
6 changes: 3 additions & 3 deletions mathics/eval/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Evaluation methods for accessing and manipulating elements in nested lists / expressions
"""

from typing import List
from typing import List, Optional, Tuple

from mathics.core.atoms import Integer
from mathics.core.convert.expression import make_expression
Expand Down Expand Up @@ -386,8 +386,8 @@ def walk_levels(
return new_expr, depth


def python_levelspec(levelspec):
def value_to_level(expr):
def python_levelspec(levelspec) -> Tuple[int, Optional[int]]:
def value_to_level(expr) -> Optional[int]:
value = expr.get_int_value()
if value is None:
if expr.sameQ(MATHICS3_INFINITY):
Expand Down
Loading