0382
2021 年12 月 14 日 03:02
1
我知道julia中的运算符和函数本质上没有区别,但是比如
julia> ×(x, y) = x * y
× (generic function with 1 method)
julia> 3×4
12
julia> ∂(x,y) = x + y
∂ (generic function with 1 method)
julia> 2 ∂ 3
ERROR: syntax: extra token "∂" after end of expression
Stacktrace:
[1] top-level scope at none:1
\times
自动的就可以作为中缀运算符,但是\partial
就不可以。在哪里可以查到所有可以作为中缀运算符的符号呢?
xgdgsc
2021 年12 月 14 日 06:16
2
目前能否作为中缀运算符是在 parser 中手动指定的,并同时指定优先级。
确有需要可以提 issue,让开发团队添加新符号。
;; Operator precedence table, lowest at top
; for most operators X there is a .X "elementwise" equivalent
(define (add-dots ops) (append! ops (map (lambda (op) (symbol (string "." op))) ops)))
(define prec-assignment
(append! (add-dots '(= += -= −= *= /= //= |\\=| ^= ÷= %= <<= >>= >>>= |\|=| &= ⊻= ≔ ⩴ ≕))
(add-dots '(~))
'(:= $=)))
;; comma - higher than assignment outside parentheses, lower when inside
(define prec-pair (add-dots '(=>)))
(define prec-conditional '(?))
(define prec-arrow (add-dots '(← → ↔ ↚ ↛ ↞ ↠ ↢ ↣ ↦ ↤ ↮ ⇎ ⇍ ⇏ ⇐ ⇒ ⇔ ⇴ ⇶ ⇷ ⇸ ⇹ ⇺ ⇻ ⇼ ⇽ ⇾ ⇿ ⟵ ⟶ ⟷ ⟹ ⟺ ⟻ ⟼ ⟽ ⟾ ⟿ ⤀ ⤁ ⤂ ⤃ ⤄ ⤅ ⤆ ⤇ ⤌ ⤍ ⤎ ⤏ ⤐ ⤑ ⤔ ⤕ ⤖ ⤗ ⤘ ⤝ ⤞ ⤟ ⤠ ⥄ ⥅ ⥆ ⥇ ⥈ ⥊ ⥋ ⥎ ⥐ ⥒ ⥓ ⥖ ⥗ ⥚ ⥛ ⥞ ⥟ ⥢ ⥤ ⥦ ⥧ ⥨ ⥩ ⥪ ⥫ ⥬ ⥭ ⥰ ⧴ ⬱ ⬰ ⬲ ⬳ ⬴ ⬵ ⬶ ⬷ ⬸ ⬹ ⬺ ⬻ ⬼ ⬽ ⬾ ⬿ ⭀ ⭁ ⭂ ⭃ ⭄ ⭇ ⭈ ⭉ ⭊ ⭋ ⭌ ← → ⇜ ⇝ ↜ ↝ ↩ ↪ ↫ ↬ ↼ ↽ ⇀ ⇁ ⇄ ⇆ ⇇ ⇉ ⇋ ⇌ ⇚ ⇛ ⇠ ⇢ ↷ ↶ ↺ ↻ --> <-- <-->)))
(define prec-lazy-or (add-dots '(|\|\||)))
(define prec-lazy-and (add-dots '(&&)))
(define prec-comparison
(append! '(in isa)
(add-dots '(> < >= ≥ <= ≤ == === ≡ != ≠ !== ≢ ∈ ∉ ∋ ∌ ⊆ ⊈ ⊂ ⊄ ⊊ ∝ ∊ ∍ ∥ ∦ ∷ ∺ ∻ ∽ ∾ ≁ ≃ ≂ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≐ ≑ ≒ ≓ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≣ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯ ≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿ ⊀ ⊁ ⊃ ⊅ ⊇ ⊉ ⊋ ⊏ ⊐ ⊑ ⊒ ⊜ ⊩ ⊬ ⊮ ⊰ ⊱ ⊲ ⊳ ⊴ ⊵ ⊶ ⊷ ⋍ ⋐ ⋑ ⋕ ⋖ ⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟ ⋠ ⋡ ⋢ ⋣ ⋤ ⋥ ⋦ ⋧ ⋨ ⋩ ⋪ ⋫ ⋬ ⋭ ⋲ ⋳ ⋴ ⋵ ⋶ ⋷ ⋸ ⋹ ⋺ ⋻ ⋼ ⋽ ⋾ ⋿ ⟈ ⟉ ⟒ ⦷ ⧀ ⧁ ⧡ ⧣ ⧤ ⧥ ⩦ ⩧ ⩪ ⩫ ⩬ ⩭ ⩮ ⩯ ⩰ ⩱ ⩲ ⩳ ⩵ ⩶ ⩷ ⩸ ⩹ ⩺ ⩻ ⩼ ⩽ ⩾ ⩿ ⪀ ⪁ ⪂ ⪃ ⪄ ⪅ ⪆ ⪇ ⪈ ⪉ ⪊ ⪋ ⪌ ⪍ ⪎ ⪏ ⪐ ⪑ ⪒ ⪓ ⪔ ⪕ ⪖ ⪗ ⪘ ⪙ ⪚ ⪛ ⪜ ⪝ ⪞ ⪟ ⪠ ⪡ ⪢ ⪣ ⪤ ⪥ ⪦ ⪧ ⪨ ⪩ ⪪ ⪫ ⪬ ⪭ ⪮ ⪯ ⪰ ⪱ ⪲ ⪳ ⪴ ⪵ ⪶ ⪷ ⪸ ⪹ ⪺ ⪻ ⪼ ⪽ ⪾ ⪿ ⫀ ⫁ ⫂ ⫃ ⫄ ⫅ ⫆ ⫇ ⫈ ⫉ ⫊ ⫋ ⫌ ⫍ ⫎ ⫏ ⫐ ⫑ ⫒ ⫓ ⫔ ⫕ ⫖ ⫗ ⫘ ⫙ ⫷ ⫸ ⫹ ⫺ ⊢ ⊣ ⟂ ⫪ ⫫ <: >:))))
(define prec-pipe< '(|.<\|| |<\||))
(define prec-pipe> '(|.\|>| |\|>|))
该文件已被截断。 显示原文
注:
两个 |
之间的视为单一的符号,\|
转义表示原始的竖线。
例如:|\|\||
==> ||
和 |\||
==> |
add-dots
和手动加前缀点 .
代表可以用作逐元素(elementwise)操作
例如:|..|
==> .
和 |.<\||
==> <|
自定义中缀运算
Custom infix operators can now be defined by appending Unicode combining marks, primes, and sub/superscripts to other operators. For example, +̂ₐ″
is parsed as an infix operator with the same precedence as +
(#22089 ).
—— julia/HISTORY.md · JuliaLang/julia
已打开 01:39PM - 17 Jun 16 UTC
speculative
parser
There is a discussion at https://groups.google.com/forum/#!topic/julia-dev/FmvQ3… Fj0hHs about creating a syntax for custom infix operators.
...
Edited to add note: @johnmyleswhite has pointed out that the comment thread below is an invitation to bikeshedding. Please refrain from new comments unless you have something truly new to add. There are several proposals below, marked by "hooray" emoticons (exploding cone). You can use those icons to skip discussion and just read the proposals, or to find the different proposals so you can vote "thumbs up" or "thumbs down".
Up/downvotes on this bug as a whole are about whether you think that Julia should have any custom infix idiom. Up/downvotes for the specific idea below should go on @Glen-O's first comment. (The bug had 3 downvotes and 1 upvote before that was clarified.)
...
Initial proposal (historical interest only):
The proposal that seems to have won out is:
```
a |>op<| b #evaluates (in the short term) and parses (in the long term) to `op(a,b)`
```
In order to have this work, there are only minor changes necessary:
- Put the precedence of `<|` above that of `|>`, instead of being the same.
- Make `<|` group left-to-right.
- Make the function `<|(a,b...)=(i...)->a(i...,b...)`. (as pointed out in the discussion thread, this would have standalone uses, as well as its use in the above idiom)
Optional:
- create new functions `>|(a...,b)=(i...)->b(a...,i...)` and `|<(a,b...)=a(b...)` with appropriate precedences and grouping.
- Pipe first means evaluation, and pipe last maintains it as a function, while the > and < indicate which one is the function.
- create new functions `>>|(a...,b)=(i...)->b(i...,a...)` and `<<|(a,b...)=(i...)->a(b...,i...)` with appropriate precedence and grouping.
- create synonyms `»`, `⁍`, and(/or) `pipe` for `|>`; `«`, `⁌`, and(/or) `rcurry` for `<|`; and(/or) `lcurry` for `<<|`; with the single-character synonyms working as infix operators.
- create an `@infix` macro in base which does the first parser fix below.
Long term:
- teach the parser to change `a |>op<| b` to `op(a,b)`, so there's no extra overhead involved when running the code, and so that operators can actually be defined in infix position. (This is similar to how the parser currently treats the binary `a:b` and the ternary `a:b:c` differently. For maximum customizability, it should do this for matched synonyms, but not for unmatched synonyms, so that e.g. `a |> b « c` would be still be treated as two binary operators.)
- teach the parser to understand commas and/or spaces so that the ellipses in the above definitions work as expected without extra parentheses.
(relates to https://github.com/JuliaLang/julia/issues/6946)
3 个赞