Skip to content

Commit b5def72

Browse files
committed
refactor!:Removed the wrapper functions.
feat:Each of the "free functions" has its own string-argument multi-sub.
1 parent 4920c78 commit b5def72

1 file changed

Lines changed: 101 additions & 13 deletions

File tree

lib/CortexJS.rakumod

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,49 +61,127 @@ sub to-latex($expr,
6161
return $bracketed ?? $left-bracket ~ $res ~ $right-bracket !! $res;
6262
}
6363

64+
#==========================================================
65+
# LaTeX pipeline
66+
#==========================================================
67+
68+
# Using LaTeX::Grammar function &latex-parse to detect LaTeX input
69+
my &is-latex-spec = { so latex-parse($_) }
70+
71+
sub latex-pipeline(Str:D $func, Str:D $expr, *@args, *%args) {
72+
my $is-latex = &is-latex-spec($expr);
73+
die 'Cannot parsr the given string as LaTeX code.' unless $is-latex;
74+
75+
my $expr2 = $is-latex ?? parse-latex($expr) !! $expr;
76+
77+
my $res = ::("&{$func}")($expr2, |@args, |%args);
78+
79+
return $is-latex ?? to-latex($res) !! $res;
80+
}
81+
6482
#==========================================================
6583
# Free symbolic functions
6684
#==========================================================
6785

68-
our sub simplify($expr) is export {
86+
#| Simplify an expression, MathJSON or LaTeX.
87+
our proto sub simplify($expr) is export {*}
88+
89+
multi sub simplify(Str:D $expr) {
90+
latex-pipeline('simplify', $expr)
91+
}
92+
93+
multi sub simplify($expr) {
6994
start-ce() without $ce;
7095
return $ce.simplify($expr);
7196
}
7297

73-
our sub assign($id, $expr) is export {
98+
#| Assign to a symbol with name $id the the expression, $expr (MathJSON or LaTeX.)
99+
our proto sub assign($id, $expr) is export {*}
100+
101+
multi sub assign($id, Str:D $expr) {
102+
latex-pipeline('assign', $expr, :$id)
103+
}
104+
105+
multi sub assign($id, $expr) {
106+
start-ce() without $ce;
107+
return $ce.simplify(:$id, $expr);
108+
}
109+
110+
multi sub assign($expr, :$id) {
74111
start-ce() without $ce;
75112
return $ce.simplify(:$id, $expr);
76113
}
77114

78-
our sub evaluate($expr) is export {
115+
#| Evaluate an expression, MathJSON or LaTeX.
116+
our proto sub evaluate($expr) is export {*}
117+
118+
multi sub evaluate(Str:D $expr) {
119+
latex-pipeline('evaluate', $expr)
120+
}
121+
122+
multi sub evaluate($expr) {
79123
start-ce() without $ce;
80124
return $ce.evaluate($expr);
81125
}
82126

83-
our sub N($expr) is export {
127+
#| Numerical value of an expression, MathJSON or LaTeX.
128+
our proto sub N($expr) is export {*}
129+
130+
multi sub N($id, Str:D $expr) {
131+
latex-pipeline('N', $expr)
132+
}
133+
134+
multi sub N($expr) {
84135
start-ce() without $ce;
85136
return $ce.N($expr);
86137
}
87138

88-
our sub expand($expr) is export {
139+
#| Expand an expression, MathJSON or LaTeX.
140+
our proto expand($expr) is export {*}
141+
142+
multi sub expand(Str:D $expr) {
143+
latex-pipeline('expand', $expr)
144+
}
145+
146+
multi sub expand($expr) {
89147
start-ce() without $ce;
90148
return $ce.expand($expr);
91149
}
92150

93-
our sub expandAll($expr) is export {
151+
#| Expand an expression, MathJSON or LaTeX.
152+
our proto sub expandAll($expr) is export {*}
153+
154+
multi sub expandAll(Str:D $expr) {
155+
latex-pipeline('expandAll', $expr)
156+
}
157+
158+
multi sub expandAll($expr) {
94159
start-ce() without $ce;
95160
return $ce.expandAll($expr);
96161
}
97162

98163
our &expand-all is export = &expandAll;
99164

100-
our sub factor($expr) is export {
165+
166+
#| Factor an expression, MathJSON or LaTeX.
167+
our proto sub factor($expr) is export {*}
168+
169+
multi sub factor(Str:D $expr) {
170+
latex-pipeline('factor', $expr)
171+
}
172+
173+
multi sub factor($expr) {
101174
start-ce() without $ce;
102175
return $ce.factor($expr);
103176
}
104177

178+
#| Solve an equation, MathJSON or LaTeX.
105179
our proto sub solve($expr, |) is export {*}
106180

181+
multi sub solve(Str:D $expr, *@args, *%args) {
182+
latex-pipeline('solve', $expr, |@args, |%args)
183+
}
184+
107185
multi sub solve($expr, $vars) {
108186
return solve($expr, :$vars);
109187
}
@@ -118,18 +196,27 @@ multi sub solve($expr) {
118196
return $ce.solve($expr);
119197
}
120198

121-
our sub cortex-js-call($func, $expr) is export {
199+
#| Invoke a function over an expression, MathJSON or LaTeX.
200+
our proto sub cortex-js-call($func, $expr) is export {*}
201+
202+
multi sub cortex-js-call($func, Str:D $expr) {
203+
latex-pipeline('cortex-js-call', $expr, :$func)
204+
}
205+
206+
multi sub cortex-js-call($expr, :$func) {
207+
cortex-js-call($func, $expr)
208+
}
209+
210+
multi sub cortex-js-call($func, $expr) {
122211
start-ce() without $ce;
123212
return $ce.call($func, $expr);
124213
}
125214

126215
#==========================================================
127216
# Wrappers
128217
#==========================================================
129-
130-
# Using LaTeX::Grammar function &latex-parse to detect LaTeX input
131-
my &is-latex-spec = { so latex-parse($_) }
132-
218+
# Not needed but I want to keep it as a reference for now.
219+
#`[
133220
our @wrappers;
134221
135222
our sub wrap-symbolic-subs() {
@@ -162,4 +249,5 @@ our sub unwrap-symbolic-subs() {
162249
return unless @wrappers;
163250
([&simplify, &evaluate, &N, &expand, &expand-all, &expandAll, &factor, &solve] Z @wrappers).map({ $_.head.unwrap($_.tail) });
164251
@wrappers = Empty
165-
}
252+
}
253+
]

0 commit comments

Comments
 (0)