Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23741

Re: Why is it so convoluted to import a .js file in javascript?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Why is it so convoluted to import a .js file in javascript?
Date Sat, 29 Mar 2014 18:57:08 +0100
Organization PointedEars Software (PES)
Lines 158
Message-ID <1658884.GDCoxO0Dda@PointedEars.de> (permalink)
References <d1050ce2-5b06-4bb8-9d69-18cc325b30c0@googlegroups.com> <include-20140327003813@ram.dialup.fu-berlin.de> <ce8b01e4-3142-4b81-b103-aab2a92c0b39@googlegroups.com> <270320141145170829%timstreater@greenbee.net> <d9854441-f57a-4b17-bf72-a934836819f2@googlegroups.com> <XnsA2FDC97BC475Beejj99@194.109.133.133> <e96817ba-43dd-412e-ac44-617254e21546@googlegroups.com> <XnsA2FEBA5B9A273eejj99@194.109.133.133> <00d68cb0-4463-426a-859d-125781cd2f56@googlegroups.com> <power-20140328185800@ram.dialup.fu-berlin.de> <eval-20140329180710@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1396115833 16345 eJwFwYEBwCAIA7CXVKB15wDS/09YEoaNpiPgoVDlCfNudVGyGue0BnuEQZLHHm2t7+C+vPgBQKARzQ== (29 Mar 2014 17:57:13 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sat, 29 Mar 2014 17:57:13 +0000 (UTC)
User-Agent KNode/4.11.5
X-User-ID eJwFwQcBACAIBMBKiDwjjqz+EbzD1aNlolDBYh3OcNeCnDsRuc3vSre94ZOexqgwMNebSiknKBFF13QtfUGnFZY=
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAA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l{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Cancel-Lock sha1:sxg7RqrMZ328i7pA8X0CJSVprRE=
X-NNTP-Posting-Host eJwFwYEBwDAEBMCVCI+O0+D3HyF3sNDo9EA4CGo15VPgYG9Oj5HGHaGrlab0Abo37l9R4AMiXBFo
Xref csiph.com comp.lang.javascript:23741

Show key headers only | View raw


Stefan Ram wrote:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>I just wrote the following evaluator that can evaluate
>>expressions like »6+3*2^3+1«. All its errors and features
>>gaps are meant to be an exercise for the reader.
> 
>   The new version
> 
>       - now correctly treats 2^3^2 right-associatively,
>       - has additional operators "/" and binary "-", and
>       - can deal with multi-digit numerals.
> 
> <!DOCTYPE HTML><html><head><title>Main</title>
> <meta charset="UTF-8"></head><body><pre><code><script>

Your code style, although apparently consistent, still is only barely 
readable.  Your code is very inefficient and error-prone.

The part of the code that does not depend on output belongs in the “head” 
element.
 
> function Scanner( source ){ this.source = source; this.pos = 0; };
> Scanner.prototype =
> { check: function( pattern )
>   { var next = this.source.substring( this.pos, this.pos + 1 );

  var next = this.source.charAt(this.pos);

or in some implementations (especially those of ES5+)

  var next = this.source[this.pos];

<http://PointedEars.de/es-matrix/?filter=string\[>

>     if( pattern.exec( next )){ ++this.pos; return next }
>     else return false; },

It does not make any sense to use regular expressions and at the same time 
to not use the resulting matches.

>   numeral: function()
>   { var next = /[0-9]+/.exec( this.source.substring( this.pos ));
>     this.pos += next[ 0 ].length; return next[ 0 ]; }, };
> 
> var ex = Object.create( null );
> ex[ "^" ]= Math.pow;
> ex[ "*" ]= function( a, b ){ return a * b };
> ex[ "/" ]= function( a, b ){ return a / b };
> ex[ "+" ]= function( a, b ){ return a*1 + b*1 };
> ex[ "-" ]= function( a, b ){ return a - b };

This can be compacted and optimized into

  var ex = Object.create(null, {
    "^": {value: Math.pow},
    "*": {value: function (a, b) { return a * b; }},
    "/": {value: function (a, b) { return a / b; }},
    "+": {value: function (a, b) { return (+a) + (+b); }},
    "-": {value: function (a, b) { return a - b; }}
  });

An important difference to the above is that these properties are read-only 
and not enumerable.  Otherwise the code begs the question why a simple, more 
compatible, Object initialiser would not suffice:

  var ex = {
    // …
  };
 
> var left_associative = Object.create( null );
> left_associative[ "^" ]= false;
> left_associative[ "*" ]= true;
> left_associative[ "/" ]= true;
> left_associative[ "+" ]= true;
> left_associative[ "-" ]= true;

Same here.  In fact, it might be more efficient, and certainly makes more 
sense data-wise, to have the associativity of an operator as the property of 
an operator, not some unrelated object:

  var operators = {
    "…": {
      call: function () { … },
      leftAssociative: …
    },

    /∕ …
  };
 
> function Parser( source ){ this.scanner = new Scanner( source ); };
> Parser.prototype =

As you have been told before, this overwrites the prototype property, 
setting the value of the  “constructor” property of Instance to a reference 
to the value of the “constructor” property of the RHS value.

> { numeral:  function( me ){ return me.scanner.numeral(); },
>   parse: function( me, op, next )
>   { var result = next( me ); var sym;
>     while( sym = me.scanner.check( op ))
>     result = ex[ sym ]( result,
>       ( left_associative[ sym ]? next( me ): me.parse( me, op, next )));
>     return result; },

Completely unmaintainable.  Kids, don't try this at home.

>   power:   function( me ){ return me.parse( me, /[\^]/,  me.numeral ); },
>   product: function( me ){ return me.parse( me, /[*\/]/, me.power   ); },
>   sum:     function( me ){ return me.parse( me, /[+-]/,  me.product ); },
>   start:   function(){     return this.sum( this ); }};

It is pointless and less efficient to pass the “this” value to a function 
called as a method of the “this” value.

  ({
    foo: function (me) {
      return me;
    },

    bar: function () {
      this.foo(this);
    }
  }).bar();

is semantically equivalent to

  ({
    foo: function () {
      return this;
    },

    bar: function () {
      this.foo();
    }
  }).bar();

[“this” has nothing to do with scope.  That it had is a common beginner’s 
misconception.]
 
> function evl( x ){ var parser = new Parser( x ); return parser.start(); }
> 
> function check( x, y )
> { var e = evl( x ); var r = e == y*1;

Because we are dealing with floating-point values, converting to Number 
explicitly with unary “+” is probably more efficient than converting 
implicitly by multiplying by 1.  It is also less likely to be upwards-
incompatible (should there in be an implementation in the future
that overloads “*” as string repetition operator in the future [1]).


[1] cf. <http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-26 16:22 -0700
  Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 01:37 -0700
    Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 11:45 +0000
      Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 09:36 -0700
        Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 17:17 +0000
        Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 19:48 +0100
          Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 20:52 +0100
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:39 -0700
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:59 -0700
            Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 18:19 +0100
              Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 10:41 -0700
                eval() vs. own expression parser (was: Why is it so convoluted to import a .js file in javascript?) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-28 19:11 +0100
                Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-28 21:43 +0100
                Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-29 18:57 +0100
      Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:51 -0700
    Re: Why is it so convoluted to import a .js file in javascript? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-03-27 17:30 +0100
  Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 09:39 +0100
  Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 21:12 +0100
    Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 07:11 -0700
      Re: Why is it so convoluted to import a .js file in javascript? John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-28 14:30 +0000
      Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-28 15:36 +0100
        Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:17 -0700
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:23 -0700
            Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-29 16:42 +0100
    Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 09:02 -0700
      Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-28 19:00 +0100
        Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 11:34 -0700
  Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-27 22:29 +0100

csiph-web