[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

General compute language, syntax question

JT

2/29/2016 1:46:00 PM

I have a problem programming uniform networks, "x nodes with y links" that turn out to be really hairy to solve for me and i feel i really lack the programming features

"Actually i program in javascript" but the problem seem general for all programming languages including Pyhton.

For a beautiful solution it would require "If in list/array return boolean" "If not in list/array return boolean".

But there is no such feature Python nor Javascript, so instead i set boolean value "inlist" to false and loop thru, to see if it is already in list. If not it is added to list.

So if the current node i generate links for is x and i try to generate a link to node z, and z's links exhausted i will add it to exhausted list.

And if there is node-x exhausted entries in list, well then script should break because it will lock into a cycle. The cyclic lockup is due to only a subset of the networks on the form (links*deep)+1=nodes is possible.

So what i need is to know howto write "if list/array ***empty*** do {something}"

I sure know howto check if an array have 1 element 2,3,4 but how do you check for empty.

I think it is crazy that you can not do general comparissons of the type
If in list/array
If not in list/array

But it is even more crazy that you can not check if list/array is empty, that is just madness.
6 Answers

JT

2/29/2016 1:59:00 PM

0

Den måndag 29 februari 2016 kl. 14:46:07 UTC+1 skrev jonas.t...@gmail.com:
> I have a problem programming uniform networks, "x nodes with y links" that turn out to be really hairy to solve for me and i feel i really lack the programming features
>
> "Actually i program in javascript" but the problem seem general for all programming languages including Pyhton.
>
> For a beautiful solution it would require "If in list/array return boolean" "If not in list/array return boolean".
>
> But there is no such feature Python nor Javascript, so instead i set boolean value "inlist" to false and loop thru, to see if it is already in list. If not it is added to list.
>
> So if the current node i generate links for is x and i try to generate a link to node z, and z's links exhausted i will add it to exhausted list.
>
> And if there is node-x exhausted entries in list, well then script should break because it will lock into a cycle. The cyclic lockup is due to only a subset of the networks on the form (links*deep)+1=nodes is possible.
>
> So what i need is to know howto write "if list/array ***empty*** do {something}"
>
> I sure know howto check if an array have 1 element 2,3,4 but how do you check for empty.
>
> I think it is crazy that you can not do general comparissons of the type
> If in list/array
> If not in list/array
>
> But it is even more crazy that you can not check if list/array is empty, that is just madness.

I mean for example Javascript tells me to use

if (typeof array[index] !== 'undefined' && array[index] !== null) {

or this one

if (array[index] != null) {

I mean how do they come up with such convoluted syntax, do they pull it out of ass? Well one can always say it is needed because undefined not equal to null.

But would not if (array[index]==empty) suffice and be alot clearer?

JT

2/29/2016 2:26:00 PM

0

Den måndag 29 februari 2016 kl. 14:59:00 UTC+1 skrev jonas.t...@gmail.com:
> Den måndag 29 februari 2016 kl. 14:46:07 UTC+1 skrev jonas.t...@gmail.com:
> > I have a problem programming uniform networks, "x nodes with y links" that turn out to be really hairy to solve for me and i feel i really lack the programming features
> >
> > "Actually i program in javascript" but the problem seem general for all programming languages including Pyhton.
> >
> > For a beautiful solution it would require "If in list/array return boolean" "If not in list/array return boolean".
> >
> > But there is no such feature Python nor Javascript, so instead i set boolean value "inlist" to false and loop thru, to see if it is already in list. If not it is added to list.
> >
> > So if the current node i generate links for is x and i try to generate a link to node z, and z's links exhausted i will add it to exhausted list.
> >
> > And if there is node-x exhausted entries in list, well then script should break because it will lock into a cycle. The cyclic lockup is due to only a subset of the networks on the form (links*deep)+1=nodes is possible.
> >
> > So what i need is to know howto write "if list/array ***empty*** do {something}"
> >
> > I sure know howto check if an array have 1 element 2,3,4 but how do you check for empty.
> >
> > I think it is crazy that you can not do general comparissons of the type
> > If in list/array
> > If not in list/array
> >
> > But it is even more crazy that you can not check if list/array is empty, that is just madness.
>
> I mean for example Javascript tells me to use
>
> if (typeof array[index] !== 'undefined' && array[index] !== null) {
>
> or this one
>
> if (array[index] != null) {
>
> I mean how do they come up with such convoluted syntax, do they pull it out of ass? Well one can always say it is needed because undefined not equal to null.
>
> But would not if (array[index]==empty) suffice and be alot clearer?

I mean how utterly fucking stupid must one be to come up with a solution that require the programmer to check if the array[index] is empty what index? Index for 0 it doesn't exist? Or do they require me to loop thru an empty list how do you do that?

Would it not be alot of clearer to have a boolean empty flag upon the list/array, that is changed when you add elements?

arr=new Array();
if (arr==empty){//sure am here };
arr[x]="something";//
if (arr==empty) {//no way i got here};

Martin Honnen

2/29/2016 2:48:00 PM

0

jonas.thornvall@gmail.com wrote:

> So what i need is to know howto write "if list/array ***empty*** do {something}"

Does

if (array.length == 0) {
//do something
}

not implement what you want with Javascript?

Ben Bacarisse

2/29/2016 3:11:00 PM

0

jonas.thornvall@gmail.com writes:
<snip>
> I mean how utterly fucking stupid must one be to come up with a
> solution that require the programmer to check if the array[index] is
> empty what index? Index for 0 it doesn't exist? Or do they require me
> to loop thru an empty list how do you do that?

It's possible that you are using the wrong data type for the job (an
array is very different from a list, so if you need a list an array
might be a bad fit).

Anyway, if you are using the new ECMAScript you might find that

!array.some(() => true)

does what you want or even (depending on how you handle deleted
elements) the much cheaper array.length == 0. For example, if you use
push/pop/ shift/unshift to add and remove elements you'll find an
empty "list" has length zero.

--
Ben.

Michael Haufe (\"TNO\")

3/3/2016 2:14:00 AM

0

On Monday, February 29, 2016 at 7:46:07 AM UTC-6, jonas.t...@gmail.com wrote:
> I have a problem programming uniform networks, "x nodes with y links" that turn out to be really hairy to solve for me and i feel i really lack the programming features

I'm making this as a general comment since you have a very high noise quotient in your posting. Coupled with the very large number of postings leaves most of us in a position of TLDR with your posts.

Don't take this as an insult, but I very much suggest reading an introductory text or two on Algorithms and to try and implement them directly instead of trying to discover the details through exploratory programming. It will answer many of the questions you tend to raise. Two suggestions:

1. Simple: "Algorithm Design"
<http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651/ref=sr_1_sc_1?s=books&ie=UTF8&qid=1456971127&sr=1-1-spell&keywords=algorithm+design%2C+Goo...

2. Complex: "Introduction to Algorithms"
<http://www.amazon.com/Introduction-Algorithms-3rd-Thomas-Cormen/dp/0262033844/ref=sr_1_1?s=books&ie=UTF8&qid=1456971203&sr=1-1&keywords=introduction+to+algo...

JT

3/3/2016 8:27:00 AM

0

Den torsdag 3 mars 2016 kl. 03:14:20 UTC+1 skrev Michael Haufe (TNO):
> On Monday, February 29, 2016 at 7:46:07 AM UTC-6, jonas.t...@gmail.com wrote:
> > I have a problem programming uniform networks, "x nodes with y links" that turn out to be really hairy to solve for me and i feel i really lack the programming features
>
> I'm making this as a general comment since you have a very high noise quotient in your posting. Coupled with the very large number of postings leaves most of us in a position of TLDR with your posts.
>
> Don't take this as an insult, but I very much suggest reading an introductory text or two on Algorithms and to try and implement them directly instead of trying to discover the details through exploratory programming. It will answer many of the questions you tend to raise. Two suggestions:
>
> 1. Simple: "Algorithm Design"
> <http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651/ref=sr_1_sc_1?s=books&ie=UTF8&qid=1456971127&sr=1-1-spell&keywords=algorithm+design%2C+Goo...
>
> 2. Complex: "Introduction to Algorithms"
> <http://www.amazon.com/Introduction-Algorithms-3rd-Thomas-Cormen/dp/0262033844/ref=sr_1_1?s=books&ie=UTF8&qid=1456971203&sr=1-1&keywords=introduction+to+algo...

http://jt.node365.se/no...