如何定义类似/@的运算符

发布于 2024-11-01 18:38:26 字数 486 浏览 0 评论 0原文

我想定义一个形式为 x /==> 的新运算符y,其中 运算符 /==> 被视为 Map/@ 运算符,并且 被转换为 MyFunction[x, y]。有一个重要的方面:我 希望结果运算符在前端的行为像任何两位一样 运算符执行此操作,即两个字符(DivideDoubleLongRightArrow) 应该连接在一起,没有语法 应该出现颜色,并且在选择时要一起选择 单击,因此必须设置优先级。另外,我宁愿避免使用 Notation` 包。因此,我希望看到这样的内容:

In[11]:= FullForm[x/\[DoubleLongRightArrow]y]

Out[11]//FullForm= MyFunction[x,y]

有人知道如何实现这一目标吗?

I would like to define a new operator of the form x /==> y, where
the operator /==> is treated as e.g. the /@ operator of Map, and
is translated to MyFunction[x, y]. There is one important aspect: I
want the resulting operator to behave in the frontend like any two-bit
operator does, that is, the two characters (a Divide and a
DoubleLongRightArrow) should be connected together, no syntax
coloration should appear, and they are to be selected together when
clicked, so precedence must be set. Also, I'd rather avoid using the
Notation` package. As a result, I'd like to see something like this:

In[11]:= FullForm[x/\[DoubleLongRightArrow]y]

Out[11]//FullForm= MyFunction[x,y]

Does anyone have an idea how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-11-08 18:38:26

Notation Package 可能是最接近做这种事情的,但根据我自己的类似性质的问题的回复,你想要的是不幸的是不实用。

但是,不要让这阻止您尝试,因为您可能会在此过程中学到新东西。符号包和支撑它的函数绝非无用。

您还可能会发现此问题的回复内容丰富。


有许多函数可用于手动实施语法更改。我不会尝试为这些功能编写自己的帮助文件,而是引导您访问这些功能的官方页面。阅读后,请提出您有的任何重点问题,或寻求实施具体想法的帮助。我或这里的其他人应该能够回答您的问题,向您展示如何做某事,或者解释为什么它不容易实现。

还有更多,稍后我将尝试扩展此列表。 (欢迎其他人编辑这篇文章

The Notation Package is perhaps the closest to doing this kind of thing, but according to the response to my own question of a similar nature, what you want is unfortunately not practical.

Don't let this stop you from trying however, as you will probably learn new things in the process. The Notation Package and the the functions that underpin it are far from useless.

You may also find the replies to this question informative.


There are a number of functions that are useful for manual implementation of syntax changes. Rather than try to write my own help file for these, I will direct you to the official pages on these functions. After reading them, please ask any focused questions you have, or for help with implementing specific ideas. I or others here should be able to either answer your question, show you how to do something, or explain why it is not readily possible.

There are more, and I will try to extend this list later. (others are welcome to edit this post)

不离久伴 2024-11-08 18:38:26

感谢 Mr.Wizard 的链接,我在文档中找到了关于如何解析新运算符的唯一示例(低级输入中的 gplus 示例)。根据这个例子,这是我的新运算符 PerArrow 的版本。请对下面的代码发表评论/批评:

In[1]:= PerArrow /: MakeBoxes[PerArrow[x_, y_], StandardForm] := 
  RowBox[{MakeBoxes[x, StandardForm], 
    RowBox[{AdjustmentBox["/", BoxMargins -> -.2], 
      AdjustmentBox["\[DoubleLongRightArrow]", BoxMargins -> -.1]}], 
    MakeBoxes[y, StandardForm]}];

MakeExpression[
   RowBox[{x_, "/", RowBox[{"\[DoubleLongRightArrow]", y_}]}], 
   StandardForm] := 
  MakeExpression[RowBox[{"PerArrow", "[", x, ",", y, "]"}], 
   StandardForm];

In[3]:= PerArrow[x, y]

Out[3]= x /\[DoubleLongRightArrow] y

In[4]:= x /\[DoubleLongRightArrow]y

Out[4]= x /\[DoubleLongRightArrow] y

In[5]:= FullForm[x /\[DoubleLongRightArrow]y]

Out[5]//FullForm= \!\(\*
TagBox[
StyleBox[
RowBox[{"PerArrow", "[", 
RowBox[{"x", ",", "y"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

为了清楚起见,这里还有一个屏幕截图:
new operator

由于该操作符尚未完全集成,进一步的问题是:

  • 单击时该操作符被选择得很奇怪(DoubleLongRightArrowy 而不是 /)。
  • 因此,解析部分需要将 DoubleLongRightArrow 进行 RowBox-ed 与 y,否则会产生语法错误
  • 语法着色(在 In [4]In[5]
  • 如果直接输入,它会打印奇怪(注意 In[4]In[5] 处的大间隙])

现在,我可以忍受这些,不过如果能有一些方法来解决所有的小问题就好了。我想所有这些基本上都归结为一些更低级别的语法处理程序,它现在不知道如何对新运算符进行分组。关于如何解决这些问题有什么想法吗?据我所知,Cell 有很多可能会派上用场的选项(例如 CellEvaluationFunctionShowAutoStylesInputAutoReplacements)我又在这里一无所知了。

Thanks to Mr.Wizard's links, I've found the only example in the documentation on how to parse new operators (the gplus example in Low-Level Input). According to this example, here is my version for the new operator PerArrow. Please comment/critize on the code below:

In[1]:= PerArrow /: MakeBoxes[PerArrow[x_, y_], StandardForm] := 
  RowBox[{MakeBoxes[x, StandardForm], 
    RowBox[{AdjustmentBox["/", BoxMargins -> -.2], 
      AdjustmentBox["\[DoubleLongRightArrow]", BoxMargins -> -.1]}], 
    MakeBoxes[y, StandardForm]}];

MakeExpression[
   RowBox[{x_, "/", RowBox[{"\[DoubleLongRightArrow]", y_}]}], 
   StandardForm] := 
  MakeExpression[RowBox[{"PerArrow", "[", x, ",", y, "]"}], 
   StandardForm];

In[3]:= PerArrow[x, y]

Out[3]= x /\[DoubleLongRightArrow] y

In[4]:= x /\[DoubleLongRightArrow]y

Out[4]= x /\[DoubleLongRightArrow] y

In[5]:= FullForm[x /\[DoubleLongRightArrow]y]

Out[5]//FullForm= \!\(\*
TagBox[
StyleBox[
RowBox[{"PerArrow", "[", 
RowBox[{"x", ",", "y"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

For sake of clarity, here is a screenshot as well:
new operator

Since the operator is not fully integrated, further concerns are:

  • the operator is selected weird when clicked (DoubleLongRightArrow with y instead of with /).
  • accordingly, the parsing part requires the DoubleLongRightArrow to be RowBox-ed with y, otherwise it yields syntax error
  • syntax coloration (at In[4] and In[5])
  • it prints weird if inputted directly (notice the large gaps at In[4] and In[5])

Now, I can live with these, though it would be nice to have some means to iron out all the minor issues. I guess all these boil down to basically some even lower-level syntax handler, which does not now how to group the new operator. Any idea on how to tackle these? I understand that Cell has a multitude of options which might come handy (like CellEvaluationFunction, ShowAutoStyles and InputAutoReplacements) though I'm again clueless here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文