Velocity Reviews > adding 2 times together

Grant Wagner
Guest
Posts: n/a

 09-30-2004
rh wrote:

> > > x = x < 10 ? "0" + +x : "" +x;
> > >

> <...>
> >
> > The second + seems unnecessary in the latter.

>
> It's there to allow for a string with extraneous leading zeros, e.g.,
> input of "09", which would be returned as "009" otherwise.
>
> > I use
> > function LZ(x) { return (x<0||x>=10?"":"0") + x }

>
> Which is good, probably optimal -- to be improved only by making it a
> prototype .

But allows for a string with extraneous leading zeros:

function LZ(x) { return (x<0||x>=10?"":"0") + x }

This could be corrected with:

function LZ(x) { return (x<0||x>=10?"":"0") + +x }

Alternatively:

String.prototype.LZ = function() { return (+this).LZ(); }
Number.prototype.LZ = function() { return (this < 0 || this >= 10 ? "" : "0")
+ this; }

--
Grant Wagner <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq

Dr John Stockton
Guest
Posts: n/a

 09-30-2004
JRS: In article <(E-Mail Removed)>, dated Thu, 30
Sep 2004 16:01:28, seen in news:comp.lang.javascript, Grant Wagner
<(E-Mail Removed)> posted :
>rh wrote:

JRS wrote :
>> > I use
>> > function LZ(x) { return (x<0||x>=10?"":"0") + x }

>>
>> Which is good, probably optimal -- to be improved only by making it a
>> prototype .

>
>But allows for a string with extraneous leading zeros:
>
>function LZ(x) { return (x<0||x>=10?"":"0") + x }

As I said : the programmer knows what the parameter will be like. My LZ
is for use on Numbers, not on strings. '09' is not a Number, it is a
String.

In the rare (not happened yet) case that the item to be processed by LZ
is or might be a string, I'd use LX(+x).

To stretch a string, I would use something like
function Prfx(Q, L, c) { var s = Q+"" // ??
// if (!c) var c = ' '
if (c.length>0) while (s.length<L) { s = c+s } ;
return s }

--
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

rh
Guest
Posts: n/a

 09-30-2004
Grant Wagner wrote:
> rh wrote:
>
> > > > x = x < 10 ? "0" + +x : "" +x;

<...>
>
> But allows for a string with extraneous leading zeros:
>

Actually, as intended though. It was a dubious attempt to match the
behaviour string input to an expression given by JRS -- in which it
turns out, he had no intention of allowing strings as input. So, time
to chuckle, perhaps, and move on.

However, I think I may ask for the criteria next time, including what
is meant by "optimal"

../rh

rh
Guest
Posts: n/a

 10-01-2004
Dr John Stockton wrote:

> JRS: In article <(E-Mail Removed) >,
> dated Wed, 29 Sep 2004 19:34:50, seen in news:comp.lang.javascript, rh
> <(E-Mail Removed)> posted :
>
> > But perhaps in a typeless language, in the overwhelming
> >majority of cases, it shouldn't be necessary for the programmer to
> >differentiate between the two.

>
> It is trivial, efficient, and not necessary.
>
> > If it is, then it seems most sensible
> >to present the utility a prototype of the core object (not that you've
> >suggested anything to the contrary).

>
> To suit all cases, ISTM that it would need to be added to String and
> Number. It's briefer to use LZ(D.getDate()) than D.getDate().LZ(), too!
>

LZ is simply a case in point.

To me, it's much more a matter of an attempt to match the language,
because in providing recommended service utilities you're really
extending the language beyond what is provided in the base.

So, the two choices I see are tightly couple to a type through
prototype, or write a function that isn't tied to a type and provides
reasonable output based on "reasonable" input.

After all, it's this sort of flexibility in the language that allows
you to write such things as you have, e.g.:

'0'.substring(X>=10)

where the substring parameter is a logical value, yet you get a
well-defined and reasonable result (through implicit coercion to the
type the method actually understands). The same could be done with LZ,
with little or no cost in efficiency, when called with an integer
type.

Even if the decision is to prototype, the function could be made
common to the types that it supports.

<...>

> >Somewhat surprisingly, it appears not to be.

>
> It would become
> function addZero(x) { if (x.length < 2) x = '0' + x ; return x }
>
> function addZero(x) { return x.length < 2 ? '0' + x : x }
>
> is perhaps better, though.
>
> > And that's what I meant
> >when saying "optimal" is hard to achieve as it depends on which JS
> >engine is performing the computation. Outside IE, computation
> >efficiency of that expression in several of the more popular browsers
> >(even with String() omitted) ranges in my tests from slow to dismal.

>
> In comparison with ???

A number of others given in the earlier posts.

>
> >The point is that it is non-trivial to assess computational efficiency
> >by simply looking at the operations in an expression.

>
> Indeed. The easiest way is to post it here, claim it as good, and wait
> for results.

Ah, yes, see who takes the bait!

../rh

Dr John Stockton
Guest
Posts: n/a

 10-01-2004
JRS: In article <(E-Mail Removed) >,
dated Thu, 30 Sep 2004 16:35:08, seen in news:comp.lang.javascript, rh
<(E-Mail Removed)> posted :
>
>However, I think I may ask for the criteria next time, including what
>is meant by "optimal"

As a practical definition, any member of the set of solutions that
cannot be shown to be non-optimal, "cannot" being judged by "has not".

--
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

rh
Guest
Posts: n/a

 10-02-2004
Dr John Stockton wrote:
> JRS: In article <(E-Mail Removed) >,
> dated Thu, 30 Sep 2004 16:35:08, seen in news:comp.lang.javascript, rh
> <(E-Mail Removed)> posted :
> >
> >However, I think I may ask for the criteria next time, including what
> >is meant by "optimal"

>
> As a practical definition, any member of the set of solutions that
> cannot be shown to be non-optimal, "cannot" being judged by "has not".

I'm onto your game -- you'd like to see me blow my (recursive) stack!
Didn't work.

../rh