Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › That feeling when you come up with a cool way to code something.
New Posts  All Forums:Forum Nav:

That feeling when you come up with a cool way to code something.

post #1 of 9
Thread Starter 
I wrote this yesterday... I felt smart. (javascript)
Code:
// Cycles trough all the tempWidgets and removes them.
        while (tempWidgets.length)
        {
            tempWidgets.pop().destroy();
        }


... and i know this is much slower than i could have coded it, and probably more confusing. But who cares, it looks sophisticated!
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #2 of 9
It's bling!
post #3 of 9
Code:
var m =(function (o) {
    return function () {
        while (o.length) {
            o.pop();
            m();
        }
    };
}(temp_widget));

m();

biggrin.gifbiggrin.gifbiggrin.gif
Anyway, I agree with you about that feeling. Nothing quite like it.

EDIT: I just thought of another way... CPS
I'm now looking for someone to take up the mantle and expand this in a creative manner.
Code:
var repop = function (z) {
    var i;
    return function (r) {
        for (i = 0; i < z; i += 1) {
            r.push(i);
        }
    };
};
        
var m = (function (o) {
    return function (ret) {
        while (o.length) {
            o.pop();
            m();
        }
        if (!o.length && typeof ret === 'function') {
            ret(o);
        } else {
            return;
        }
    };
}(temp_widget));

m(repop(5));

//to explain:
//repop generates anon function and closure where z=5. This is passed to m as the return.
//m pops each element by recursively calling itself
//the while loop is there to make things appear complicated. In reality, it will never loop
//an 'else' clause is needed and a second loop would be gauche, so an if is used
//when the length reaches zero, the recursions with no value for ret will simply return until
//the outermost recursion is reached. At this point, our anonymous
//will repopulate the list with five numbers from 0-4.

Edited by hajile - 9/3/13 at 1:28pm
post #4 of 9
Thread Starter 
easy now... the goal wasn't to obfuscate... that's a WHOLE new world of stuff.

I should have done this. It would reduce the amount of checks i need to do on a global variable property. Yay for GoogleTechTalks showing me how to do loops right... Not sure if thats the best way to break down an array though.
Code:
var len = tempWidgets.length;
while(len--)
{
   tempWidgets.pop().destroy();
}
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #5 of 9
Quote:
Originally Posted by Mrzev View Post

easy now... the goal wasn't to obfuscate... that's a WHOLE new world of stuff.

I should have done this. It would reduce the amount of checks i need to do on a global variable property. Yay for GoogleTechTalks showing me how to do loops right... Not sure if thats the best way to break down an array though.
Code:
var len = tempWidgets.length;
while(len--)
{
   tempWidgets.pop().destroy();
}

That's not actually obfuscated (except for using a while in place of an if/else block). It's all widely used techniques (though there's much more efficient methods for this particular goal).

When you want to get rid of an array, there is seldom a reason to not set the array to null
Code:
var x = [1,2,3,4];
x = null;

//garbage collectors collect all objects that don't have accessible references
//The only variable pointing to that array object was x. Now that x is null,
//there are no references left and the garbage collector will clean it up next cycle
post #6 of 9
Thread Starter 
I need to run the destroy commands on the elements. Basically I was running into issues with Dojo not cleaning up its widgets, so whenever i created one, i pushed it to my array, and every time I needed to make sure everything is clear, i ran this function which deleted the nodes.

I guess my question is, i dont know how efficient .pop actually is. Would it be better to go array[x].destroy in a loop then array= null at the end, or array.pop.destroy in a loop.

... array.pop.destory... chome thinks i mean supererogatory ... bit of a stretch. Interesting but array.pop.destroy is perfectly fine. I guess if each word is fine, then its good, otherwise the '.' was probably a typo.... i got distracted..
Edited by Mrzev - 8/30/13 at 4:32pm
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #7 of 9
Quote:
Originally Posted by Mrzev View Post

I wrote this yesterday... I felt smart. (javascript)
Code:
// Cycles trough all the tempWidgets and removes them.
        while (tempWidgets.length)
        {
            tempWidgets.pop().destroy();
        }


... and i know this is much slower than i could have coded it, and probably more confusing. But who cares, it looks sophisticated!

I just realized that I must be a total nerd. You know how I realized that I'm a nerd? Because when I looked at that code I actually lol'd out loud.

Edit: yeah, nothing better than finding a nice clean way to do something that is actually very complicated, in as few lines a possible.
Edited by wedge - 8/30/13 at 4:41pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #8 of 9
Quote:
Originally Posted by Mrzev View Post

I wrote this yesterday... I felt smart. (javascript)
Code:
// Cycles trough all the tempWidgets and removes them.
        while (tempWidgets.length)
        {
            tempWidgets.pop().destroy();
        }


... and i know this is much slower than i could have coded it, and probably more confusing. But who cares, it looks sophisticated!

I wouldn't say that was particularly sophisticated to be honest. In fact it's probably the simplest way you could code that. Not that there's anything wrong with the code - sometimes the simplest solutions are the best. But what I'm saying is I'd expect you to have plenty more eureka moments over coming months. smile.gif
post #9 of 9
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

I wouldn't say that was particularly sophisticated to be honest. In fact it's probably the simplest way you could code that. Not that there's anything wrong with the code - sometimes the simplest solutions are the best. But what I'm saying is I'd expect you to have plenty more eureka moments over coming months. smile.gif
Yeah, being new to the whole world of Javascript, my mind was blown recently upon learning about AMD and their whole object oriented movement.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › That feeling when you come up with a cool way to code something.