Skip to content

Commit 0be8834

Browse files
committed
Handle MapAt with associations
1 parent e2967c9 commit 0be8834

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

mathics/builtin/structure.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
BinaryOperator,
88
Test,
99
MessageException,
10+
PartRangeError,
1011
)
1112
from mathics.core.expression import (
1213
Expression,
1314
String,
1415
Symbol,
16+
SymbolTrue,
17+
SymbolFalse,
1518
Integer,
1619
Rational,
1720
strip_context,
1821
)
19-
from mathics.core.rules import Pattern
22+
from mathics.core.rules import Pattern, Rule
2023

21-
from mathics.builtin.lists import python_levelspec, walk_levels, InvalidLevelspecError, List
24+
from mathics.builtin.lists import (
25+
python_levelspec,
26+
walk_levels,
27+
InvalidLevelspecError,
28+
List,
29+
)
2230
from mathics.builtin.functional import Identity
2331

2432
import platform
@@ -524,25 +532,44 @@ class MapAt(Builtin):
524532
<dd>applies $f$ to the element at position $n$ in $expr$. If $n$ is negative, the position is counted from the end.
525533
</dl>
526534
535+
Map $f$ onto the part at position 2:
527536
>> MapAt[f, {a, b, c, d}, 2]
528537
= {a, f[b], c, d}
538+
539+
Map $f$ onto the at the end:
529540
>> MapAt[f, {a, b, c, d}, -1]
530541
= {a, b, c, f[d]}
542+
543+
Map $f$ onto an association:
544+
>> MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|>, 3]
545+
= {a -> 1, b -> 2, c -> f[3], d -> 4, e -> 5}
546+
547+
Use negative position in an association:
548+
>> MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, -3]
549+
= {a -> 1, b -> f[2], c -> 3, d -> 4}
531550
"""
532551

533552
def apply(self, f, expr, n, evaluation, options={}):
534553
"MapAt[f_, expr_, n_Integer]"
535554
i = n.get_int_value()
536555
m = len(expr.leaves)
537556
if 1 <= i <= m:
538-
j = i -1
557+
j = i - 1
539558
elif -m <= i <= -1:
540559
j = m + i
541560
else:
542-
evaluation.message('MapAt', 'normal')
561+
raise PartRangeError
543562

544563
new_leaves = list(expr.leaves)
545-
new_leaves[j] = Expression(f, new_leaves[j])
564+
replace_leaf = new_leaves[j]
565+
if hasattr(replace_leaf, "head") and replace_leaf.head == Symbol("System`Rule"):
566+
new_leaves[j] = Expression(
567+
"System`Rule",
568+
replace_leaf.leaves[0],
569+
Expression(f, replace_leaf.leaves[1]),
570+
)
571+
else:
572+
new_leaves[j] = Expression(f, replace_leaf)
546573
return List(*new_leaves)
547574

548575

@@ -854,9 +881,9 @@ def apply(self, expr, form, evaluation):
854881

855882
form = Pattern.create(form)
856883
if expr.is_free(form, evaluation):
857-
return Symbol("True")
884+
return SymbolTrue
858885
else:
859-
return Symbol("False")
886+
return SymbolFalse
860887

861888

862889
class Flatten(Builtin):
@@ -1060,6 +1087,7 @@ def insert_leaf(leaves):
10601087
new_leaves.append(Expression(h, *insert_leaf(group)))
10611088

10621089
return new_leaves
1090+
10631091
return Expression(h, *insert_leaf(leaves))
10641092

10651093
def apply(self, expr, n, h, evaluation):

0 commit comments

Comments
 (0)