Open Source / Collaboration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Cornford

    #16
    Re: Open Source / Collaboration

    Dr John Stockton wrote:
    <snip>[color=blue]
    > While it is entirely appropriate for delivered code that is intended
    > to be read only by browsers to be not indented, code intended to be
    > read or edited by humans should be indented to show its intended
    > logical structure - preferably by two or three spaces per level, and
    > not by a tab. If that is not in the FAQ or the FAQ notes, then it
    > should be.[/color]

    The notes state that *posted* code should be indented with spaces rather
    than tabs, and explain the practical reasons for doing so. I don't think
    I would recommend the same restriction for all code. Different people
    seem to favour different depths of indentation. If they indent with tabs
    and set their text editors up so that tabs are the indent they prefer
    others can do likewise, and all can work on the same code in the comfort
    of their own preferred indentation depths.
    [color=blue]
    > The code uses getElementById. ...[/color]
    <snip>[color=blue]
    > provide code to emulate, if necessary, that function. Such code may
    > be in the FAQ notes; it is in my js-other.htm (taken from a post by
    > Randy).[/color]
    <snip>

    It is. It is on the alternative DynWrite page (as getElementById is used
    there).

    Richard.


    Comment

    • Dr John Stockton

      #17
      Re: Open Source / Collaboration

      JRS: In article <290e65a0.04062 62006.5e693733@ posting.google. com>, seen
      in news:comp.lang. javascript, rh <codeHanger@yah oo.ca> posted at Sat, 26
      Jun 2004 21:06:54 :[color=blue]
      >
      > AnsObj = GenerateRandomS et(M,N);
      > var Ans = [];
      > for (var k in AnsObj) Ans.push( AnsObj[k] );
      > Ans.sort( function (a,b) { return a-b } );[/color]

      MSIE4 does not do well on push - but that's easy to code around.
      The wanted elements are already in order; no _real_ sorting is needed.

      But your k in AnsObj extracts them in historical order, I think; so

      function TestGRS(X, Z, M, N) { var Q, j, k=0, Ans=[]
      Q = GenerateRandomS et(M, N)
      for (j=0 ; j < Q.length ; j++) if (Q[j]) Ans[k++] = Q[j]
      X.value = Q ; Z.value=Ans /* or return Ans */ }

      Q.length appears the highest index used in Q, plus one - I believe that
      to be as it should be.

      However, if the selection is sparse, (k in Q) then sort may be faster.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • rh

        #18
        Re: Open Source / Collaboration

        Dr John Stockton wrote:
        [color=blue]
        > MSIE4 does not do well on push - but that's easy to code around.[/color]

        Right. The usual? :

        if (! Array.prototype .push) {
        Array.prototype .push = function() {
        for(var k=0; k < arguments.lengt h; k++) this[this.length] =
        arguments[k];
        }
        }

        should supplant.
        [color=blue]
        > The wanted elements are already in order; no _real_ sorting is needed.
        >[/color]

        Realized that at some later point and re-worked it into the following
        non-recursive non-push, non-sort dependant (equivalent, I think :))
        function, which has a certain similarity to your revision with regard
        to the array compression:

        function GenerateRandomS et(M,N) {
        var aSet = [], n, J;
        if (M <= N && M > 0) {
        for (var k = 1; k <= M; k++) {
        n = N-M+k;
        J = Math.floor(Math .random()*n+1) ;
        aSet[J] ? aSet[n]= n : aSet[J] = J;
        }
        for (var j=k=0; k<aSet.length; k++) aSet[k]? aSet[j++]=aSet[k]:0;
        aSet.length = j;
        }
        return aSet;
        }
        [color=blue]
        > But your k in AnsObj extracts them in historical order, I think; so
        >
        > function TestGRS(X, Z, M, N) { var Q, j, k=0, Ans=[]
        > Q = GenerateRandomS et(M, N)
        > for (j=0 ; j < Q.length ; j++) if (Q[j]) Ans[k++] = Q[j]
        > X.value = Q ; Z.value=Ans /* or return Ans */ }
        >
        > Q.length appears the highest index used in Q, plus one - I believe that
        > to be as it should be.
        >
        > However, if the selection is sparse, (k in Q) then sort may be faster.[/color]

        Aside from the problems mentioned earlier with Netscape sorting of
        sparse arrays, I found IE to be extremely slow when sorting on large
        M, sparse arrays. Therefore, I think sort would be best left out of
        it.

        The non-recursiveness should be more efficient and avoids recursion
        depth limitations that can occur in some browsers with relatively
        modest-sized N.

        ../rh

        Comment

        • Dr John Stockton

          #19
          Re: Open Source / Collaboration

          JRS: In article <%_BDc.255490$h Y.16585@twister .nyroc.rr.com>, seen in
          news:comp.lang. javascript, Mick White <mwhite13@BOGUS rochester.rr.co m>
          posted at Sun, 27 Jun 2004 15:48:11 :[color=blue]
          >Dr John Stockton wrote:[color=green]
          >> JRS: In article <UhWCc.248625$h Y.72235@twister .nyroc.rr.com>, seen in
          >> news:comp.lang. javascript, Mick White <mwhite13@BOGUS rochester.rr.co m>
          >> posted at Fri, 25 Jun 2004 14:04:36 :[color=darkred]
          >>>For some ideas look at:
          >>>
          >>>http://www.mickweb.com/javascript/ra...y_lottery.html[/color][/color][/color]
          [color=blue]
          >What would you suggest? I use "func" to remind me that the parameter to
          >be passed was to be the return statement of a function.[/color]

          Retn? Ansr? But the routine, IIRC, will handle any string; so maybe
          St or Str?

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
          <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
          <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

          Comment

          • Dr John Stockton

            #20
            Re: Open Source / Collaboration

            JRS: In article <290e65a0.04062 72001.3f5fe771@ posting.google. com>, seen
            in news:comp.lang. javascript, rh <codeHanger@yah oo.ca> posted at Sun, 27
            Jun 2004 21:01:04 :[color=blue]
            >Dr John Stockton wrote:
            >[color=green]
            >> MSIE4 does not do well on push - but that's easy to code around.[/color]
            >
            >Right. The usual? :
            >
            > if (! Array.prototype .push) {
            > Array.prototype .push = function() {
            > for(var k=0; k < arguments.lengt h; k++) this[this.length] =
            >arguments[k];
            > }
            > }
            >
            >should supplant.[/color]

            No need in this case, where only one element is pushed at a time; direct
            code suffices.

            [color=blue][color=green]
            >> The wanted elements are already in order; no _real_ sorting is needed.
            >>[/color]
            >
            >Realized that at some later point and re-worked it into the following
            > ...[/color]
            [color=blue]
            > for (var j=k=0; k<aSet.length; k++) aSet[k]? aSet[j++]=aSet[k]:0;
            > aSet.length = j;[/color]

            Why the :0, or is it just that ? needs : ?
            // At this step, j should have the same value as M.
            [color=blue][color=green]
            >> However, if the selection is sparse, (k in Q) then sort may be faster.[/color]
            >
            >Aside from the problems mentioned earlier with Netscape sorting of
            >sparse arrays, I found IE to be extremely slow when sorting on large
            >M, sparse arrays. Therefore, I think sort would be best left out of
            >it.[/color]

            Above, the data will not be sparse when being sorted.
            [color=blue]
            >The non-recursiveness should be more efficient and avoids recursion
            >depth limitations that can occur in some browsers with relatively
            >modest-sized N.[/color]

            Good.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • rh

              #21
              Re: Open Source / Collaboration

              Dr John Stockton wrote:[color=blue]
              > ...[color=green]
              > > for (var j=k=0; k<aSet.length; k++) aSet[k]? aSet[j++]=aSet[k]:0;
              > > aSet.length = j;[/color]
              >
              > Why the :0, or is it just that ? needs : ?[/color]

              It's just syntax fodder, producing an expression result that is
              discarded when exercised. The "for() if()" syntax you used may well be
              preferable for both clarity and efficiency.
              [color=blue]
              > // At this step, j should have the same value as M.[/color]

              Yes.

              ../rh

              Comment

              • rh

                #22
                Re: Open Source / Collaboration

                Dr John Stockton wrote:[color=blue]
                > ...[color=green]
                > > for (var j=k=0; k<aSet.length; k++) aSet[k]? aSet[j++]=aSet[k]:0;
                > > aSet.length = j;[/color]
                >
                > Why the :0, or is it just that ? needs : ?[/color]

                It's just syntax fodder, producing an expression result that is
                discarded when exercised. The "for() if()" syntax you used may well be
                preferable for both clarity and efficiency.
                [color=blue]
                > // At this step, j should have the same value as M.[/color]

                Yes.

                ../rh

                Comment

                • Dr John Stockton

                  #23
                  Re: Open Source / Collaboration

                  JRS: In article <290e65a0.04062 82107.6c763dd3@ posting.google. com>, seen
                  in news:comp.lang. javascript, rh <codeHanger@yah oo.ca> posted at Mon, 28
                  Jun 2004 22:07:36 :[color=blue]
                  >
                  >It's just syntax fodder, producing an expression result that is
                  >discarded when exercised. The "for() if()" syntax you used may well be
                  >preferable for both clarity and efficiency.[/color]

                  Thanks.

                  In fact, the "compacting " stage is unnecessary, since the elements which
                  are put into aSet can be duplicated, in sequence of creation, in an
                  array bSet. The order of bSet is not fully random, for M>1; and is
                  systematic for M>=N. M<1 needs no specific test (I assume M, N are
                  integer).

                  If needed, the result can be sorted; but sorting should be a separate
                  stage.

                  function GenRndSubSet(M, N) { var aSet = [], bSet=[], j, k, n
                  if (M>N) M = N
                  for (k = 0; k < M; k++) { n = N - M + k + 1
                  j = Math.floor(Math .random()*n + 1)
                  if (aSet[j]) j = n
                  bSet[k] = aSet[j] = j }
                  return bSet }

                  Sorting is convenient if lottery results are to be checked; but not
                  necessary if a decimating centurion's intent is to select ten out of a
                  hundred.

                  <URL:http://www.merlyn.demo n.co.uk/js-randm.htm> is updated, in colour.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk DOS 3.3, 6.20; Win98. ©
                  Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
                  PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
                  My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm> - also batprogs.htm.

                  Comment

                  • rh

                    #24
                    Re: Open Source / Collaboration

                    Dr John Stockton wrote:[color=blue]
                    >...
                    >
                    > <URL:http://www.merlyn.demo n.co.uk/js-randm.htm> is updated, in colour.[/color]

                    One final small note: The line

                    if (M<0) return aSet

                    in RH_GenRndSubSet could be dropped. The returned result for negative
                    M should be an empty array (the same as for M=0).

                    Thanks for the attribution -- does that mean I have to be nice to you
                    now? ;).

                    Seriously, if credit is due, I'd say most of it goes to Lee and to
                    you.

                    ../rh

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #25
                      Re: Open Source / Collaboration

                      Lee wrote:[color=blue]
                      > Lee said:[color=green]
                      >> <script type="text/javascript">
                      >> function lotto(m,n){
                      >> var selected=new Array();
                      >> var hat=new Array(m+1);
                      >> for(while n--){[/color][/color]
                      ^^^^^^^^^^^^^^[color=blue][color=green]
                      >> [...][/color]
                      >
                      > Extra credit: find the bug.[/color]
                      ^^^^^^^^^^^^

                      [x] done

                      As usual, please send it to the account specified
                      on the opposite page of this posting.


                      Regards,
                      PointedEars

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #26
                        Re: Open Source / Collaboration

                        Richard Cornford wrote:[color=blue]
                        > Dr John Stockton wrote:[color=green]
                        >> While it is entirely appropriate for delivered code that is intended
                        >> to be read only by browsers to be not indented, code intended to be
                        >> read or edited by humans should be indented to show its intended
                        >> logical structure - preferably by two or three spaces per level, and
                        >> not by a tab. If that is not in the FAQ or the FAQ notes, then it
                        >> should be.[/color]
                        >
                        > The notes state that *posted* code should be indented with spaces rather
                        > than tabs, and explain the practical reasons for doing so. I don't think
                        > I would recommend the same restriction for all code. Different people
                        > seem to favour different depths of indentation. If they indent with tabs
                        > and set their text editors up so that tabs are the indent they prefer
                        > others can do likewise, and all can work on the same code in the comfort
                        > of their own preferred indentation depths.[/color]

                        That may help for certain text editors. However, with others and when
                        displaying source code outside of text editors, the tab character may
                        have a different meaning, aligning the text to next possible tab grid
                        column of the terminal rather than creating a fixed spacing. Repeatedly
                        this has resulted in bad display on different devices and thus hardly
                        legible code (apart from the fact that there are programming languages
                        where indentation depth is considered to create block structures rather
                        than delimiter characters), so it is best to avoid tab characters as
                        whitespace in source code at all. Space whitespace, usually 2 and/or 4
                        of it, has sufficed for indentation to date.


                        PointedEars

                        Comment

                        • Jim Ley

                          #27
                          Re: Open Source / Collaboration

                          On Sat, 03 Jul 2004 23:38:45 +0200, Thomas 'PointedEars' Lahn
                          <PointedEars@nu rfuerspam.de> wrote:
                          [color=blue]
                          > Repeatedly
                          >this has resulted in bad display on different devices and thus hardly
                          >legible code (apart from the fact that there are programming languages
                          >where indentation depth is considered to create block structures rather
                          >than delimiter characters), so it is best to avoid tab characters as
                          >whitespace in source code at all. Space whitespace, usually 2 and/or 4
                          >of it, has sufficed for indentation to date.[/color]

                          so the first thing I have to do is search and replace those spaces to
                          tabs which has a much worse risk of stuff going wrong than you've
                          suggested - 4 spaces per indent is absolutely unusable to me, and
                          gives no flexibility. tab (but not on usenet)

                          Jim.
                          --
                          comp.lang.javas cript FAQ - http://jibbering.com/faq/

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #28
                            Re: Open Source / Collaboration

                            Jim Ley wrote:[color=blue]
                            > On Sat, 03 Jul 2004 23:38:45 +0200, Thomas 'PointedEars' Lahn
                            > <PointedEars@nu rfuerspam.de> wrote:[color=green]
                            >> Repeatedly this has resulted in bad display on different devices
                            >> and thus hardly legible code (apart from the fact that there are
                            >> programming languages where indentation depth is considered to
                            >> create block structures rather than delimiter characters), so it is
                            >> best to avoid tab characters as whitespace in source code at all.[/color][/color]
                            ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^[color=blue][color=green]
                            >> Space whitespace, usually 2 and/or 4 of it, has sufficed for
                            >> indentation to date.[/color]
                            >
                            > so the first thing I have to do is search and replace those spaces to
                            > tabs [...][/color]

                            I wonder how you get *this* idea?


                            PointedEars

                            Comment

                            • Jim Ley

                              #29
                              Re: Open Source / Collaboration

                              On Sun, 04 Jul 2004 00:30:02 +0200, Thomas 'PointedEars' Lahn
                              <PointedEars@nu rfuerspam.de> wrote:
                              [color=blue]
                              >Jim Ley wrote:[color=green]
                              >> On Sat, 03 Jul 2004 23:38:45 +0200, Thomas 'PointedEars' Lahn
                              >> <PointedEars@nu rfuerspam.de> wrote:[color=darkred]
                              >>> Repeatedly this has resulted in bad display on different devices
                              >>> and thus hardly legible code (apart from the fact that there are
                              >>> programming languages where indentation depth is considered to
                              >>> create block structures rather than delimiter characters), so it is
                              >>> best to avoid tab characters as whitespace in source code at all.[/color][/color]
                              > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^[color=green][color=darkred]
                              >>> Space whitespace, usually 2 and/or 4 of it, has sufficed for
                              >>> indentation to date.[/color]
                              >>
                              >> so the first thing I have to do is search and replace those spaces to
                              >> tabs [...][/color]
                              >
                              >I wonder how you get *this* idea?[/color]

                              Because space whitespace of 4 is too large, I cannot read that
                              indention effectively (my tabwidth is set to 1) it will reduce my
                              coding effectiveness by many orders of magnitude.

                              Jim.
                              --
                              comp.lang.javas cript FAQ - http://jibbering.com/faq/

                              Comment

                              • Dr John Stockton

                                #30
                                Re: Open Source / Collaboration

                                JRS: In article <40E72765.90507 05@PointedEars. de>, seen in
                                news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                                <PointedEars@nu rfuerspam.de> posted at Sat, 3 Jul 2004 23:38:45 :[color=blue]
                                >Richard Cornford wrote:[/color]
                                [color=blue][color=green]
                                >> The notes state that *posted* code should be indented with spaces rather
                                >> than tabs, and explain the practical reasons for doing so. I don't think
                                >> I would recommend the same restriction for all code. Different people
                                >> seem to favour different depths of indentation. If they indent with tabs
                                >> and set their text editors up so that tabs are the indent they prefer
                                >> others can do likewise, and all can work on the same code in the comfort
                                >> of their own preferred indentation depths.[/color][/color]

                                However, if the material is treated in a manner which converts tabs to
                                spaces - and at least some news-posting software does that - then it is
                                necessary first to ensure that the tab stop spacing used in conversion
                                is set to suit both the material and the medium.

                                [color=blue]
                                >That may help for certain text editors. However, with others and when
                                >displaying source code outside of text editors, the tab character may
                                >have a different meaning, aligning the text to next possible tab grid
                                >column of the terminal rather than creating a fixed spacing. Repeatedly
                                >this has resulted in bad display on different devices and thus hardly
                                >legible code (apart from the fact that there are programming languages
                                >where indentation depth is considered to create block structures rather
                                >than delimiter characters), so it is best to avoid tab characters as
                                >whitespace in source code at all. Space whitespace, usually 2 and/or 4
                                >of it, has sufficed for indentation to date.[/color]

                                Evidently you do not understand the difference between not recommending
                                and recommending not. Richard was expressing a liberal position in
                                respect of non-posted code; one sufficiently liberal that only a
                                dirigiste would find occasion to differ.


                                Also, you give an uncommon interpretation of the tab character.

                                In coding, they are commonly used only at the beginning of a line, and a
                                row of leading tab characters have the same effect as the same number of
                                multiple-spaces.

                                But it is intrinsic to the tab concept that it is not fixed width; it
                                occupies one position on the visible line, plus as many more as are
                                needed to reach the next "tab stop". This usage derives, I expect, from
                                mechanical typewriter usage, where tab stops were optional protrusions
                                that could stop the carriage going further. Interpretation of a tab as
                                giving a fixed spacing, independent of position, seems to me to be most
                                unusual, possibly unique.

                                In computing, tab stops are very commonly defaulted to be after each
                                8n'th column. A reader should always consider it likely, if there is
                                nothing to the contrary written, that such a tab setting is what an
                                author had in mind.


                                I have tried indentation by one per logical level; it is too little to
                                suit me. Two or three spaces seems fine; and anything more than four
                                seems to make the code hard to read.


                                In News/Mail, the character } (/inter alia/) should never be in the
                                first column; it is very likely to be taken as implying that the line is
                                a quoted line.

                                --
                                © John Stockton, Surrey, UK. ???@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
                                Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                                Check boilerplate spelling -- error is a public sign of incompetence.
                                Never fully trust an article from a poster who gives no full real name.

                                Comment

                                Working...