Re: Get list of unique words in a string
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Thu, 04 Jun 2009 07:30:10 +0200
kangax wrote:
Thomas 'PointedEars' Lahn wrote:
kangax wrote:
Thomas 'PointedEars' Lahn wrote:You can find a first draft here:
Thomas 'PointedEars' Lahn wrote:I'd love to take a look at your implementation.
Thomas 'PointedEars' Lahn wrote:
That is why I proposed "double hashing". Also, in my "double-hashed" "hashI'd still go for this solution instead, it is much more robust.
table" implementation (that I wrote this morning) I use trailing underscores
instead because it is less likely that such property names are already used.
<http://pointedears.de/scripts/test/map>
Constructive comments are welcome.
Is jsx.object available publicly? I see that Map is using its `isMethod`
only (?) but I would be interested to look at it overall.
Yes, consider the `script' elements. (It's a draft as well. `jsx.object'
is going to be the additional "namespace" provided by the next version of
object.js to avoid in-library incompatibilities with foreign scripts that
may declare/define the same identifiers. `jsx' is going to be the
additional "namespace" for all libraries of PointedEars' JavaScript
Extensions (as I have come to call it) for the same reason. Maybe I will
eventually abandon the global "namespace", but it is going to be supported
for a while longer for compatibility.)
A couple of questions regarding implementation:
1) Why do you declare functions in Map which don't use any of map
instance private variables? _hasOwnProperty, _maxAliasLength, _Value,
_Value.isInstance and few others could all be taken out of constructor
into the enclosing "wrapping" scope. Declaring them in Map seems
unnecessary and inefficient. Why waste time and memory redeclaring same
function objects over and over again?
_maxAliasLength is not a function but a "private property", so (AFAIK) it
must be declared in the constructor, accessible only through "public"
methods. _hasOwnProperty() accesses the "private" _items. And _Value()
(and _Value.isInstance) are defined within the constructor so that they are
unique for each Map object.
var Map = (function(){
...
var _hasOwnProperty = (function() {
return (jsx.object.isMethod(_items, "hasOwnProperty"))
? function(o, p) {
return o.hasOwnProperty(p);
}
: function(o, p) {
return typeof o[p] != "undefined";
};
})()
...
function Map(){}
...
return Map;
})();
Or am I missing something?
Yes, I think so.
2) Don't you think that _hasOwnProperty fallback - typeof o[p] !=
"undefined" - is a bit weak? Shouldn't you (at least) be comparing
property value to the value of the same named property of object's
`constructor.prototype`?
No, that would be the opposite of an equivalent to
Object.prototype.hasOwnProperty(). Please observe
that _hasOwnProperty() is called in _getSafeKey()
in a specific way.
3) I find it convenient to decouple unit tests as much as possible. Your
tests seem to depend on each other more than needed, so changing one
will affect another. Instead, why not create "clean" map in test
runner's "setup" method (I assume JSUnit should have such facility).
Good idea, will do.
PointedEars
.
- Follow-Ups:
- Re: Get list of unique words in a string
- From: Thomas 'PointedEars' Lahn
- Re: Get list of unique words in a string
- References:
- Re: Get list of unique words in a string
- From: kangax
- Re: Get list of unique words in a string
- Prev by Date: Re: Onclick event in nested list
- Next by Date: Re: Onclick event in nested list
- Previous by thread: Re: Get list of unique words in a string
- Next by thread: Re: Get list of unique words in a string
- Index(es):
Relevant Pages
|