By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

Vector data type (2nd attempt)

P: n/a
VK
A while ago I wrote a "Vector data type" script
using DOM interface to select.options.
That was a (beautiful) mind game :-) rather than
a practical thing to use.

Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<html>
<head>
<title>Vector constructor</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function Vector(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = Vector.$add;
this.remove = Vector.$remove;
this.toString = Vector.$toString;
}

Vector.$add = function(m,i) {
if (('number'!=typeof(i))||(i<0)||(i>=this.$_$.length )) {
this.$_$.push(m);
}
else {
var tmp = [m];
tmp.push(this.$_$[i]);
this.$_$.splice(i,1,tmp);
}
this.length = this.$_$.length;
}

Vector.$remove = function(i) {
var ret = this.$_$.splice(i,1)[0];
this.length = this.$_$.length;
return ret;
}

Vector.$toString = function() {
return this.$_$.toString();
}

// Create new vector and use it as a wrapper
// over Array argument:
var v = new Vector([1,2,3]);

// add(newElement, atPosition) method
// if no atPosition provided, newElement will
// be added to the top
v.add(4); // add 4 to the top

// Add 3.5 atPosition 3
// The element currently located at v[3] and all elements atop
// of it will be moved one position up
v.add(3.5, 3);

// remove(atPosition) method
// Elements currently located atop of it will be moved
// one position down
v.remove(1);

// toString method is overloaded so in string
// context it gives comma-separated vector values
alert(v); //1, 3, 3.5, 4
</script>
</head>

<body>

</body>
</html>

Sep 13 '06 #1
Share this Question
Share on Google+
28 Replies


P: n/a
VK wrote:
A while ago I wrote a "Vector data type" script
using DOM interface to select.options.
That would be the one that eared the accolade: "It uses the most bizarre
and obtuse storage method I've ever witnessed, and it uses it
inconsistently and without any obvious benefits.".
That was a (beautiful) mind game :-)
That would be very much in the eye of the beholder.
rather than a practical thing to use.
No arguments there.
Here is another attempt open for criticism,
It is a trivial script to write and you failed anyway.
this time dead serious.
God help the people who pay you to write scripts for them.
I really need an effective
Vector emulator for a project
Given that what you have implemented (if it worked) would be no more
than a wrapper around an array "really need" seems excessive.
>(as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
When you write "productivity" you mean performance (or at lest everyone
else would, what you may mean is another matter). It may be an idea to
write something that works properly first and not let yourself be fooled
into thinking you have achieved something by superficial appearances.
>
<html>
<snip>
alert(v); //1, 3, 3.5, 4
</script>
<snip>

Feeling proud of yourself? Try inserting:-

Array.prototype.toString = function(){
return '['+this.join(', ')+']';
};

- at the beginning of the page as see if you can work out how you f****d
up this time.

Though having demonstrated that you cannot write something as simple as
this and get it right first time I think you should give up all pretence
of being anything like a competent programmer.

Richard.
Sep 13 '06 #2

P: n/a

VK wrote:
<script>
Why aren't you using the type attribute? For as long as you have been
here in the group, you would know most would say something about this.
However, let's not talk about validity since it has been discussed so
many times.
function Vector(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = Vector.$add;
this.remove = Vector.$remove;
this.toString = Vector.$toString;
}
I don't understand your use for the '$' symbols to be used as
identifiers. Just from quick skimming of your project makes it look
like a potential maintenance nightmare.
Vector.$add = function(m,i) {
[snip]

Unless it's just for your own personal educational project, I couldn't
really recommend it to anyone as it doesn't really do anything new that
I can do with just Array itself. It seems you are adding an
unnecessary layer (wrapper) to make things more complicated.

Sep 13 '06 #3

P: n/a
VK wrote:
I really need an effective
Vector emulator for a project
I assume you are trying to emulate Java's Vector.
Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?
(hint: you don't)

All you really want is an interface to the Array object that you find more
familiar. This wasn't obvious to you, seeing as how you're just messing
around with an internally-held Array?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 13 '06 #4

P: n/a
Ray
VK wrote:
Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<snip>

As a long time Java programmer and newbie in JavaScript--I wonder--what
kind of value does your Vector add on top of JavaScript's existing
Array? Vector was created in Java because Java's Array is nowhere as
flexible as JavaScript's. Not so with JS's Array.

The methods exposed by your Vector class don't add value to what one
does with JS Array--if you think I'm wrong, I'll be glad to be
corrected.

(and what is with the $add and stuff? They're weird.)

Sep 14 '06 #5

P: n/a
Ray said the following on 9/14/2006 12:56 AM:
VK wrote:
>Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<snip>

As a long time Java programmer and newbie in JavaScript--I wonder--what
kind of value does your Vector add on top of JavaScript's existing
Array?
None.

Vector was created in Java because Java's Array is nowhere as
flexible as JavaScript's. Not so with JS's Array.

The methods exposed by your Vector class don't add value to what one
does with JS Array--if you think I'm wrong, I'll be glad to be
corrected.
You aren't wrong.
(and what is with the $add and stuff? They're weird.)
Figments of VK's imagination where he thinks that adds something to his
convoluted code.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 14 '06 #6

P: n/a
Ray wrote:
VK wrote:
Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<snip>

As a long time Java programmer and newbie in JavaScript--I wonder--what
kind of value does your Vector add on top of JavaScript's existing
Array? Vector was created in Java because Java's Array is nowhere as
flexible as JavaScript's. Not so with JS's Array.

The methods exposed by your Vector class don't add value to what one
does with JS Array--if you think I'm wrong, I'll be glad to be
corrected.

(and what is with the $add and stuff? They're weird.)
Consider that when he posted the code here VK could be guaranteed to be
subject to criticism for any technical faults it may have. Most people,
under similar circumstances, would post the very best code they could
write and seriously verify that it worked properly. That would then
only leave questions of style, efficiency, clarity etc. But what VK
posted doesn't actually work at all, and his testing did not expose
that (which doesn't stop it from being the best he is capable of). The
reason for that is simply that VK does not understand the code he
writes himself and so cannot tell what it is supposed to do or identify
what it actually is dong.

The result of this is that there is no point in asking for him to
explain or justify anything he does because when he doesn't know what
he is doing he cannot know why he is doing it.

However, VK has demonstrated a tendency to gravitate towards the worst
available approach to everything he does (some sort of unconscious
imperative), which is what you observer here.

Richard.

Sep 14 '06 #7

P: n/a
VK

Matt Kruse wrote:
VK wrote:
I really need an effective
Vector emulator for a project

I assume you are trying to emulate Java's Vector.
Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?
That is the perception of the reality you've been taught a couple of
years ago while starting to learn JavaScript out of c.l.j. ("whatever
is not documented is not necessary and most probably wrong") As I felt
myself not in full power to comment on your educational process, I have
only to admit that it is fully whithin the expected frame of a clj'ed
person (with a nuked demand to do anything above the allowed borders).
>why would you event want to emulate a Vector?
Because outside of the no-threads JavaScript environment there is a
whole new world they thaugt you to be not existing.
Or, as practical as it is: I need for my project an array to add/remove
some references while having an exact number of involved objects and
while avoiding any "gaps" in the continuum: so Vector seems as the best
solution - but possibly not.

Sep 14 '06 #8

P: n/a
VK

Ray wrote:
As a long time Java programmer and newbie in JavaScript--I wonder--what
kind of value does your Vector add on top of JavaScript's existing
Array?
var arr = [0,1,2,3];

delete arr[1];

for (var i=0; i<arr.length; ++i) {
alert(arr[i]);
}

So what kind of functionality is missng? An ability to add/remove w/o
overriding or creating gaps in the involved array. I thought it was it
was pretty obvious, but it could be a mistake (the simplicity).

Sep 14 '06 #9

P: n/a
VK wrote:
Matt Kruse wrote:
VK wrote:
I really need an effective
Vector emulator for a project
I assume you are trying to emulate Java's Vector.
Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?

That is the perception of the reality you've been taught a couple of
years ago while starting to learn JavaScript out of c.l.j. ("whatever
is not documented is not necessary and most probably wrong")
Halfwit.
As I felt myself not in full power to comment on your educational
process, I have only to admit that it is fully whithin the expected
frame of a clj'ed person (with a nuked demand to do anything
above the allowed borders).
Are you ' doing something above the allowed borders'? Is that what you
call writing code that doesn't actually work while not being aware that
it doesn't work?

All claims form you of 'I will ignore all advice because what I do is
practical' sounds pretty hollow posted below a demonstration that your
best effort doesn't produce code that works, and you cannot see that it
doesn't work.

Do you think Matt, or any that point out your faults on such a regular
basis, would have failed so completely at such a trivial task?
>>why would you event want to emulate a Vector?
Because outside of the no-threads JavaScript environment there
is a whole new world they thaugt you to be not existing.
This would be the fantasy world of VKScript then, where 'real
programmers' cannot even tell when the code they write does not do
anything close to what it is intended to do?
Or, as practical as it is:
LOL
I need for my project an array to add/remove some references
while having an exact number of involved objects and while
avoiding any "gaps" in the continuum: so Vector seems as
the best solution - but possibly not.
Oh dear, you need something that you cannot write yourself. Perhaps you
had better go out and hire a programmer.

Richard.

Sep 14 '06 #10

P: n/a
Ray
VK wrote:
var arr = [0,1,2,3];

delete arr[1];

for (var i=0; i<arr.length; ++i) {
alert(arr[i]);
}

So what kind of functionality is missng? An ability to add/remove w/o
overriding or creating gaps in the involved array. I thought it was it
was pretty obvious, but it could be a mistake (the simplicity).
But both cases are JavaScript one-liners:

arr.splice(1, 1);

or if you want to insert an element at index 1, and push everything
from 1 onwards back:

arr.splice(1, 0, 3.5);

so I don't understand what's the big deal?

Sep 14 '06 #11

P: n/a
VK
Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?
That is the perception of the reality you've been taught a couple of
years ago while starting to learn JavaScript out of c.l.j. ("whatever
is not documented is not necessary and most probably wrong")

Halfwit.
Full dumb...

So what is your point? That Vector data type (emulation) may be never
needed at no circumstances?
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/eea91f958a345189>
(besides my real case) is a disproof.

I do agree though that Java programmers are the most negligeable
persons around to talk about arrays because in the native Java
environment it is just an occasional tool to send/receive multiple
arguments in methods' calls. So they just have to listen and learn ;-)

Can be the algorithm more effective to add/remove elements w/o gaps and
overriding: that is the question. Any comments on it?

Sep 14 '06 #12

P: n/a
VK wrote:
>>>Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?

That is the perception of the reality you've been taught a couple of
years ago while starting to learn JavaScript out of c.l.j. ("whatever
is not documented is not necessary and most probably wrong")

Halfwit.

Full dumb...

So what is your point? That Vector data type (emulation) may be never
needed at no circumstances?
Does the material that I quoted to provide a context for my response
say anything about Vectors, or is it a rather hollow attempt to deride
Matt for learning something about programming?

<snip>
(besides my real case) is a disproof.
Disproof of what? That you are a halfwit? Posting code here that does
not work was about as much proof of that as anyone would need.
I do agree though that Java programmers are the most negligeable
persons around to talk about arrays because in the native Java
environment it is just an occasional tool to send/receive multiple
arguments in methods' calls. So they just have to listen and learn ;-)
Agree with who? That gibberish sounds more like the product of your
mind than something anyone sane would say. (hearing voices in your
head?)
Can be the algorithm more effective to add/remove elements w/o gaps and
overriding: that is the question. Any comments on it?
Which algorithm? The one you implemented that doesn't actually work at
all?

Richard.

Sep 14 '06 #13

P: n/a
VK

Ray wrote:
or if you want to insert an element at index 1, and push everything
from 1 onwards back:

arr.splice(1, 0, 3.5);

so I don't understand what's the big deal?
The deal (besides that it is needed to be a constructor with a bounch
of other methods) is that you are a genius: I simply did not think/try
a call with 0 elements to remove.

Thanks.

Sep 14 '06 #14

P: n/a
VK
Matt Kruse wrote:
I assume you are trying to emulate Java's Vector.
Seeing as how a Vector is synchronized, and javascript is not
multi-threaded, why would you event want to emulate a Vector?

That is the perception of the reality you've been taught ... <snip>
I was glad to hear the January news at <http://www.mattkruse.com>
*No* obligations of any kind neither from my side nor from yours. Just
to not mix the virtual reality with the real one...

Sep 14 '06 #15

P: n/a
In article <11**********************@e3g2000cwe.googlegroups. com>, VK
<sc**********@yahoo.comwrites

<snip>
this.add = Vector.$add;
this.remove = Vector.$remove;
<snip>

Looks more like a List than a Vector.

John
--
John Harris
Sep 14 '06 #16

P: n/a
VK

John G Harris wrote:
In article <11**********************@e3g2000cwe.googlegroups. com>, VK
<sc**********@yahoo.comwrites

<snip>
this.add = Vector.$add;
this.remove = Vector.$remove;
<snip>

Looks more like a List than a Vector.
For me it looks more like a Vector than a List :-)

<http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html>

If we take the main aim of the emulation, that's going to be the
elastic "ribbon behavior" with elements on the the top either going up
or down upon insertion/deletion of an element.

In this aspect Vector is the base object providing such functionality
with derivates out of it including List.

Of course, as anything emulated in one enviroment based on an entity
from totally another environment, that can be a subject of a term
definition.

Sep 14 '06 #17

P: n/a
In article <11**********************@i3g2000cwc.googlegroups. com>, VK
<sc**********@yahoo.comwrites
>
John G Harris wrote:
>In article <11**********************@e3g2000cwe.googlegroups. com>, VK
<sc**********@yahoo.comwrites

<snip>
this.add = Vector.$add;
this.remove = Vector.$remove;
<snip>

Looks more like a List than a Vector.

For me it looks more like a Vector than a List :-)

<http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html>

If we take the main aim of the emulation, that's going to be the
elastic "ribbon behavior" with elements on the the top either going up
or down upon insertion/deletion of an element.

In this aspect Vector is the base object providing such functionality
with derivates out of it including List.

Of course, as anything emulated in one enviroment based on an entity
from totally another environment, that can be a subject of a term
definition.
You're reading the wrong page of the JavaDoc manual. Here's an extract
from the right page :
Interface List

An ordered collection (also known as a sequence). The user of this
interface has precise control over where in the list each element is
inserted. The user can access elements by their integer index (position
in the list), and search for elements in the list.

All Superinterfaces: CollectionAll

Known Implementing Classes: AbstractList, LinkedList, Vector, ArrayList
You are trying to implement the List interface. In Java, Vector is the
(badly named) implementation of List that supports multi-threading.
You'll have noticed that all the other classes have 'List' in their
names.

John
--
John Harris
Sep 14 '06 #18

P: n/a
Ray

VK wrote:
For me it looks more like a Vector than a List :-)
But a Vector is, for all practical purposes, a (synchronized) List.
These days Java programmers don't use Vector anymore, it's a remnant of
the old Java days.
<http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html>

If we take the main aim of the emulation, that's going to be the
elastic "ribbon behavior" with elements on the the top either going up
or down upon insertion/deletion of an element.
Yeah, that's what List is about. Vector is just but one implementation
of List.
In this aspect Vector is the base object providing such functionality
with derivates out of it including List.
Sep 15 '06 #19

P: n/a
VK
List data type v.2

1) Correct term use (List not Vector): Ray, Harris
2) splice() to insert an element optimization: Ray
<html>
<head>
<title>List constructor</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function List(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = List.$add;
this.remove = List.$remove;
this.toString = List.$toString;
}
List.$add = function(m,i) {
if (('number' != typeof i)||(i >= this.$_$.length)) {
this.$_$.push(m);
}
else if ((i<0)||(parseInt(i,10)!=i)){
throw new Error('Invalid index');
// as a "problem hiding" option can be implied:
// i = Math.abs(parseInt(i,10));
}
else {
this.$_$.splice(i,0,m);
}
this.length = this.$_$.length;
}
List.$remove = function(i) {
if (('number' != typeof i)||(i >= this.$_$.length)) {
return null;
}
else if (parseInt(i,10) != i){
throw new Error('Invalid index');
// as a "problem hiding" option can be implied:
// i = (parseInt(i,10);
}
else {
if (i<0) {i-= this.$_$.length;}
var ret = this.$_$.splice(i,1)[0];
this.length = this.$_$.length;
return ret;
}
}
List.$toString = function() {
return this.$_$.toString();
}
// Create new vector and use it as a wrapper
// over Array argument:
var myList = new List(['Apples', 'Oranges', 'Bananas']);

// add(newElement, atPosition) method
// if no atPosition provided, newElement will
// be added to the top
myList.add('Mangos'); // add 'Mangos' to the top

// Add 'Strawberries' atPosition 3
// The element currently located at myList[3] and all elements
// atop of it will be moved one position up
myList.add('Strawberries', 3);

// remove(atPosition) method
// Elements currently located atop of it
// will be moved one position down
myList.remove(1); // remove 'Oranges'

// length property shows the actual length
window.alert(myList.length); // 4

// toString method is overloaded so in string
// context it gives comma-separated values
window.alert(myList); // Apples,Bananas,Strawberries,Mangos
</script>
</head>

<body>

</body>
</html>

P.S. what do $add, $remove and so names (starting with $)? Just a
marker of identifiers used only internally (that would be "private"
modifier if existed). Some are using underscores instead (_add,
_remove), but it is really an in-company choice, could be even prvtAdd,
prvtRemove. That is to make the track of vars easier and - if needed -
to have one click conversion to JScript.NET

Sep 15 '06 #20

P: n/a
In article <11**********************@i3g2000cwc.googlegroups. com>, VK
<sc**********@yahoo.comwrites
>List data type v.2

1) Correct term use (List not Vector): Ray, Harris
2) splice() to insert an element optimization: Ray
<html>
<head>
<title>List constructor</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function List(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = List.$add;
this.remove = List.$remove;
this.toString = List.$toString;
}
<snip>

Why don't you give List objects a prototype ?

>P.S. what do $add, $remove and so names (starting with $)? Just a
marker of identifiers used only internally (that would be "private"
modifier if existed). Some are using underscores instead (_add,
_remove), but it is really an in-company choice, could be even prvtAdd,
prvtRemove. That is to make the track of vars easier and - if needed -
to have one click conversion to JScript.NET
length needs to be Read Only a lot more than the internal array needs to
be private. If someone changes a List object's length value then you're
scuppered. A length() method accessing the object's array would be a lot
safer.

John
--
John Harris
Sep 15 '06 #21

P: n/a
VK wrote:
List data type v.2
1) Correct term use (List not Vector): Ray, Harris
Do you understand Java? List is an interface. You cannot create a "List"
object.
Further, you only implemented two of its methods, and one of them has a
different argument order. Why even try to make your js look like Java?
2) splice() to insert an element optimization: Ray
How about:
3) Just enhance Array and call it List then: Matt

The following code is much simpler:

----------
Array.prototype.add = function(element,index) {
if (('number'!=typeof index)||(index>=this.length)) {
this.push(element);
}
else {
this.splice(index,0,element);
}
}
Array.prototype.remove = function(index) {
if (('number' != typeof index)||(index >= this.length)) {
return null;
}
else {
if (index<0) {index -= this.length;}
return this.splice(index,1)[0];
}
}
var List = Array;
----------

Now, what value does it add? None, really. Under the hood, you're still just
using Array methods. So why not just call it an Array? You're not adding any
value by creating this "List" object.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 16 '06 #22

P: n/a
VK
John G Harris wrote:
Why don't you give List objects a prototype ?
That is another fully viable option, see the reply by Matt Kruze
length needs to be Read Only a lot more than the internal array needs to
be private. If someone changes a List object's length value then you're
scuppered. A length() method accessing the object's array would be a lot
safer.
Yeh, and have typos all around with length called as property (w/o
parenthesis) :-(
I tried it already in another object. That is not even a programming
issue but an ergonomical one (damn human factor). In this aspect it
seems better then to use VB approach and to have a stay-alone function
for length:
len(myList); // length(myList); ?
That is another thing I'm missing terribly in ECMAScript: an ability to
have compound properties (objects with getter/setter) and respectively
read-only / write-only options for them, not just fields as it is now.
That is a breeze with HTC/XBL. In the regular inline script I once
twisted my mind around of thinking a way to call a function as a
property, but no way. The only trick is available in string context by
overloading toString method.

Sep 16 '06 #23

P: n/a
VK
Matt Kruse wrote:
List is an interface.
You cannot create a "List" object.
Think of "myArray extends Array implements List", that helps ;-)

How about:
Just enhance Array and call it List then
Another fully viable option.

----------
Array.prototype.add = function(element,index) {
if (('number'!=typeof index)||(index>=this.length)) {
this.push(element);
}
else {
this.splice(index,0,element);
}
}

Array.prototype.remove = function(index) {
if (('number' != typeof index)||(index >= this.length)) {
return null;
}
else {
if (index<0) {index -= this.length;}
return this.splice(index,1)[0];
}
}

var List = Array;
----------
Now, what value does it add? None, really. Under the hood, you're still just
using Array methods. So why not just call it an Array? You're not adding any
value by creating this "List" object.
That is a question of the kind "Why do you need to create class Manager
if it has only one property more than Employee? Simply add that
property to an Employee instance."

Or more generally: "Why do you need OOP hassle? Your custom objects do
not do anything what their supers couldn't, so just augment supers".
:-)

Having the prototype way as totally correct option, I need (think I
need, want) a separate object type, not just Array with extra methods.
Array is hadled as a fixed continuum defined by 0 - (length-1) borders.
We can extend this continuum or make "holes" ("gaps") in this continuum
by delete'ing elements.
List (interface) is flexible continuum able to automatically expand and
collapse ("healing the holes") on elements adding/removal.
That seems as enough of behavioral difference for a will to have two
different objects from different constuctors: rather than all existing
and future Array's acting this way or that way depending on what
methods do you apply to them.

Once again: prototype augmentation is definitely and always an option.

Sep 16 '06 #24

P: n/a
VK

VK wrote:
... see the reply by Matt Kruze ...
How many times I did this damn typo...

Matt *Kruse*

Sep 16 '06 #25

P: n/a
VK wrote:
<snip>
function List(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = List.$add;
this.remove = List.$remove;
this.toString = List.$toString;
}
List.$add = function(m,i) {
<snip>
}
List.$remove = function(i) {
<snip>
}
<snip>
List.$toString = function() {
return this.$_$.toString();
}
<snip>
P.S. what do $add, $remove and so names (starting
with $)? Just a marker of identifiers used only
internally (that would be "private" modifier if
existed).
<snip>

It would still be more efficient to be creating the methods as
properties of the object's prototype and so having them inherited by the
objects constructed (instead of explicitly assigning them) and then
there is no reason for using (poorly chosen) naming conventions to
discourage people form using them in the context where they are exposed.

Richard.
Sep 16 '06 #26

P: n/a

Ray wrote:
VK wrote:
Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<snip>

As a long time Java programmer and newbie in JavaScript--I wonder--what
kind of value does your Vector add on top of JavaScript's existing
Array? Vector was created in Java because Java's Array is nowhere as
flexible as JavaScript's. Not so with JS's Array.

The methods exposed by your Vector class don't add value to what one
does with JS Array--if you think I'm wrong, I'll be glad to be
corrected.

(and what is with the $add and stuff? They're weird.)
Which leads to ask why Vector and not java.util.List ? Nobody
programming java since version 1.2 should be using Vector. It was
implemented for the reason you pointed out and used becasue there was
nothing better at the time. Promoting the use of a (admitted by Sun)
broken API just doesn't seem like a good idea. As a java programmer he
should know that best practices recommend programming toward interfaces
and not the concrete classes, especially so when it comes the the
Collections API.
If a synchronized list is desired, then the static method
"synchronizedList(List)" should be called on the java.util.Collections
class.

OTOH, it looks like all he is really after is the ability to add an
item at a particualr index and shift the other items, and remove a
particular item and have everything shift back. Which is really much
less than the methods defined in Vector. Seems like the KISS priciple
would dictate just adding these methods to objects of type Array using
the evil prototype.

Sep 18 '06 #27

P: n/a

VK wrote:
John G Harris wrote:
In article <11**********************@e3g2000cwe.googlegroups. com>, VK
<sc**********@yahoo.comwrites

<snip>
this.add = Vector.$add;
this.remove = Vector.$remove;
<snip>

Looks more like a List than a Vector.

For me it looks more like a Vector than a List :-)

<http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html>
Try re-reading the Javadocs.
Vector implements the interface List. List defines the methods. An
interface can not be a derivative of a class.
>From the Javadoc for List pertinent to what you are trying to do:
void add(int index, Object element)
Object remove(int index)

Take a look at the documentation of the Collections framework:
http://java.sun.com/j2se/1.4.2/docs/.../overview.html
>From my previous post, Vectors as a rule should be avoided (unless you
are targeting a JVM1.1 or older).

Sep 18 '06 #28

P: n/a

VK wrote:
Matt Kruse wrote:
List is an interface.
You cannot create a "List" object.

Think of "myArray extends Array implements List", that helps ;-)

How about:
Just enhance Array and call it List then

Another fully viable option.

----------
Array.prototype.add = function(element,index) {
if (('number'!=typeof index)||(index>=this.length)) {
this.push(element);
}
else {
this.splice(index,0,element);
}
}

Array.prototype.remove = function(index) {
if (('number' != typeof index)||(index >= this.length)) {
return null;
}
else {
if (index<0) {index -= this.length;}
return this.splice(index,1)[0];
}
}

var List = Array;
----------
Now, what value does it add? None, really. Under the hood, you're still just
using Array methods. So why not just call it an Array? You're not adding any
value by creating this "List" object.

That is a question of the kind "Why do you need to create class Manager
if it has only one property more than Employee? Simply add that
property to an Employee instance."

Or more generally: "Why do you need OOP hassle? Your custom objects do
not do anything what their supers couldn't, so just augment supers".
:-)

Having the prototype way as totally correct option, I need (think I
need, want) a separate object type, not just Array with extra methods.
Array is hadled as a fixed continuum defined by 0 - (length-1) borders.
We can extend this continuum or make "holes" ("gaps") in this continuum
by delete'ing elements.
List (interface) is flexible continuum able to automatically expand and
collapse ("healing the holes") on elements adding/removal.
That seems as enough of behavioral difference for a will to have two
different objects from different constuctors: rather than all existing
and future Array's acting this way or that way depending on what
methods do you apply to them.

Once again: prototype augmentation is definitely and always an option.
Since this looks like its getting out of control and Matt was the only
one kind enough to post code, I'm going to offer up the following as
well.
In keeping with the java.util.List inteface, I'm going to give the
size() method instead of length. I am also going to internalize the
methods of the List object. The constructor in this method will copy
the supplied array to prevent its modification (as in VK's posted
code).
I am also adding a few xUnit type tests to validate functionality and
to document usage.
I Hope it helps to clarify what others have been trying to explain.

<html>
<head>
<title>List in JS</title>
</head>

<script type="text/javascript">

function List(srcAry){
this._internAry = new Array();
if(srcAry){
/* Copy supplied array to prevent side effects */
this._internAry = new Array(srcAry.length);
for(var i=0;i<srcAry.length;i++){
this._internAry[i] = srcAry[i];
}
}
this.add = function(obj,indx){
this._internAry.splice(indx,1,obj)
};
this.remove = function(indx){
return this._internAry.splice(indx,1);
};
this.toString = function(){
return this._internAry.toString();
}
this.size = function(){
return this._internAry.length;
}
}
</script>

<script type="text/javascript">
function assertEquals(msg,val1,val2){
if(val1 != val2){
alert(msg + "\n expected: "+val1+"\nreceived: "+val2);
arguments.callee.failed = +arguments.callee.failed || 0;
arguments.callee.failed++;
}
}
// Tests
var srcAry = new Array('Apples','Oranges','Bananas');
assertEquals("Should be 3 fruit",3,srcAry.length);

var myList = new List(srcAry);
assertEquals("toString test
failed","Apples,Oranges,Bananas",myList.toString() );
assertEquals("incorrect size on construction",3,myList.size());

myList.add('Strawberries', 3);
assertEquals("Size failed after adding",4,myList.size());
assertEquals("toString test
failed","Apples,Oranges,Bananas,Strawberries",myLi st.toString());
assertEquals("Source array modified (bad juju)",3,srcAry.length);

var fruit1 = myList.remove(1);
assertEquals("wrong fruit","Oranges",fruit1);
assertEquals("resize failed after remove",3,myList.size());
assertEquals("toString test
failed","Apples,Bananas,Strawberries",myList.toStr ing());
assertEquals("Source array modified (bad juju)",3,srcAry.length);

var fruit2 = myList.remove(1);
assertEquals("wrong fruit","Bananas",fruit2);
assertEquals("resize failed after remove",2,myList.size());
assertEquals("toString test
failed","Apples,Strawberries",myList.toString());
assertEquals("Source array modified (bad juju)",3,srcAry.length);

if(assertEquals.failed){
alert("tests failed: " + assertEquals.failed);
}else{
alert("All tests passed");
}
</script>
<body>

</body>
</html>

Sep 18 '06 #29

This discussion thread is closed

Replies have been disabled for this discussion.