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


Groups > comp.lang.javascript > #26651 > unrolled thread

function to get frequencies of a char array

Started byJRough <janis.rough@gmail.com>
First post2015-01-22 14:43 -0800
Last post2015-01-23 17:09 +0100
Articles 20 on this page of 39 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 14:43 -0800
    Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-22 23:09 +0000
      Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:22 -0800
    Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:23 -0800
    Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 00:36 +0100
      Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 14:27 +0100
        Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 16:52 +0100
          Re: function to get frequencies of a char array Scott Sauyet <scott.sauyet@gmail.com> - 2015-01-23 09:03 -0800
          Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 23:03 +0100
          Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 14:29 +0000
            Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-24 15:36 +0100
              Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-24 16:06 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-24 18:05 +0100
                  Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:21 +0100
                    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 13:22 +0100
                      Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 15:50 +0100
                        Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 20:48 +0100
                          Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 23:08 +0100
                          Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:05 +0100
                            Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 13:55 +0100
                              Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 14:50 +0100
                                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 19:35 +0100
                                  Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 22:15 +0100
                                    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-31 00:40 +0100
              Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 17:49 +0000
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:23 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:00 +0100
                  Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-27 01:37 +0000
                    Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 03:03 +0100
      Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 15:29 +0100
    Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:16 +0000
      Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:18 +0000
      Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 09:06 +0100
        Re: function to get frequencies of a char array Stefan Weiss <krewecherl@gmail.com> - 2015-01-23 09:53 +0100
          Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 13:37 +0100
            Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:49 +0100
      Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:31 +0100
      Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 14:49 +0000
      Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 17:09 +0100

Page 1 of 2  [1] 2  Next page →


#26651 — function to get frequencies of a char array

FromJRough <janis.rough@gmail.com>
Date2015-01-22 14:43 -0800
Subjectfunction to get frequencies of a char array
Message-ID<dec04bcb-c8c2-4446-b82c-6c3fa7e05e48@googlegroups.com>
the output should be:
a:1,b:1,j:4,c:1,d:1

I am trying to use a map and use the keys as the char and the values as the count but I'm getting undefined.
thanks,


function frequencies(){
  var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
  var key, value;
    for(i=0; i< arr.length; i++){
        var myMap = new Map();    
           if (arr[i]==arr[i+1]){
              myMap.set(key=i, value= value+1)
            }else{
              myMap.set(key=i,"1")
            };  
     };

} 
frequencies();  

[toc] | [next] | [standalone]


#26652

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-01-22 23:09 +0000
Message-ID<877fwetntb.fsf@bsb.me.uk>
In reply to#26651
JRough <janis.rough@gmail.com> writes:

> the output should be:
> a:1,b:1,j:4,c:1,d:1

The code does no output.  You mean these should be the value in the map,
yes?

> I am trying to use a map and use the keys as the char and the values
> as the count but I'm getting undefined.  thanks,

This looks like coursework, so my intuition is just to ask some
questions that I hope will lead you to an answer.

> function frequencies(){
>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>   var key, value;
>     for(i=0; i< arr.length; i++){

You should declare i as well.  You can do it in the for statement.

>         var myMap = new Map();
>            if (arr[i]==arr[i+1]){

This will access beyond the end of the array and I can't see what it has
to do with counting.  You want to set the map entry to 1 when you see a
letter for the first time, not when you see a letter that is different
to the one that follows it.

The key thing here is that you get the value out of the map.  That is
what tells you if you have seen this letter before.

>               myMap.set(key=i, value= value+1)

value has not been set.  value = value + 1 sets value to one mode than
undefined.  Not what you mean at all.

>             }else{
>               myMap.set(key=i,"1")

Why "1" rather than 1?  Why key=i rather than i?  Is i even the right
key?

>             };  
>      };

The function returns nothing, so it has no point.

> } 
> frequencies();  

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26653

FromJRough <janis.rough@gmail.com>
Date2015-01-22 15:22 -0800
Message-ID<aa3c77bf-840b-453d-85a0-33b82a600b6f@googlegroups.com>
In reply to#26652
I removed the map.  It isn't homework.  It was an interview question come up by some wiley programmer :-)  IT doesn't like my arry assignment.
 var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
   function frequencies(arr){
 var arry = arr;
  
    for(i=0; i< arry.length; i++){
         var key, value, i;
           if (arry[i]==arry[i+1]){
            var kvArray = [[key=i,value=value+1 ]];
            }else{
              kyArray = [[key=i, "1"]];
            }; 
            return kyArray; 
     };

} 
frequencies(); 
frequencies(); 


On Thursday, January 22, 2015 at 3:09:44 PM UTC-8, Ben Bacarisse wrote:
> JRough <janis.rough@gmail.com> writes:
> 
> > the output should be:
> > a:1,b:1,j:4,c:1,d:1
> 
> The code does no output.  You mean these should be the value in the map,
> yes?
> 
> > I am trying to use a map and use the keys as the char and the values
> > as the count but I'm getting undefined.  thanks,
> 
> This looks like coursework, so my intuition is just to ask some
> questions that I hope will lead you to an answer.
> 
> > function frequencies(){
> >   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> >   var key, value;
> >     for(i=0; i< arr.length; i++){
> 
> You should declare i as well.  You can do it in the for statement.
> 
> >         var myMap = new Map();
> >            if (arr[i]==arr[i+1]){
> 
> This will access beyond the end of the array and I can't see what it has
> to do with counting.  You want to set the map entry to 1 when you see a
> letter for the first time, not when you see a letter that is different
> to the one that follows it.
> 
> The key thing here is that you get the value out of the map.  That is
> what tells you if you have seen this letter before.
> 
> >               myMap.set(key=i, value= value+1)
> 
> value has not been set.  value = value + 1 sets value to one mode than
> undefined.  Not what you mean at all.
> 
> >             }else{
> >               myMap.set(key=i,"1")
> 
> Why "1" rather than 1?  Why key=i rather than i?  Is i even the right
> key?
> 
> >             };  
> >      };
> 
> The function returns nothing, so it has no point.
> 
> > } 
> > frequencies();  
> 
> -- 
> Ben.

[toc] | [prev] | [next] | [standalone]


#26654

FromJRough <janis.rough@gmail.com>
Date2015-01-22 15:23 -0800
Message-ID<64781793-eddb-4fa1-b98f-f1815e362fe2@googlegroups.com>
In reply to#26651
 var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
   function frequencies(arr){
 var arry = arr;
  
    for(i=0; i< arry.length; i++){
         var key, value, i;
           if (arry[i]==arry[i+1]){
            var kvArray = [[key=i,value=value+1 ]];
            }else{
              kyArray = [[key=i, "1"]];
            }; 
            return kyArray; 
     };

} 
output =frequencies(); 
On Thursday, January 22, 2015 at 2:43:24 PM UTC-8, JRough wrote:
> the output should be:
> a:1,b:1,j:4,c:1,d:1
> 
> I am trying to use a map and use the keys as the char and the values as the count but I'm getting undefined.
> thanks,
> 
> 
> function frequencies(){
>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>   var key, value;
>     for(i=0; i< arr.length; i++){
>         var myMap = new Map();    
>            if (arr[i]==arr[i+1]){
>               myMap.set(key=i, value= value+1)
>             }else{
>               myMap.set(key=i,"1")
>             };  
>      };
> 
> } 
> frequencies();

[toc] | [prev] | [next] | [standalone]


#26656

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-23 00:36 +0100
Message-ID<XnsA42B6242E020eejj99@194.109.133.133>
In reply to#26651
JRough <janis.rough@gmail.com> wrote on 22 jan 2015 in
comp.lang.javascript: 

> the output should be:
> a:1,b:1,j:4,c:1,d:1
> 
> I am trying to use a map and use the keys as the char and the values as
> the count but I'm getting undefined. thanks,
> 
> 
> function frequencies(){
>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>   var key, value;
>     for(i=0; i< arr.length; i++){
>         var myMap = new Map();    

Do you want a new map for every loop?

>            if (arr[i]==arr[i+1]){

eh?

>               myMap.set(key=i, value= value+1)
>             }else{
>               myMap.set(key=i,"1")
>             };  
>      };

Don't you want to use the result somewhere?

>} 
> frequencies();  
> 

Try this, using an object as target:

=============================
var arr = 'abjcdjjj'.split(''); // source array
var res = {};                   // result object

function frequencies() {
  for(i=0; i< arr.length; i++) {
    if (!res[arr[i]])
      res[arr[i]] = 0;
    res[arr[i]] +=1;
  };
}; 
frequencies();  

console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
=============================


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26665

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-23 14:27 +0100
Message-ID<m9ti7r$sbg$1@solani.org>
In reply to#26656
Evertjan. wrote:

> Try this, using an object as target:
> 
> =============================
> var arr = 'abjcdjjj'.split(''); // source array
> var res = {};                   // result object
> 
> function frequencies() {
>   for(i=0; i< arr.length; i++) {
>     if (!res[arr[i]])
>       res[arr[i]] = 0;
>     res[arr[i]] +=1;
>   };
> }; 
> frequencies();  
> 
> console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
> =============================

Or maybe using Array.prototype.reduce:

    var a = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
    a.reduce(function (prev, curr) {
        if (prev.hasOwnProperty(curr)) {
            prev[curr]++;
        } else {
            prev[curr] = 1;
        }
        return prev;
    }, {});

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26670

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-23 16:52 +0100
Message-ID<26839541.MRbOrngyXZ@PointedEars.de>
In reply to#26665
Christoph M. Becker wrote:

> Evertjan. wrote:
>> Try this, using an object as target:
>> 
>> =============================
>> var arr = 'abjcdjjj'.split(''); // source array
>> var res = {};                   // result object
>> 
>> function frequencies() {
>>   for(i=0; i< arr.length; i++) {
>>     if (!res[arr[i]])
>>       res[arr[i]] = 0;
>>     res[arr[i]] +=1;
>>   };
>> };
>> frequencies();
>> 
>> console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
>> =============================
> 
> Or maybe using Array.prototype.reduce:
> 
>     var a = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>     a.reduce(function (prev, curr) {
>         if (prev.hasOwnProperty(curr)) {
>             prev[curr]++;
>         } else {
>             prev[curr] = 1;
>         }
>         return prev;
>     }, {});

While this works – but I would use “Object.create(null)” instead of “{}” to 
avoid name collisions –, I find a “for” or “for–in” statement more obvious 
and compatible:

  var counts = {};

  for (var i = 0, len = a.length; i < len; ++i)
  {
    /* NOTE: hasOwnProperty() may be required */
    var e = a[i];
    e ? ++counts[e] : (counts[e] = 1);
  }

  for (var i in a)
  {
    /* NOTE: hasOwnProperty() may be required */
    var e = a[i];
    e ? ++counts[e] : (counts[e] = 1);
  }

“for–in” is more efficient than “for“ and Array.prototype.forEach() with 
sparse arrays.

Array.prototype.forEach() can be used instead if it is supported and the 
array is insufficiently sparse:

  a.forEach(function (e, i, a) {
    var counts = a.counts_ || (a.counts_ = Object.create(null));
    (counts[e]) ? ++counts[e] : (counts[e] = 1);
  });
  
  a.counts_

The advantage of using the Array instance as container is that the scope 
chain is shorter, which is faster.  However, that method, too, can be passed 
a “this” value:

  var counts = Object.create(null);

  a.forEach(
    function (e) { (this[e]) ? ++this[e] : (this[e] = 1); },
    counts);
  
  counts

Array.prototype.reduce(), if I use it (it needs to be emulated in case it is 
not available), I reserve for getting a single *primitive* value from an 
Array instead.  For example:

  var sum = a.reduce(function (prev, curr) { return prev + (+curr); }, 0);

(Implicit type conversion is unnecessary if all elements are of type 
Number.)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26673

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-01-23 09:03 -0800
Message-ID<99ac21de-42d7-48ac-afc7-b32d4c02cc95@googlegroups.com>
In reply to#26670
Thomas 'PointedEars' Lahn wrote:

> Array.prototype.reduce(), if I use it [ ... ], I reserve for getting a
> single *primitive* value from an Array instead.  For example:
> 
>   var sum = a.reduce(function (prev, curr) { return prev + (+curr); }, 0);

Why is that?  Given the universality of `fold` [1], it's clear that other
list iteration techniques such as `map` and `filter` could be implemented
atop `fold` (`reduce`).  So it's broadly useful.  I've never seen an
argument for using it only for primitive returns.  Most people I've known
use it for much more.

Would you be willing to supply your rationale?


  [1]: http://www.cs.nott.ac.uk/~gmh/fold.pdf

  -- Scott

[toc] | [prev] | [next] | [standalone]


#26675

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-23 23:03 +0100
Message-ID<m9ugeh$di7$2@solani.org>
In reply to#26670
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>> Or maybe using Array.prototype.reduce:
>>
>>     var a = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>>     a.reduce(function (prev, curr) {
>>         if (prev.hasOwnProperty(curr)) {
>>             prev[curr]++;
>>         } else {
>>             prev[curr] = 1;
>>         }
>>         return prev;
>>     }, {});
> 
> While this works – but I would use “Object.create(null)” instead of “{}” to 
> avoid name collisions –, 

Good point!

> I find a “for” or “for–in” statement more obvious 
> and compatible:

I had deliberately ignored compatibility with older environments (i.e.
not ES 5 compatible), but of course, that may be an issue.  And I have
to admit that I would find a for(-in) loop more obvious, too, but it
seems to me that is a matter of habit.

Anyhow, I mainly wanted to point out that current ECMAScript
implementations offer interesting tools for functional/applicative
programming which might be worth consideration -- especially for long
time ECMAScript programmers who might not be aware of these tools.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26687

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-01-24 14:29 +0000
Message-ID<87twzgl0aq.fsf@bsb.me.uk>
In reply to#26670
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
<snip>
> While this works – but I would use “Object.create(null)” instead of “{}” to 
> avoid name collisions –, I find a “for” or “for–in” statement more obvious 
> and compatible:
>
>   var counts = {};
>
>   for (var i = 0, len = a.length; i < len; ++i)
>   {
>     /* NOTE: hasOwnProperty() may be required */
>     var e = a[i];
>     e ? ++counts[e] : (counts[e] = 1);
>   }

I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
immediately below):

>   for (var i in a)
>   {
>     /* NOTE: hasOwnProperty() may be required */
>     var e = a[i];
>     e ? ++counts[e] : (counts[e] = 1);
>   }

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26689

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-24 15:36 +0100
Message-ID<2697158.aGRKFDBAEn@PointedEars.de>
In reply to#26687
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>   var counts = {};
>>
>>   for (var i = 0, len = a.length; i < len; ++i)
>>   {
>>     /* NOTE: hasOwnProperty() may be required */
>>     var e = a[i];
>>     e ? ++counts[e] : (counts[e] = 1);
>>   }
> 
> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
> immediately below): […]

No, but

  if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26690

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-24 16:06 +0100
Message-ID<XnsA42CA3CBC8610eejj99@194.109.133.133>
In reply to#26689
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 24 jan 2015 in 
comp.lang.javascript:

> No, but
> 
>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }

if (e) counts[e] = (counts[e]|0) + 1;

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26692

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-24 18:05 +0100
Message-ID<ma0jd2$5kg$1@solani.org>
In reply to#26690
Evertjan. wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 24 jan 2015 in 
> comp.lang.javascript:
> 
>> No, but
>>
>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> if (e) counts[e] = (counts[e]|0) + 1;

That has slightly different semantics, which probably won't matter in
practise, though.  Nonetheless I would prefer the following (if I'd use
a shortcut at all):

  if (e) counts[e] = (counts[e] || 0) + 1;

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26700

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-26 10:21 +0100
Message-ID<XnsA42E6953CC277eejj99@194.109.133.246>
In reply to#26692
"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 24 jan 2015 in 
comp.lang.javascript:

> Evertjan. wrote:
> 
>> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 24 jan 2015 in 
>> comp.lang.javascript:
>> 
>>> No, but
>>>
>>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
>> 
>> if (e) counts[e] = (counts[e]|0) + 1;
> 
> That has slightly different semantics, which probably won't matter in
> practise, though.  Nonetheless I would prefer the following (if I'd use
> a shortcut at all):
> 
>   if (e) counts[e] = (counts[e] || 0) + 1;

Indeed, 
but if count[] is either undefined or numeric,
as in this case, it does not matter.

[and | is a shorter shortcut than || ]

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26702

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-26 13:22 +0100
Message-ID<ma5bha$6q1$1@solani.org>
In reply to#26700
Evertjan. wrote:

> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 24 jan 2015 in 
> comp.lang.javascript:
> 
>> Evertjan. wrote:
>>
>>> if (e) counts[e] = (counts[e]|0) + 1;
>>
>> That has slightly different semantics, which probably won't matter in
>> practise, though.  Nonetheless I would prefer the following (if I'd use
>> a shortcut at all):
>>
>>   if (e) counts[e] = (counts[e] || 0) + 1;
> 
> Indeed, 
> but if count[] is either undefined or numeric,
> as in this case, it does not matter.

The semantic difference I was referring to is the internal call of
ToInt32()[1].  Compare:

  1e32 | 0 // => 0
  1e32 || 0 // => 1e+32

[1] <http://www.ecma-international.org/ecma-262/5.1/#sec-11.10>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26703

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-26 15:50 +0100
Message-ID<XnsA42EA12D52C8Eeejj99@194.109.133.246>
In reply to#26702
"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 jan 2015 in 
comp.lang.javascript:

> Evertjan. wrote:
> 
>> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 24 jan 2015 in 
>> comp.lang.javascript:
>> 
>>> Evertjan. wrote:
>>>
>>>> if (e) counts[e] = (counts[e]|0) + 1;
>>>
>>> That has slightly different semantics, which probably won't matter in
>>> practise, though.  Nonetheless I would prefer the following (if I'd use
>>> a shortcut at all):
>>>
>>>   if (e) counts[e] = (counts[e] || 0) + 1;
>> 
>> Indeed, 
>> but if count[] is either undefined or numeric,
>> as in this case, it does not matter.
> 
> The semantic difference I was referring to is the internal call of
> ToInt32()[1].  Compare:
> 
>   1e32 | 0 // => 0
>   1e32 || 0 // => 1e+32
> 
> [1] <http://www.ecma-international.org/ecma-262/5.1/#sec-11.10>

This was about a count, 
in Javascript you cannot 
in a single variable
count from 0 to 1e32
for reasons of time and resolution. 



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26712

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-26 20:48 +0100
Message-ID<ma65ls$auj$1@solani.org>
In reply to#26703
Evertjan. wrote:

> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 jan 2015 in 
> comp.lang.javascript:
> 
>> The semantic difference I was referring to is the internal call of
>> ToInt32()[1].  Compare:
>>
>>   1e32 | 0 // => 0
>>   1e32 || 0 // => 1e+32
>>
>> [1] <http://www.ecma-international.org/ecma-262/5.1/#sec-11.10>
> 
> This was about a count, 
> in Javascript you cannot 
> in a single variable
> count from 0 to 1e32
> for reasons of time and resolution. 

I agree that is impractical to have 1e32 iterations on current hardware.
 However, I don't understand what you mean by "resolution".

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26717

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-26 23:08 +0100
Message-ID<XnsA42EEB7522BEeejj99@194.109.133.246>
In reply to#26712
"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 jan 2015 in 
comp.lang.javascript:

> Evertjan. wrote:
> 
>> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 jan 2015 in 
>> comp.lang.javascript:
>> 
>>> The semantic difference I was referring to is the internal call of
>>> ToInt32()[1].  Compare:
>>>
>>>   1e32 | 0 // => 0
>>>   1e32 || 0 // => 1e+32
>>>
>>> [1] <http://www.ecma-international.org/ecma-262/5.1/#sec-11.10>
>> 
>> This was about a count, 
>> in Javascript you cannot 
>> in a single variable
>> count from 0 to 1e32
>> for reasons of time and resolution. 
> 
> I agree that is impractical to have 1e32 iterations on current hardware.
>  However, I don't understand what you mean by "resolution".

The floating point variable has not enough resolution to be able to contain 
different integer numbers that differ 1 when the number gets large.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26721

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-27 01:05 +0100
Message-ID<10619254.HIfIgzSeSj@PointedEars.de>
In reply to#26712
Christoph M. Becker wrote:

> Evertjan. wrote:
>> "Christoph M. Becker" […] wrote […]:
>>> The semantic difference I was referring to is the internal call of
>>> ToInt32()[1].  Compare:
>>>
>>>   1e32 | 0 // => 0
>>>   1e32 || 0 // => 1e+32
>>>
>>> [1] <http://www.ecma-international.org/ecma-262/5.1/#sec-11.10>
>> 
>> This was about a count,
>> in Javascript you cannot
>> in a single variable
>> count from 0 to 1e32
>> for reasons of […] resolution.
> 
> […]
>  However, I don't understand what you mean by "resolution".

The largest integer that can be represented with IEEE 754 “double precision” 
floating-point values as used in the ECMAScript Number type such that all 
smaller integers greater than or equal to ±0 can be represented exactly, is 
2⁵³ = 9'007'199'254'740'992 (the mantissa has 52 bits and the leading 1 is 
implied.)  Implementations of ECMAScript Ed. 6 are going to make that value 
available with the *successor* of the value of the Number.MAX_SAFE_INTEGER 
property:

<https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER> 
p.

The aforementioned values are approximately 9 × 10¹⁵ ≪ 10³².

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26730

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-27 13:55 +0100
Message-ID<ma81sf$c7u$1@solani.org>
In reply to#26721
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>> Evertjan. wrote:
>>
>>> This was about a count,
>>> in Javascript you cannot
>>> in a single variable
>>> count from 0 to 1e32
>>> for reasons of […] resolution.
>>
>> […]
>>  However, I don't understand what you mean by "resolution".
> 
> The largest integer that can be represented with IEEE 754 “double precision” 
> floating-point values as used in the ECMAScript Number type such that all 
> smaller integers greater than or equal to ±0 can be represented exactly, is 
> 2⁵³ = 9'007'199'254'740'992 (the mantissa has 52 bits and the leading 1 is 
> implied.)  Implementations of ECMAScript Ed. 6 are going to make that value 
> available with the *successor* of the value of the Number.MAX_SAFE_INTEGER 
> property:
> 
> <https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER> 
> p.
> 
> The aforementioned values are approximately 9 × 10¹⁵ ≪ 10³².

Thanks for the explanation.

Actually, I screwed up again, as I meant 2³² (not 10³²), which can be
represented exactly as integer, but doesn't "survive" the ToInt32()
call.  Even 2³¹ does not:

  Math.pow(2, 31) | 0 // => -2147483648

As we were talking about array *indexes*[1], values equal to or beyond
2³² are irrelevant, anyway.

[1] <news:2697158.aGRKFDBAEn@PointedEars.de>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web