Not entirely sure to understand what you really need and why you can't just perform the arithmetic operations with Perl
If other people are going to use it, I don't really want them to have to learn perl syntax. It looks messy and confusing to the uninitiated, with all the sigils, and the nonstandard mechanism for passing arguments to functions. I don't know, maybe it's just my perception, but say you show someone who has some basic coding experience this code:
sub f {$x=shift; return $x*0.1}
If they've never seen perl before, they're going to wonder what the heck the dollar signs are, and what shift is. The equivalent in Scheme is this:
(define (f x) (* x 0.1))
Here, I think the only possibly mysterious thing would be that you have to realize that it's prefix rather than infix notation. Maybe for zero-surprises pure vanilla syntax, JS wins:
function f(x) {return x*0.1}
However, the only JS interpreter that seems to meet my criteria is Rhino, and it seems to have unacceptable performance for my purposes if I try to start up an interpreter once for each operation:
$ time rhino -e 'function f(x) {return x*0.1}; print(f(34))'
3.4000000000000004
real 0m0.240s
user 0m0.292s
sys 0m0.036s
(Rhino is actually amazingly fast once it starts up -- in my experience it's sometimes faster than perl, and within a factor of 2 or 3 compared to C.)
Another possible option that comes to my mind is to shell out to the bc Unix or Linux utility. bc can be used in non-interactive mode in various ways, including piping (echo "42/7" | bc), shell redirections, and Un*x heredocs.
Nice suggestion, thanks. It's definitely nice in terms of performance and zero-surprises syntax:
time echo "define f(x) {x*0.1}; print f(34)" | bc
3.4
0
real 0m0.002s
user 0m0.000s
sys 0m0.000s
I think the only problem from my point of view might be that I will probably sometimes need to use data structures, which bc doesn't have. E.g., a common application would be that you want to drop the student's lowest quiz score, so you need to pass the list of quiz scores to the function. (Another issue would be that this would make it linux-only.)
|