Skip to content

Commit

Permalink
Add more WMA links, and show operator symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Dec 21, 2024
1 parent 58c2267 commit 0b12250
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
28 changes: 21 additions & 7 deletions mathics/builtin/functional/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@
Function Application
"""

# This tells documentation how to sort this module
sort_order = "mathics.builtin.function-application"


from itertools import chain

import sympy

from mathics.core.atoms import Integer, Integer1
from mathics.core.attributes import A_HOLD_ALL, A_N_HOLD_ALL, A_PROTECTED
from mathics.core.builtin import Builtin, PostfixOperator, SympyFunction
from mathics.core.builtin import Builtin, PostfixOperator, PrefixOperator, SympyFunction
from mathics.core.convert.sympy import SymbolFunction
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.symbols import Symbol, sympy_slot_prefix
from mathics.core.systemsymbols import SymbolSlot

# This tells documentation how to sort this module
sort_order = "mathics.builtin.function-application"


class Function(PostfixOperator, SympyFunction):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Function.html</url>
<dl>
<dt>'Function[$body$]'
<dt>'$body$ &'
Expand Down Expand Up @@ -164,8 +166,11 @@ def to_sympy(self, expr: Expression, **kwargs):
raise NotImplementedError


class Slot(SympyFunction):
class Slot(SympyFunction, PrefixOperator):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Slot.html</url>
<dl>
<dt>'#$n$'
<dd>represents the $n$th argument to a pure function.
Expand All @@ -190,6 +195,9 @@ class Slot(SympyFunction):
"""

attributes = A_N_HOLD_ALL | A_PROTECTED

operator = "#" # FIXME generate this automatically

rules = {
"Slot[]": "Slot[1]",
"MakeBoxes[Slot[n_Integer?NonNegative],"
Expand All @@ -204,8 +212,12 @@ def to_sympy(self, expr: Expression, **kwargs):
return sympy.Symbol(f"{sympy_slot_prefix}{index.get_int_value()}")


class SlotSequence(Builtin):
class SlotSequence(PrefixOperator, Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/SlotSequence.html</url>
<dl>
<dt>'##'
<dd>is the sequence of arguments supplied to a pure function.
Expand All @@ -225,6 +237,8 @@ class SlotSequence(Builtin):

attributes = A_N_HOLD_ALL | A_PROTECTED

operator = "##" # FIXME generate this automatically

rules = {
"SlotSequence[]": "SlotSequence[1]",
"MakeBoxes[SlotSequence[n_Integer?Positive],"
Expand Down
24 changes: 24 additions & 0 deletions mathics/builtin/functional/apply_fns_to_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

class Apply(InfixOperator):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Apply.html</url>
<dl>
<dt>'Apply[$f$, $expr$]'
Expand Down Expand Up @@ -67,6 +71,8 @@ class Apply(InfixOperator):
summary_text = "apply a function to a list, at specified levels"
grouping = "Right"

operator = "@@" # FIXME generate this automatically

options = {
"Heads": "False",
}
Expand Down Expand Up @@ -100,6 +106,9 @@ def callback(level):

class Map(InfixOperator):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Map.html</url>
<dl>
<dt>'Map[$f$, $expr$]' or '$f$ /@ $expr$'
<dd>applies $f$ to each part on the first level of $expr$.
Expand Down Expand Up @@ -155,6 +164,9 @@ def callback(level):

class MapAt(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/MapAt.html</url>
<dl>
<dt>'MapAt[$f$, $expr$, $n$]'
<dd>applies $f$ to the element at position $n$ in $expr$. If $n$ is negative, the position is counted from the end.
Expand Down Expand Up @@ -211,6 +223,9 @@ def eval(self, f, expr, args, evaluation: Evaluation):

class MapIndexed(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/MapIndexed.html</url>
<dl>
<dt>'MapIndexed[$f$, $expr$]'
<dd>applies $f$ to each part on the first level of $expr$, including the part positions in the call to $f$.
Expand Down Expand Up @@ -280,6 +295,9 @@ def callback(level, pos: Iterable):

class MapThread(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/MapThread.html</url>
<dl>
<dt>'MapThread[$f$, {{$a1$, $a2$, ...}, {$b1$, $b2$, ...}, ...}]
<dd>returns '{$f$[$a1$, $b1$, ...], $f$[$a2$, $b2$, ...], ...}'.
Expand Down Expand Up @@ -367,6 +385,9 @@ def walk(args, depth=0):

class Scan(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Scan.html</url>
<dl>
<dt>'Scan[$f$, $expr$]'
<dd>applies $f$ to each element of $expr$ and returns 'Null'.
Expand Down Expand Up @@ -417,6 +438,9 @@ def callback(level):

class Thread(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Thread.html</url>
<dl>
<dt>'Thread[$f$[$args$]]'
<dd>threads $f$ over any lists that appear in $args$.
Expand Down

0 comments on commit 0b12250

Please sign in to comment.