Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User-defined infix operator

Tags:

julia

I know operators in Julia are just standard functions, and I can use them using the ordinary prefix call syntax:

julia> +(1, 2)
3

However, they are also special in the sense that they can be (and are usually) used as infix operators:

julia> 1+2
3


Could I define my own infix operator? If so, how?

For example:

julia> α(x, y) = x+y
α (generic function with 1 method)

julia> α(1, 2)
3 # as expected

julia> 1α2
# expected result: 3
ERROR: UndefVarError: α2 not defined
Stacktrace:
 [1] top-level scope at REPL[5]:1

julia> 1 α 2
# expected result: 3
ERROR: syntax: extra token "α" after end of expression
Stacktrace:
 [1] top-level scope at REPL[5]:0
like image 432
François Févotte Avatar asked Feb 20 '20 13:02

François Févotte


People also ask

What is operator in infix?

infix operator (plural infix operators) (computing) an operator that is placed in between the operands like it is commonly used in arithmetical and logical formulae and statements. The plus sign in "2 + 2" is placed as an infix operator in arithmetic.

How do you write an infix operator?

This function can be used as infix operator a %divisible% b or as a function call `%divisible%`(a, b) . Both are the same. Things to remember while defining your own infix operators are that they must start and end with % . Surround it with back tick ( ` ) in the function definition and escape any special symbols.

How do you define an infix function?

What are infix functions? In R, most of the functions are “prefix” - meaning that the function name comes before the arguments, which are put between parentheses : fun(a,b) . With infix functions, the name comes between the arguments a fun b . In fact, you use this type on a daily basis with : , + , and so on.

How do I define infix operator Haskell?

There's no way to define a function with an alphanumeric name as infix. Haskell's syntax rules only allow for functions with symbolic names or function names surrounded with backticks to be used infix - there's no way to change that.


1 Answers

As you said, operators are just standard functions, which you can define and otherwise manipulate like any other function. However, Julia's parser is configured to recognize a certain set of symbols as infix operators; if you define a function whose name is one of these symbols, it will be parsed as an infix operator.

For example:

julia> ⊕(x, y) = x+y
⊕ (generic function with 1 method)

# standard prefix function call
julia> ⊕(1, 2)
3

# infix operator call
julia> 1⊕2
3

julia> 1 ⊕ 2
3


The list of symbols recognized as infix operators (and associated precedence) can be found in the Julia parser source code. For the most part, this list is a subset of unicode category Sm(Symbol, math).

At the moment, it includes for example:

  • parsed with the same precedence as +:
+ - ⊕ ⊖ ⊞ ⊟ ∪ ∨ ⊔ ± ∓ ∔ ∸ ≏ ⊎ ⊻ ⊽ ⋎ ⋓ ⧺ ⧻ ⨈ ⨢ ⨣ ⨤ ⨥ ⨦
⨧ ⨨ ⨩ ⨪ ⨫ ⨬ ⨭ ⨮ ⨹ ⨺ ⩁ ⩂ ⩅ ⩊ ⩌ ⩏ ⩐ ⩒ ⩔ ⩖ ⩗ ⩛ ⩝ ⩡ ⩢ ⩣
  • parsed with the same precedence as *:
* / ÷ % & ⋅ ∘ × ∩ ∧ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ ⊓ ∗ ∙ ∤ ⅋ ≀ ⊼ ⋄ ⋆ ⋇
⋉ ⋊ ⋋ ⋌ ⋏ ⋒ ⟑ ⦸ ⦼ ⦾ ⦿ ⧶ ⧷ ⨇ ⨰ ⨱ ⨲ ⨳ ⨴ ⨵ ⨶ ⨷ ⨸ ⨻
⨼ ⨽ ⩀ ⩃ ⩄ ⩋ ⩍ ⩎ ⩑ ⩓ ⩕ ⩘ ⩚ ⩜ ⩞ ⩟ ⩠ ⫛ ⊍ ▷ ⨝ ⟕ ⟖ ⟗
like image 140
François Févotte Avatar answered Sep 20 '22 03:09

François Févotte