Vector data type (2nd attempt)

V

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.$_$);
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>
 
R

Richard Cornford

VK said:
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.
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.
 
W

web.dev

VK said:

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.
 
M

Matt Kruse

VK said:
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?
 
R

Ray

VK said:
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.)
 
R

Randy Webb

Ray said the following on 9/14/2006 12:56 AM:
<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.
 
R

Richard Cornford

Ray said:
<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.
 
V

VK

Matt said:
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.
 
V

VK

Ray said:
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);
}

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).
 
R

Richard Cornford

VK said:
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?
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.
 
R

Ray

VK said:
var arr = [0,1,2,3];

delete arr[1];

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

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?
 
V

VK

Seeing as how a Vector is synchronized, and javascript is not

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?
 
R

Richard Cornford

VK said:
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?

(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.
 
V

VK

Ray said:
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.
 
V

VK

John said:
<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.
 
J

John G Harris

VK said:
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
 
R

Ray

VK said:
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.
 
V

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top