-
Notifications
You must be signed in to change notification settings - Fork 5
/
operator.edh
106 lines (79 loc) · 2.03 KB
/
operator.edh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# to define mid-dot as the function composition operator
# see: https://middot.net for how to input a mid-dot
infixr 9 ( · ) ( f, g ) x => f ( g ( x ) )
show$ ( · )
# %%
method f x x+5
method g x x*3
p = f · g
p|desc
# %%
2 | p
# %%
p $ 2
# %%
p( 2 )
# Some fancy chars can be used in operators
#
# 🔀🔁🔂⏩⏪🔼⏫🔽⏬⏹️
# 🌀📢📣🔔🔕🎵⭕
# 🆔🆘🆚
# ✨
# %%
infixr 5 ( 📣 ) ( lhv, rhv ) {
console.info <| rhv ++ ' is telling ' ++ lhv
return rhv
}
; ( 📣 ) | show
# %%
'a lie' 📣 'a tale' 📣 'the goat'
# %%
operator 1 ( 🆚 ) ( lhv, rhv ) {
console.info <| "🌀 What's the difference?\n "
++ lhv ++ '\n 🆚\n ' ++ rhv
}
; ( 🆚 ) | show
# %%
let ( a, b ) = ( 'Orange', 'Apple', )
a 🆚 b
# %%
# destined to fail
a 🆚 b 🆚 c
# %%
# overide the (++) operator within a dedicated namespace, avoid polluting the
# module scope
namespace comparison'workspace () {
before = 'You' ++ ' and ' ++ 'me'
# for an operator already declared beforehand, the fixity follows existing
# declaration when you use `operator` or `infix` as the keyword to override it
operator (++) ( lhv, rhv ) {
# inside the overriding operator definition, the overridden,
# original operator is available as was before the overide
lhv ++ ' ⭕ ' ++ rhv
}
after = 'You' ++ ' and ' ++ 'me'
}
# obtain a reflective scope object, we can eval expressions in the namespace
# with this scope object
dynamic'workspace = scope( comparison'workspace )
# check the result there
dynamic'workspace.eval( expr
before 🆚 after
)
# %%
# a method or interpreter procedure can be re-declared as some operator, if
# it takes exactly 2 positional arguments
method concat1( lhv, rhv ) {
lhv ++ ' plus ' ++ rhv
}
infixl 3 (>+<) () concat1
interpreter concat2( callerScope, lhe, rhe ) {
callerScope.eval( lhe ) ++ ' and ' ++ callerScope.eval( rhe )
}
infixl 3 (>&<) () concat2
# %%
# all operators can combine together, according to their respective fixity and
# precedence
3*7 >+< 9*9
🆚
3*7 >&< 9*9