Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23539 > unrolled thread
| Started by | JRough <janis.rough@gmail.com> |
|---|---|
| First post | 2014-03-11 15:36 -0700 |
| Last post | 2014-03-12 15:34 +0100 |
| Articles | 14 — 6 participants |
Back to article view | Back to comp.lang.javascript
class needs to initialize vars, set data structure JRough <janis.rough@gmail.com> - 2014-03-11 15:36 -0700
Re: class needs to initialize vars, set data structure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 03:34 +0100
Re: class needs to initialize vars, set data structure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 15:09 +0100
Re: class needs to initialize vars, set data structure John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-12 17:04 +0000
Re: class needs to initialize vars, set data structure John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-14 10:48 +0000
Re: class needs to initialize vars, set data structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-12 03:17 +0000
Re: class needs to initialize vars, set data structure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 11:46 +0100
Re: class needs to initialize vars, set data structure Christoph Michael Becker <cmbecker69@arcor.de> - 2014-06-25 22:51 +0200
Re: class needs to initialize vars, set data structure Christoph Michael Becker <cmbecker69@arcor.de> - 2014-06-26 00:42 +0200
Re: class needs to initialize vars, set data structure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-26 11:16 +0200
Re: class needs to initialize vars, set data structure Christoph Michael Becker <cmbecker69@arcor.de> - 2014-06-26 21:44 +0200
Re: class needs to initialize vars, set data structure John Harris <niam@jghnorth.org.uk.invalid> - 2014-06-27 09:46 +0100
Re: class needs to initialize vars, set data structure Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-28 21:57 -0700
Re: class needs to initialize vars, set data structure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 15:34 +0100
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2014-03-11 15:36 -0700 |
| Subject | class needs to initialize vars, set data structure |
| Message-ID | <972c7156-b68b-4ff4-8d82-55f00676b1a7@googlegroups.com> |
I am trying to make a set data structure. Storing the items in the set should use a private variable. My question is how to initialize it? And also I get a syntax error: missing : after property ID
thanks,
var Set = function() {
var obj = {}; //private assoc arr var to store some key in the set
var input = "input";
obj[nput].push(o);
return {
add : function(o){ this[o] = true; }, // returns true if the key was added, false otherwise
remove : function(o){delete this[o]; }, //deletes the key form the set, returns true if the key was removed
contains : function(o){ return this.hasOwnProperty(o); }, //returns true if the key is in the set
count = function(o){ //returns the number of keys in the set
return length(obj[input].keys(o)){
}
};
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 03:34 +0100 |
| Message-ID | <6465521.OizMrxDySb@PointedEars.de> |
| In reply to | #23539 |
Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> var Set = function()
Note that, according to the latest working draft, ECMAScript Edition 6 is
going to define a Set object type, so it is wise to test whether “Set”
already exists before redefining it.
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set>
>> { var map = {};
>> return
>> { add : function( element ){ map[ element ]= true; },
This breaks existing properties because the prototype chain of Object
instances is _not_ empty; they inherit from the object referred from
Object.prototype (the Object prototype [object]).
>> remove : function( element ){ delete map[ element ]; },
This could delete built-in non-inherited properties.
>> contains: function( element ){ return map.hasOwnProperty( element );
This would consider built-in non-inherited properties.
>> },
>> count : function( element ){ return Object.keys( map ).length; }
This would count only enumerable own properties, contradicting .contains().
>> }};
This would return an Object instance from a constructor call where a Set
instance would be expected.
Compare <http://PointedEars.de/wsvn/JSX/trunk/map.js>.
> The style above, I sometimes call »module style«.
> Usually I will prefer the style below, which I sometimes call »prototype
> style«:
>
> function Set(){ this.map = {}; }
> Set.prototype.add = function( element ){ this.map[ element ]= true;
> };
> Set.prototype.remove = function( element ){ delete this.map[ element ];
> }; Set.prototype.contains = function( element ){ return
> this.map.hasOwnProperty( element ); };
> Set.prototype.count = function( element ){ return Object.keys( this.map
> ).length; };
The latter style that you prefer not only is tedious to write, both styles
are also less efficient than others. Granted, the latter style is more
compatible than the above, but only if you consider implementations older
than 16 years to be still relevant. Other than that, it has no obvious
advantages, but it has less obvious disadvantages. BTDT.
<http://PointedEars.de/es-matrix/?filter=%7B>
Also, a set is _not_ a map, and vice-versa. A set is a data structure that
contains no duplicate *values*; a map is a data structure that contains no
duplicate *keys*.
Your ongoing delusions of grandeur speaking from your postings are tiresome
to say the least; it would be acceptable if you acquired a significant
amount of humility. Rest assured that your preferences and terminology are
largely irrelevant to readers as you are evidently not experienced enough to
serve as an authority on this topic.
--
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]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 15:09 +0100 |
| Message-ID | <2749015.oGmWMc4bhc@PointedEars.de> |
| In reply to | #23559 |
Thomas 'PointedEars' Lahn wrote:
> Stefan Ram wrote:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> var Set = function()
> […]
>>> { var map = {};
>>> return
>>> { add : function( element ){ map[ element ]= true; },
> […]
>>> remove : function( element ){ delete map[ element ]; },
> […]
>>> contains: function( element ){ return map.hasOwnProperty( element );
> […]
>>> },
>>> count : function( element ){ return Object.keys( map ).length; }
> […]
>>> }};
> […]
> Compare <http://PointedEars.de/wsvn/JSX/trunk/map.js>.
> […]
> Also, a set is _not_ a map, and vice-versa. A set is a data structure
> that contains no duplicate *values*; a map is a data structure that
> contains no duplicate *keys*.
Reviewing this, I see that you are probably aware of that fact as the
“true” indicates that you intended your “Set” factory to work that way.
However, you are not aware of the fact that property names are, and can only
be, primitive String values in ECMAScript. Non-strings are converted to
String for property access. [1] So this works reliably only for String
values, *with the provisions that I named*.
AFAICS, a reliable *and* efficient Set implementation in ECMAScript leads to
a hash table implementation where the key is the string representation of
the value (while avoiding to overwrite built-in properties), and each entry
of the table contains no duplicates. IOW, for this a Map implementation is
required that supports non-strings as keys.
Incidentally, that is what jsx.map.Map() does, so I had used it in
jsx.python.set(), which I had completely forgotten about:
<http://PointedEars.de/wsvn/JSX/trunk/python.js>
<http://PointedEars.de/scripts/test/python>
(This code is on “trunk”, therefore largely untested and not stable;
bug reports are appreciated.)
__________
[1] <http://ecma-international.org/ecma-262/5.1/#sec-11.2.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]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-03-12 17:04 +0000 |
| Message-ID | <hp41i95rr0npq86ru29n8ifpfg3ffs475q@4ax.com> |
| In reply to | #23559 |
On Wed, 12 Mar 2014 03:34:11 +0100, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: <snip> >A set is a data structure that >contains no duplicate *values*; <snip> Actually, this is an Urban Myth. In mathematics, the Is_A_Member_Of relation for sets, as in x Is_A_Member_Of y , can only be False or True. If False then x is not a member of y. If True then x is a member of y at least once. It could be a member once, or billions of times, or an infinite number of times. As there is NO OTHER WAY of looking inside y then you just can't tell. Luckily, you don't care so it doesn't matter. In computing, on the other hand, it is often convenient to restrict x to being inside y at most once. This can make things more efficient and less annoying, but it's a development choice, not a law of nature. John
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-03-14 10:48 +0000 |
| Message-ID | <2dn5i95ctgh58a3j3gv5g7dcvvqrknukia@4ax.com> |
| In reply to | #23576 |
On 13 Mar 2014 10:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote: >John Harris <niam@jghnorth.org.uk.invalid> writes: >>If True then x is a member of y at least once. It could be a member >>once, or billions of times, or an infinite number of times. > > In mathematics, the phrases »it is a member /once/« or »it > is a member /billions of times/« have no meaning, so phrases > with it make no sense. Not only in mathematics, but also to > me, these phrases have no meaning. I do not know what you > mean by »[being ]a member /billions of times«/, so I cannot > understand your above sentence. In a way you are right. "Is_A_Member_Of" is the name of a relation whose behaviour is restricted by the Set axioms, but the name has no special meaning at all. So talking about being inside a set is meaningless, which is essentially what I was saying. However, it is often convenient to think of sets as containers, so here's an empty container : ( ) and here's one that isn't empty : ( 3 3 ). 3 Is_A_Member_Of this container, but how many times it's a member is not a question that can be answered. Note that ( 3 3 ) can arise when implementing (finite) sets using a simple list. Care has to be taken when removing 3 to remove all instances of 3, otherwise you're not obeying the set axioms. Incidentally, your last sentence means you agree that it's meaningless to say : "A set is a data structure that contains no duplicate *values*". John
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-03-12 03:17 +0000 |
| Message-ID | <0.22105c4cd91aee1e4818.20140312031736GMT.87y50gjeb3.fsf@bsb.me.uk> |
| In reply to | #23539 |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> JRough <janis.rough@gmail.com> writes:
>>var Set = function() {
>
> var Set = function()
> { var map = {};
> return
> { add : function( element ){ map[ element ]= true; },
> remove : function( element ){ delete map[ element ]; },
> contains: function( element ){ return map.hasOwnProperty( element ); },
> count : function( element ){ return Object.keys( map ).length; }}};
>
> (The »error« with the brace after the return is intended.
> I will not allow JavaScript to dictate my bracing style!)
So what do you do? Do you sit and admire it, or do you have an
implementation that can run it?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 11:46 +0100 |
| Message-ID | <1518416.lQCqFX8JqD@PointedEars.de> |
| In reply to | #23564 |
Stefan Ram wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>> var Set = function()
>>> { var map = {};
>>> return
>>> { add : function( element ){ map[ element ]= true; },
>>> remove : function( element ){ delete map[ element ]; },
>>> contains: function( element ){ return map.hasOwnProperty( element );
>>> },
>>> count : function( element ){ return Object.keys( map ).length; }}};
>>>
>>> (The »error« with the brace after the return is intended.
>>> I will not allow JavaScript to dictate my bracing style!)
>> So what do you do? Do you sit and admire it, or do you have an
>> implementation that can run it?
>
> I made a note once that reads:
>
> return \
> { ...
>
> , and I remember that this did work somewhere,
Not in any ECMAScript implementation I have come across within now 17 years
of experience.
A remote possibility is that this was part of a /Program/ evaluated with
eval(), in which case it could have worked if the implementation supported
what ECMAScript Edition 5 (2009) now specifies: multi-line string literals.
eval("function f () { return \
{}; }");
Of course, both eval() and this notation is to be avoided.
<http://PointedEars.de/es-matrix?filter=%5C>
> but now it turned out it does not work in my browser.
Apparently you have yet to take the important step in the learning curve of
thinking in terms of implementations instead of in browsers.
> However, when my published program still contains errors, I
> do not risk the accusation of doing other people's homework!
Or you could be considered a wannabe/troll.
--
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]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-06-25 22:51 +0200 |
| Message-ID | <53ab3668$0$6698$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #23564 |
Stefan Ram wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> return
>>> { add : function( element ){ map[ element ]= true; },
>> So what do you do? Do you sit and admire it, or do you have an
>> implementation that can run it?
>
> I have found that instead of
>
> return 1
>
> I can write
>
> return +
> 1
>
> , and instead of
>
> return {}
>
> I can write
>
> return 0,
> {}
>
> . Maybe someone sees a possibility how to shorten the »0,«
> to just a single character (like the »+« that can be used in
> the case of following numbers)?
Yes. You can use a { character (and omit that in the following line):
return {
}
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-06-26 00:42 +0200 |
| Message-ID | <53ab503d$0$6699$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #25029 |
Stefan Ram wrote:
> Christoph Michael Becker <cmbecker69@arcor.de> writes:
>> Stefan Ram wrote:
>>> I can write
>>> return 0,
>>> {}
>>> . Maybe someone sees a possibility how to shorten the »0,«
>>> to just a single character (like the »+« that can be used in
>>> the case of following numbers)?
>> Yes. You can use a { character (and omit that in the following line):
>> return {
>> }
>
> Yes, but I was looking for a single character
> to replace the »0,« with the rest of the code
> left unchanged.
Why? Just to stick with your preferred brace style, tricking out the
automatic semicolon insertion?
Even if there is such a trick (that I'm not aware of), don't you think
it'll make your code harder to understand for others? To quote Martin
Fowler[1]:
| Any fool can write code that a computer can understand. Good
| programmers write code that humans can understand.
[1]
<http://www.cs.umss.edu.bo/doc/material/mat_gral_137/M.Fowler%20et%20al%20-%20Refactoring%20-%20Improving%20the%20Design%20of%20Existing.pdf>
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-26 11:16 +0200 |
| Message-ID | <1552924.UEShXEnhKH@PointedEars.de> |
| In reply to | #25032 |
Stefan Ram wrote: > Christoph Michael Becker <cmbecker69@arcor.de> writes: >> Why? Just to stick with your preferred brace style, tricking out the >> automatic semicolon insertion? >> Even if there is such a trick (that I'm not aware of), don't you think >> it'll make your code harder to understand for others? > > The formatting too is part of the message I want to convey, > so to me it would not be the same message anymore. > > If someone is paying me to write code, he can get any format > from me that he likes. But on the Usenet I enjoy the freedom > to use the indentation style that I deem best, Why? Because when it is free and in their free time, people are more likely to accept (your) gibberish? Think twice. > [300+ lines of "explanations"] You cannot be serious. -- 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]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-06-26 21:44 +0200 |
| Message-ID | <53ac781f$0$6608$9b4e6d93@newsspool4.arcor-online.net> |
| In reply to | #25032 |
Stefan Ram wrote:
> Christoph Michael Becker <cmbecker69@arcor.de> writes:
>> Why? Just to stick with your preferred brace style, tricking out the
>> automatic semicolon insertion?
>> Even if there is such a trick (that I'm not aware of), don't you think
>> it'll make your code harder to understand for others?
>
> The formatting too is part of the message I want to convey,
> so to me it would not be the same message anymore.
I understand that personal coding style is something that one could take
very serious, and I'm not interested in starting a war.
However, IMO it is not sensible to use hard to understand tricks, such as
return 0,
{}
just to be able to use one's preferred coding style. If the language
doesn't allow one's preferred coding style, one should either use
another language, or adapt his coding style to match the language. For
instance, try your bracing style with Pascal.
> Moreover, why would anyone sane in his mind compose a line
> of the head of a structure /and the first character of its
> body/ with the rest of the body following in another line?
Ap parently, most programmers nowadays are not "sane in their minds". At
least most of the Java guys. SMH.
> from Kernighan's B tutorial from 1972:
>
> if(---)
> { -----
> ----- }
> else if(---)
> { -----
> ----- }
> else if(----)
> { -----
> ----- }
Interestingly, in 1978 this very Brian Kernighan wrote a book together
with Dennis Ritchie, in which the so-called K&R bracing style was used
for all examples. Do you think, Kernighan went insane in the meantime?
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-06-27 09:46 +0100 |
| Message-ID | <nmbqq9lujj0376dkthdpfeicme8i9onc1k@4ax.com> |
| In reply to | #25050 |
On Thu, 26 Jun 2014 21:44:37 +0200, Christoph Michael Becker <cmbecker69@arcor.de> wrote: <snip> >Interestingly, in 1978 this very Brian Kernighan wrote a book together >with Dennis Ritchie, in which the so-called K&R bracing style was used >for all examples. Do you think, Kernighan went insane in the meantime? This bracing style uses the minimum amount of paper. Is it a co-incidence that the book's publisher saved money with this style ? If you look at Bjarne Stroustrup's books on C++ you'll see that he uses at least three different styles without insisting that one of them is best. John
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2014-06-28 21:57 -0700 |
| Message-ID | <f0e1a287-9521-4fc2-ab25-d9dcfdd3825c@googlegroups.com> |
| In reply to | #25094 |
John Harris wrote: > If you look at Bjarne Stroustrup's books on C++ you'll see that he > uses at least three different styles without insisting that one of > them is best. If you look at Bjarne Stroustrup's language, C++, you'll see that he uses at least thirty-three different paradigms without insisting that one of them is best or in fact noticing that they may well not play very nicely together. :-) -- Scott
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 15:34 +0100 |
| Message-ID | <3504411.n7x7zaDXQ0@PointedEars.de> |
| In reply to | #23539 |
Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> function Set(){ this.map = {}; }
>
> Next version:
>
> function Set(){ this.map = Object.create( null ); };
This makes the “map” property public, allowing everyone to mess with the
set. It also requires an implementation of ECMAScript Edition 5 or later,
or an emulation of Object.create().
> Set.prototype =
> { add : function( element ){ this.map[ element ]= true; } ,
> remove : function( element ){ delete this.map[ element ]; },
> contains : function( element ){ return element in this.map; },
Again, these work reliably only for String values where the value is not the
name of a *non-inherited* built-in property, or for non-String values where
the string representation of the value is no such name.
> count : function( element ){ return Object.keys( this.map ).length;
> }};
This would prevent the methods from being created on each invocation of
Set(), however it overwrites the existing “prototype” property so that the
“constructor” property of the object referred to from the “prototype”
property value refers to the same object as “Object”, not as “Set” (because
it now refers to an Object instance). It also allows everyone to mess with
the methods because they are public.
Instead of bothering everyone with your repetitions of common mistakes, you
should read previous discussions before you post, and follow-ups to your
postings more carefully. And RTFM, RTFFAQ, STFW.
--
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] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web