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.)


In reply to Re^2: Extending a perl program with Scheme, Lua, or JS by bcrowell2
in thread Extending a perl program with Scheme, Lua, or JS by bcrowell2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.