Programming Thread

General off-topic stuffs goes here.
User avatar
Ashan
Posts: 503
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Programming Thread

Postby Ashan » December 5th, 2015, 1:50 am

We had one of these on the old haus of talk
Figured we could remake it here!

For those of you into programming or general computer-ey, or hack-ey stuff, share your stuff here! Projects you're working on, upcoming stuff that excites you, etc.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 5th, 2015, 7:18 am

Good idea. I was actually thinking of making one myself over the last few weeks but never really got around to it because I wasn't sure exactly how active it'd be. There's only four of us that I know of who might potentially use it but only two of us are very active posters and at least one of the others (chridd) doesn't seem to post about programming here unless it's pointing out errors in stuff I posted.
💙💙💙
Image
Image

User avatar
Absolut Zero
Site Developer
Posts: 610
Joined: December 24th, 2014, 3:51 am

Re: Programming Thread

Postby Absolut Zero » December 5th, 2015, 7:39 am

I support this thread as well, I was going to make a thread similar to this a long long time ago but me and Alice usually do our programming talks in the mod forum, and it didnt seem like there were many users here interested in that kind of stuff but hopefully this thread will prove me wrong.

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 5th, 2015, 8:00 am

Since we have this going I might as well cross post that image I posted in the admin forum yesterday or the day before and see if anyone else has alternate solutions for it.

Alice wrote:Image
I'm kinda curious just how many ways there are to do this. I'm pretty sure that's c# and I forget if c# has a built in floor function but I came up with two off the top of my head. What would likely be the proper way:

Code: Select all

if (userNumber % 2 == 0) {
    console.WriteLine("Your number is even!");
} else {
    console.WriteLine("Your number is odd!");
}

And the alternate version I came up with:

Code: Select all

if ((userNumber / 2) - floor(userNumber) == 0) {
    console.WriteLine("Your number is even!");
} else {
    console.WriteLine("Your number is odd!");
}

If c# doesn't have it then this should work fine as a simple floor function:

Code: Select all

function floor(var) {
    return var - (var % 1);
}

This should return only the bits after the decimal and then subtract that from the variable you input. (Also that may not be the proper structure for a function in c#. It's right for javascript but I haven't messed with c# in months.)
💙💙💙
Image
Image


User avatar
chridd
Posts: 210
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » December 5th, 2015, 12:45 pm

Alice wrote:Good idea. I was actually thinking of making one myself over the last few weeks but never really got around to it because I wasn't sure exactly how active it'd be. There's only four of us that I know of who might potentially use it but only two of us are very active posters and at least one of the others (chridd) doesn't seem to post about programming here unless it's pointing out errors in stuff I posted.
That's because I mostly respond to other people rather than bringing up topics myself and there haven't been that many other programming things to respond to (that I had a response to). I hope you don't mind.

Also my signature links to my website which has various things I've made, some of which are programs.

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 5th, 2015, 12:49 pm

chridd wrote:That's because I mostly respond to other people rather than bringing up topics myself and there haven't been that many other programming things to respond to (that I had a response to). I hope you don't mind.

Nah, I don't mind at all. You've been pretty helpful actually.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 503
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » December 5th, 2015, 5:41 pm

None of the stuff I'll be showing off will be near as impressive as stuff you guys do cause I'm still a relative noob, but I've kinda been playing around and figuring out how bash works lately, and how to customize it.
https://github.com/Ashanmaril/dotfiles

Basically all I have right now is color profiles, aliases, and custom prompts. I actually found a couple tools people made for making custom prompts, which is neat. I also set tinkered with my path so I can launch emacs and Sublime Text from bash.
And to top it all off I found a font that makes the terminal look like a VT220.
► Show Spoiler
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 5th, 2015, 6:07 pm

Ashan wrote:None of the stuff I'll be showing off will be near as impressive as stuff you guys do cause I'm still a relative noob

It's not terribly fair to try to compare the stuff you've done with the stuff people like Zero and I have done though. You program in C++ iirc from the talkhaus which is a completely different beast than Zero's php or my javascript. And while you might be less experienced than me at programming from what I've seen you've got a stronger base going. I tend to learn in a really haphazard and seemingly aimless (well not really aimless but it's spastic at least) fashion whereas what I've seen from you seems better regimented.

Most of what you've seen from me has actually been pretty simple anyways all things considered. The most complex stuff is the canvas stuff I think. If you go and look through the more complex stuff I've done on JSFiddle you'll see that it's all actually reasonably simple in the end:
Magnifying glass for images
One of my recent attempts at an animated canvas
FPS graph using an animated canvas

For that last one especially the basic function of it was taken almost verbatim from an example and a fair amount of it (lines 21-25) are still almost identical to the example. That might not sound like much but those lines are some of the most vital to the entire script.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 6th, 2015, 11:06 am

Kinda odd and possibly stupid question but I'm not sure what I'd even need to search for sure to find something like this. Does anyone know if there's something that would allow me to write with a code style I'm used to (Javascript, php, and C# all share the same basic C-based syntax style) then automatically convert that to Lua's weird choice?

For example:

Code: Select all

if (varhere < 100) {
    //do this
} else {
    //do this
}

instead of Lua's weird

Code: Select all

if varhere < 100 then
    --do this
else
    --do this
end

format which I personally find to be far more unreadable. It's the main factor other than procrastination that has stopped me from really looking into LunaLua so far.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 503
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » December 6th, 2015, 6:28 pm

I can't say I know anything about Lua, maybe ask in /r/lua and see if anyone there knows?
Image
► Show Spoiler

Speedless
Posts: 129
Joined: March 20th, 2015, 12:11 am
Location: Seeking eating

Re: Programming Thread

Postby Speedless » December 6th, 2015, 7:07 pm

Well, I recently got a fantastic programming book by the central figure in C++, Bjarne Stroustrup. I'm only five chapters in (of 20+), and the book hasn't even started actually teaching us about classes yet (which is where it seems like the level of stuff you can reasonably do in C++ really explodes), but it's already laid down a really solid foundation (and provided a clearer explanation of, and more compelling argument for, using exceptions to handle errors than I ever found on the Internet). The one thing I've noticed that's a little weird about it is that some of the example code is formatted differently. Some of the function definitions, for example, put the opening brace of the function body on the same line:

Code: Select all

int main() {
   // things
}

and others put it on its own line:

Code: Select all

int main()
{
   // things
}

And I've heard from a couple different sources, including the book itself, that keeping your coding style consistent within a given project is really helpful for readability. It's a little thing, to be sure, but I feel like if I were writing a book about programming, it'd drive me crazy while proofreading if any little piece of code was formatted differently.

Alice wrote:Kinda odd and possibly stupid question but I'm not sure what I'd even need to search for sure to find something like this. Does anyone know if there's something that would allow me to write with a code style I'm used to (Javascript, php, and C# all share the same basic C-based syntax style) then automatically convert that to Lua's weird choice?

For example:

Code: Select all

if (varhere < 100) {
    //do this
} else {
    //do this
}

instead of Lua's weird

Code: Select all

if varhere < 100 then
    --do this
else
    --do this
end

format which I personally find to be far more unreadable. It's the main factor other than procrastination that has stopped me from really looking into LunaLua so far.

Well, I'm pretty sure it's possible to make a program that parses code in one format and then writes code in another. It's probably pretty tricky, though, and I don't know whether there are any existing code-conversion tools that have been released to the public. And even if there are, my guess is that most of them would be specialized for one specific conversion.

Apropos to not much, but Lua's choice to use end instead of curly braces to define the boundaries of a block of code seems to be the major other way to do it. There are a couple other languages, like Pascal/Ada and some varieties of BASIC, that end blocks with an "end" statement of some kind.

User avatar
chridd
Posts: 210
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » December 6th, 2015, 7:33 pm

Speedless wrote:The one thing I've noticed that's a little weird about it is that some of the example code is formatted differently. Some of the function definitions, for example, put the opening brace of the function body on the same line:

Code: Select all

int main() {
   // things
}

and others put it on its own line:

Code: Select all

int main()
{
   // things
}
Are you sure that there are function definitions (as opposed to other constructs that use braces) that have the brace on the same line? Because there's an indentation style that has braces on their own line for function definitions, but not for if statements, loops, class/struct definitions, etc.

User avatar
Ashan
Posts: 503
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » December 6th, 2015, 7:36 pm

I've found that with Java, everybody leaves the bracket after the arguments

Code: Select all

void func() {
   //stuff
}

while with C++ they put it under

Code: Select all

void func()
{
    //stuff
}

I've personally stuck with doing it the second way since it keeps your opening and closing brackets aligned. But I'm usually writing C++ anyway, and in the off-time I write in Java I end up leaving the opening bracket up top. I just do whatever everyone else does. When in Rome.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 6th, 2015, 7:50 pm

Ashan wrote:I can't say I know anything about Lua, maybe ask in /r/lua and see if anyone there knows?

I dunno that Reddit would be all that helpful honestly. In my experience any place where a large amount of programmers congregate asking a question like that is more likely to start some idiotic argument than actually get useful help.
Speedless wrote:Well, I'm pretty sure it's possible to make a program that parses code in one format and then writes code in another. It's probably pretty tricky, though, and I don't know whether there are any existing code-conversion tools that have been released to the public. And even if there are, my guess is that most of them would be specialized for one specific conversion.

It probably wouldn't even be too terribly difficult to write one if I really tried. I was just hoping that there'd be one already made and easily accessible so I didn't have to bother.
Apropos to not much, but Lua's choice to use end instead of curly braces to define the boundaries of a block of code seems to be the major other way to do it. There are a couple other languages, like Pascal/Ada and some varieties of BASIC, that end blocks with an "end" statement of some kind.

And COBOL too from what I saw earlier while looking it up after someone asked peoples' opinions on it on another forum I frequent.
Ashan wrote:I've found that with Java, everybody leaves the bracket after the arguments

Code: Select all

void func() {
   //stuff
}

while with C++ they put it under

Code: Select all

void func()
{
    //stuff
}

I've personally stuck with doing it the second way since it keeps your opening and closing brackets aligned. But I'm usually writing C++ anyway, and in the off-time I write in Java I end up leaving the opening bracket up top. I just do whatever everyone else does. When in Rome.

I prefer the first way personally. After something like your "void func()" there it's pretty clear that it's going to be followed by an opening curly brace so you can save a line by just putting it on the same line. I do the same with else statements. I see a lot of people do it this way:

Code: Select all

}
else
{

where

Code: Select all

} else {

saves space and is just as easy to read in my opinion. (Actually easier to me but that's probably because I'm used to it.) It really boils down to which you find most intuitive though and you shouldn't let anyone else tell you that you're wrong without at least giving some logic on the matter.
💙💙💙
Image
Image


Speedless
Posts: 129
Joined: March 20th, 2015, 12:11 am
Location: Seeking eating

Re: Programming Thread

Postby Speedless » December 6th, 2015, 11:18 pm

chridd wrote:Are you sure that there are function definitions (as opposed to other constructs that use braces) that have the brace on the same line? Because there's an indentation style that has braces on their own line for function definitions, but not for if statements, loops, class/struct definitions, etc.

Ohhhhh, okay, that makes more sense. Yeah, I double-checked the book, and it's definitely following Stroustrup style, treating function definition braces differently. Which looks super weird to me and is something I would never even have thought of--I think this is my first brush with an indent style that doesn't treat same-line/separate-line as a binary choice, and I've always thought it made sense to format all kinds of curly braces the same way. Doesn't help that I sometimes have trouble figuring out what to call a concept like "indent style" in order to search for it on the Internet. Fact-checking 1, Speedless 0. :tstick: Interesting to know that I feel the same about "cuddled elses" as Stroustrup, anyway.

Ashan wrote:I've found that with Java, everybody leaves the bracket after the arguments

Code: Select all

void func() {
   //stuff
}

while with C++ they put it under

Code: Select all

void func()
{
    //stuff
}

I've personally stuck with doing it the second way since it keeps your opening and closing brackets aligned. But I'm usually writing C++ anyway, and in the off-time I write in Java I end up leaving the opening bracket up top. I just do whatever everyone else does. When in Rome.

It's funny, I always had the impression that it was the other way around--that it was C-family languages where same-line opening braces were most common. Maybe there are just groups of programmers for each language that use each style, and which language you associate with which style depends on which group(s) you learn the language with?

Left to my own devices, I go with same-line. I don't particularly mind the uniform separate-line style, but I think maybe typing Space-{ feels a little faster or smoother to me than Enter-{ for some reason.

User avatar
Absolut Zero
Site Developer
Posts: 610
Joined: December 24th, 2014, 3:51 am

Re: Programming Thread

Postby Absolut Zero » December 6th, 2015, 11:25 pm

Oh man so much new stuff to read in here! I dont have time now but will love to go over this later tonight

User avatar
Ashan
Posts: 503
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » December 7th, 2015, 6:32 am

Absolut Zero wrote:Oh man so much new stuff to read in here! I dont have time now but will love to go over this later tonight

Prepare to have your mind blown on the topic of which line to put your opening curly brace on. It'll change the way you program forever!
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 7th, 2015, 7:56 am

Ashan wrote:Prepare to have your mind blown on the topic of which line to put your opening curly brace on. It'll change the way you program forever!

Probably actually isn't a terribly uncommon topic for a thread about programming anyways, lol.
💙💙💙
Image
Image


User avatar
Tovarisch Red Yoshi
Resident Commie Chameleon
Posts: 1943
Joined: December 29th, 2014, 10:57 pm
Location: Ashtree

Re: Programming Thread

Postby Tovarisch Red Yoshi » December 7th, 2015, 8:22 am

Perl is the best language because it can both into C syntax and be like infinitely obfuscated

although i don't actually know how to do the second part
wikipedia wrote:The word "w00t" itself was first seen in 1994.[citation needed] The expression rose in popularity in the late 1990s and early 2000s (decade) mostly on MMORPG such as RuneScape. It remains a niche Internet term and is not in general usage. The symbolic approximation of Latin letter forms makes w00t a prime example of internet leetspeak. It may also sometimes be seen spelled as "wewt" or "wought".
Isocitration wrote:<Isocitration> a long obscure nonsequitur that must be explained
<Isocitration> the joke is funny because of that alone
<tovakj> you've known me how long, yet?
<tovakj> yes
<tovakj> you're finally figuring out my aesthetic

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 7th, 2015, 7:35 pm


It's nothing terribly impressive or complex but I'm lazily working (I've actually been procrastinating a crap load on several different things I intend to do because I've been really tired lately) on redoing our audio tags a bit and came up with this as a basis for the play/pause button. That's done only using CSS and jQuery to switch between states. To be fair though the triangle bit I got from here after a failed attempt at my own using the border-radius property. (Also a failed original attempt at the pause button using the ::after pseudo-element which was actually far more complicated than the method I used here just using borders too, lol.)

Edit: In case it's not clear you can click it.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 8th, 2015, 10:28 am

I ended up having to write what's probably some of the hackiest code I've done yet in javascript. I randomly decided to update the user script I wrote for the Dynasty forums to try to fix an issue with the quick reply stuff I added. The quote to quick reply button wouldn't allow you to open the new reply window like you used to be able to do with middle click. For some reason though simply changing my replace command to prepend the normal quote link or prependTo it completely broke everything. At first one of the commands just make neither quote button appear while the other one dropped my quick quote button into the normal quote link. Eventually I got this sorted out and both links were working. However for some completely random reason it would now drop five copies of the quick reply button into the controls. In fact this was worse at first because it would randomly drop FIFTEEN because there's some issues with my code. Partly related to my inexperience when I wrote the script and partly related to an extremely stupid oversight on my part. There's three separate places the quick reply code might get called from. Rather than doing something smart and writing a single function and calling said function from each of those three places I apparently decided the better idea was to copy+paste the code to each spot. All three spots are widely spread out throughout my code too... (I really don't know why I did this. Even when I first made this script I knew better than to do something so incredibly stupid...)

Anyways after not getting that to cooperate I decided to try a new approach. I added a middle click handler to my code and intended to open the standard quote page when you middle click the button. This turned out to be an incredibly massive chore because javascript (I'm assuming for security reasons just like with dealing with the user's local filesystem) doesn't actually let scripts open a new tab. I eventually found a way to do it with this code:

Code: Select all

GM_setValue("newquote", "true");
var url =  $(this).attr('id');
url.replace(/&amp;/, "&");
var form = document.createElement("form");
form.method = "POST";
form.action = url;
form.target = "_blank";
document.body.appendChild(form);
form.submit();

We'll skip over the first line of this for now since that's relevant for later. The second line is pretty hacky but it was the way I used to store the standard reply link for the quotes for my quick reply function since it needs to load the reply page in the background and scrape the actual reply from the page. The url.replace is actually extraneous in the final version. It came from one of the major issues with this method. For some reason that I don't understand this code as it is will load an error page. If you refresh that page however it works. When I was troubleshooting it I, at first, though it was because for some reason console.log(form) was returning a "&amp;" in the url rather than the normal ampersand that was supposed to be there. I later realized this wasn't the issue at all and moved on to working around it instead since I have absolutely no idea what's causing the error. The rest of the code basically opens the reply page. It tries to open it in a new tab (I tried several other ways but this is the only one that reliably worked for me) but there's one other major issue with it. For some reason it needs pop-up windows to be allowed.

Anyways now for that first line. That bit is accompanied by this code at the very start of the file:

Code: Select all

var newquote = GM_getValue("newquote", "Not Set");
console.log(newquote);
if (newquote === "true") {
   GM_setValue("newquote", "False");
   window.location.href = window.location.href;
}

As you saw earlier the first line is setting the "newquote" value to "true". This code retrieves the value stored to "newquote" and if it's equal to "true" then we set it to false then refresh the page. This works around that error page issue by basically automatically refreshing the page as soon as it's done loading so the user doesn't have to do it themselves. Though they will get the error screen for a few seconds.

While my script does work I think I'd actually be better of rewriting this entire thing more or less from scratch to try to fix the stupidity and some of the issues with it. I'm pretty confident I can do significantly better, though it will take a fair amount of time. I'm definitely not going to do it within the next few days at least however. I've been procrastinating on a job for the scanlation group I'm a member of and I have another job I need to wrap up and submit as well. (Also there's the audio tags thing I mentioned last night though I accomplished a fairly large chunk of that after my post about it.)
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 8th, 2015, 9:23 pm

Alice wrote:While my script does work I think I'd actually be better of rewriting this entire thing more or less from scratch to try to fix the stupidity and some of the issues with it. I'm pretty confident I can do significantly better, though it will take a fair amount of time. I'm definitely not going to do it within the next few days at least however. I've been procrastinating on a job for the scanlation group I'm a member of and I have another job I need to wrap up and submit as well. (Also there's the audio tags thing I mentioned last night though I accomplished a fairly large chunk of that after my post about it.)

And this is how I procrastinate. By doing something that consistently infuriates me, lol. I went to do the QC job for my scanlation group only to find out someone else got to it before I could so I ended up working on rewriting my userscript. I completed it though. Rewrote basically everything from scratch or at least very heavily modified 90% of the code I copy+pasted from the old one. It's more efficient, far better formatted than the old one, better commented other than the stuff that I didn't modify much, fixes several bugs and issues, and loads an external stylesheet hosted here so I can more easily edit it. Took nearly twice as long as it had any reason to take. Often because of me overlooking simple things.
💙💙💙
Image
Image


User avatar
Absolut Zero
Site Developer
Posts: 610
Joined: December 24th, 2014, 3:51 am

Re: Programming Thread

Postby Absolut Zero » December 9th, 2015, 12:07 pm

The whole thing about where the opening brace goes for definitions is really up to personal preference. There are some open source projects that have coding practices outlined for a specific format to be followed, but those are recommendations really. The most important thing about it is to be consistent. Don't flip flop on your coding style. Most programmers really appreciate it when code is well maintained and easy to read.

That being said, I much prefer and always write my code with brackets like this;

Code: Select all

void func()
{
    //stuff
}


I just think it looks cleaner and is more readable, but from what I have read this opinion is in the minority. Most coding practices I have read have it the other way. But In practice I have found it annoying if there are many arguments or complex nesting of arrays and or definitions that make that style look just nasty to me. Phpbb's coding practices suggest to do it with no line break before brackets as well as many other php projects but I am a rebel and dont conform to that nonsense lol

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 9th, 2015, 2:12 pm

Is anyone here experienced with Ajax requests? The quote to quick reply function in the userscript I posted earlier relies on an Ajax GET request through jQuery to retrieve post contents for the quick reply. There's a really weird issue with it however. It works perfectly fine the first time you click the quick quote button. If you ever click any other quick quote button on the page including the one you just clicked however the Ajax request no longer works. I've been looking around a bit on Stack Overflow and I'm seeing a handful of people who have had similar issues but so far I haven't seen anything useful about why it happens and how to prevent it.

Edit: Okay I think I might have found some useful info on it that I'll need to test out later. It may be because that bit of code is inside my document.ready function.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 9th, 2015, 6:02 pm

Alice wrote:Is anyone here experienced with Ajax requests? The quote to quick reply function in the userscript I posted earlier relies on an Ajax GET request through jQuery to retrieve post contents for the quick reply. There's a really weird issue with it however. It works perfectly fine the first time you click the quick quote button. If you ever click any other quick quote button on the page including the one you just clicked however the Ajax request no longer works. I've been looking around a bit on Stack Overflow and I'm seeing a handful of people who have had similar issues but so far I haven't seen anything useful about why it happens and how to prevent it.

Edit: Okay I think I might have found some useful info on it that I'll need to test out later. It may be because that bit of code is inside my document.ready function.

Okay, got this working. I'm still not entirely 100% sure on what the issue was because the Ajax request was apparently returning correctly as it turns out. Just for some reason it was randomly not retrieving data off the page after the first attempt.

Also found out that I missed a window.varname somewhere when I used the same variable in another place for the exact same function which got really confusing when I was getting inconsistent results with my fix for it, lol.
💙💙💙
Image
Image


User avatar
Absolut Zero
Site Developer
Posts: 610
Joined: December 24th, 2014, 3:51 am

Re: Programming Thread

Postby Absolut Zero » December 10th, 2015, 9:17 am

Ajax is annoying to work with most of the time. I have only used it a handful of times and I have been trying to incorporate some into the new extension for a couple features I plan. I may end up scrapping those features though since they are extremely complex.

Also the thing about putting Ajax calls in a document.ready function is something I have heard not to do, since that would be taking some of the asynchronous-ness out of it. As for your information loss maybe there is some variable that is falling out of scope and cant be accessed after the document.ready function is ran or something I dont have any code to look at so I am just speculating.

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 10th, 2015, 12:42 pm

Absolut Zero wrote:Also the thing about putting Ajax calls in a document.ready function is something I have heard not to do, since that would be taking some of the asynchronous-ness out of it. As for your information loss maybe there is some variable that is falling out of scope and cant be accessed after the document.ready function is ran or something I dont have any code to look at so I am just speculating.

I moved it out of the document ready function first but that turned out not to be it. And checking variable scope was the first thing I checked afterwards. You can see the code for it here. Line 548 is the specific variable that was only working once and the only thing that has really changed with that is exactly what I'm setting to the variable. You can see the old version here. The only difference in variable scope would be that in the new version I'm declaring the variable inside the main function instead of the Ajax function but that wasn't the issue at all. Line 488 is specifically where I make use of the variable there though.

It works now so I'm not too terribly worried about it but I actually would like to know why it wasn't working after the first use.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 10th, 2015, 6:25 pm

jQuery's .insertAfter() command is a bit wonky.

Code: Select all

$(link).insertAfter('div.forum_topic:eq(' + i + ') span.pages');

This code inserts the value of the variable "link" after the element(s) selected by the insertAfter command. This is weird because effectively the insertAfter() command is the selector here which is completely backwards compared to how jQuery normally works which would normally be like this instead:

Code: Select all

$('div.forum_topic:eq(' + i + ') span.pages').insertAfter(link);

It does make sense after you realize how it works but it feels rather inconsistent to me since $() is supposed to be jQuery's selector command.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 12th, 2015, 11:44 pm

http://jsfiddle.net/Alice3173/1r9j9esh/7/

(Not embedding this time because the layout currently doesn't show everything since the size types tooltip deal is too long for the current method.)
I've been working on a customization menu for the magnifier option in my script. The part where it actually saves and loads settings isn't included in this version since that needs to actually be in the script to work (it's a Greasemonkey specific function) and the layout needs some work but other than those two details it's more or less done. Each of the fields will have a red border if they have an invalid value (though the number fields do that by default, I had to add it in myself for the others) and if you still try to submit it then you'll get a message telling you exactly what you screwed up on. I may make the error message a little bit more in-depth though. I think I might at least add the invalid value inside some parentheses at the end of the error line.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 17th, 2015, 6:56 pm

https://jsfiddle.net/Alice3173/uq99kdnu/8/

I'm having a strange issue, lol. I decided to make a script in javascript to calculate whether a number is prime. I screwed something up somewhere trying to fix it up so now it doesn't like to return a number as prime. Or rather it likes to return every number as prime. I need to figure out where I screwed up though because I had it return every prime up to 1000 correctly earlier. (Or well every prime up to 137 and at least several up to 1000. I didn't comprehensively check every single entry but every one I checked against a list of the first 1000 prime numbers was correct.)

I think the basic intent behind my method is correct though. As the script finds prime numbers it adds them to an array. Then it checks against the array first when it's looking for whether a number is prime since every number below the last entry in the array that isn't in the array is going to be a moot point since they're divisible by one of the numbers in the array. If we successfully reach the last number in the array then we brute force every number starting at the last number in the array+2 and going up to the floored value of half our current number.

It'll have to wait for me to actually figure it out however because I have other things I should be working on at the moment.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 17th, 2015, 10:49 pm


It looks like I've gotten it working now. Just be careful with larger numbers, especially when you haven't done lower numbers first. It'll take awhile and you'll risk your browser crashing. (Ie: 1001 -> 100000 crashed Firefox for me.) I haven't checked thoroughly but it looks like the numbers it's returning are indeed primes too. I wish I could find a way to do it that didn't involve locking up the user's browser while it was happening. It seems like, in Firefox at least, a script that will have an involved process such as this one will take priority over literally anything else you're trying to do in the browser. I'd prefer it to run as a low priority process in the background of what the user is doing but I'm not sure how.

Edit: Also it misses even factors (other than two) of even numbers you input because of the way I'm handling things. No point in calculating other even factors since the purpose of the script is to figure out if the number is prime or not and it being divisible by 2 is enough for that.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 22nd, 2015, 5:37 am

Well this is one of the more strange issues I've come across. For one manga I've come across the magnifier in my Dynasty userscript refuses to actually load an image. I'm using a simple jQuery selector to retrieve the path to the current page's image.

Code: Select all

if (pageurl.match(/chapters/)) {
          pWidth = $('div#image.thumbnail img').width() || $('div#image').width();
          pHeight = $('div#image.thumbnail img').height() || $('div#image').height();
          imgPath = $('#image img').attr('src') || "none";
        } else if (pageurl.match(/images/)) {
          pWidth = $('div.image img').width();
          pHeight = $('div.image img').height();
          imgPath = $('div.image img').attr('src') || "none";
        }

According to that code this:
Image
should work fine for getting the image path but it doesn't work. On the other hand this:
Image
works perfectly fine as it should. For reference this is the one that doesn't work and this is the one that does work from that example. The weird part is the pWidth and pHeight variables are getting set properly. And it's not the fact that the image path one is just #image rather than the div#image of the other two variables. I've changed it anyways for consistency but it still doesn't work. I've also tried changing the relevant line to this:

Code: Select all

imgPath = $('div#image.thumbnail img').attr('src') ||$('div#image img').attr('src') || "none";

to no avail. Even more interesting if I type my code into the console I get this:
Image
which is completely correct.
💙💙💙
Image
Image


User avatar
Absolut Zero
Site Developer
Posts: 610
Joined: December 24th, 2014, 3:51 am

Re: Programming Thread

Postby Absolut Zero » December 23rd, 2015, 12:48 am

That is really weird alice, there doesn't seem to be any syntax errors that I can see with that snippet. You do have the OR pipes not spaced between the selectors though I dont think that would cause an error. Thats the only nitpicking thing I cans see wrong with it. Those kinds of problems are super annoying

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 23rd, 2015, 1:16 am

Absolut Zero wrote:That is really weird alice, there doesn't seem to be any syntax errors that I can see with that snippet. You do have the OR pipes not spaced between the selectors though I dont think that would cause an error. Thats the only nitpicking thing I cans see wrong with it. Those kinds of problems are super annoying

Yep, I noticed that after I made the post. And I got some users of the script to link some others where it also doesn't work (the one I linked here was the only one I'd personally come across) so I'll have a better pool to work off of when I start looking into it more seriously. I'm sure it's actually an error I made somewhere but it's seriously a strange one since it's reliably reproducible yet works on a seemingly random basis and doesn't seem like it should even happen in the first place.
💙💙💙
Image
Image


User avatar
Nixitur
Posts: 25
Joined: December 23rd, 2015, 3:19 pm
Location: The internet
Contact:

Re: Programming Thread

Postby Nixitur » December 29th, 2015, 2:30 am

Alice wrote:It looks like I've gotten it working now. Just be careful with larger numbers, especially when you haven't done lower numbers first. It'll take awhile and you'll risk your browser crashing. (Ie: 1001 -> 100000 crashed Firefox for me.) I haven't checked thoroughly but it looks like the numbers it's returning are indeed primes too.

I don't know much about Javascript, but it seems like you go through all numbers smaller than input/2 to check for factors which works, but is kind of inefficient.
In fact, you only need to check up to the square root of input. Because if there's a number a > sqrt(input) such that a*b = input, then b must be smaller than sqrt(input). So, only iterate up to sqrt(input) and if you find b that divides input, push both b and input/b to the list of factors and you should have the same result, but much faster.
Last edited by Nixitur on December 29th, 2015, 2:44 am, edited 2 times in total.
Image

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 29th, 2015, 2:44 am

Nixitur wrote:I don't know much about Javascript, but it seems like you go through all numbers smaller than input/2 to check for factors which works, but is kind of inefficient.
In fact, you only need to check up to the square root of input. Because if there's a number a > sqrt(input) such that a*b = input, then b must be smaller than sqrt(input). So, only iterate up to sqrt(input) and if you find b that divides input, push both b and input/b to the list of factors and you should have the same result, but much faster.

You might be right on that. I know that a lot of algorithms you can find through a search do something with square root. I've just never seen an explanation of why they do that and didn't put much thought into it.
💙💙💙
Image
Image


User avatar
Nixitur
Posts: 25
Joined: December 23rd, 2015, 3:19 pm
Location: The internet
Contact:

Re: Programming Thread

Postby Nixitur » December 29th, 2015, 2:45 am

Well, I hope my explanation is sufficient.

Also, I just looked over it again and if I read it correctly, you don't even need all that "brute force up to an upper limit" stuff. After all, the way that code is called, you already know all the prime numbers under n before you call check(n+loopincr). So, you could probably take out that entire loop without losing any functionality. Checking for divisors among the primes array should be sufficient.
However, in that case, you also go through the entire primes array which is not necessary. The loop should break if primes[h] is larger than sqrt(input).
Image

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 29th, 2015, 2:51 am

Nixitur wrote:Also, I just looked over it again and if I read it correctly, you don't even need all that "brute force up to an upper limit" stuff. After all, the way that code is called, you already know all the prime numbers under n before you call check(n+loopincr). So, you could probably take out that entire loop without losing any functionality. Checking for divisors among the primes array should be sufficient.

Now that I'm thinking on it this way I think you may actually be right. Though with that square root deal I don't think the brute force loop would actually end up seeing any use anyways since the latest primes are, I believe, always going to be higher than the square root of the current number.
However, in that case, you also go through the entire primes array which is not necessary. The loop should break if primes[h] is larger than sqrt(input).

Why's that?

Edit: Oh wait, I think I see what you mean now. Since we don't need to deal with numbers over the square root of our input number there's no point in using primes larger than that number either, right?
💙💙💙
Image
Image


User avatar
Nixitur
Posts: 25
Joined: December 23rd, 2015, 3:19 pm
Location: The internet
Contact:

Re: Programming Thread

Postby Nixitur » December 29th, 2015, 3:38 am

Alice wrote:Oh wait, I think I see what you mean now. Since we don't need to deal with numbers over the square root of our input number there's no point in using primes larger than that number either, right?

Yup, that's exactly what I mean.
I also noticed that you do a lot of things every time check is called, like setting the #primes, #output and #factors elements. That can't take much time, but it's nonetheless superfluous, since you could just do that only once after the very last call (which should be check(input)).
I changed it up a bit, so it should now run a bit faster. The check function itself never does any UI stuff, it's only concerned with factors and primes. To illustrate how much faster it is, going to 100000 from the start only takes about 1.5 seconds.
I also noticed that loopincr is always 1 if input is even which means that you check all numbers smaller than input if input itself is even. That includes even numbers. I think the intention might have been that you only ever check odd numbers? That's not really what it does, though. If I change it to only check odd numbers except for input itself, it still correctly finds prime numbers, but doesn't find any factors, I'm not sure why.
The difference is huge as giving 1,000,000 as an input makes Firefox crash, while 1,000,001 doesn't. It still freezes the browser for quite a while, but hey, better than nothing.

Also, for running parts of scripts in the background, take a look at Web Workers, they sound like exactly what you want. You would only need one Worker, I think, so it shouldn't be too complicated. Fiddle appears to only support one script file, though, so as far as I can see, that wouldn't work.
Image

User avatar
Alice
⦂☽
Posts: 3809
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 29th, 2015, 4:04 am

Nixitur wrote:I also noticed that you do a lot of things every time check is called, like setting the #primes, #output and #factors elements. That can't take much time, but it's nonetheless superfluous, since you could just do that only once after the very last call (which should be check(input)).

That sounds right. I didn't exactly plan that script out before trying it so there's probably quite a few places where something's kinda wonky.
I changed it up a bit, so it should now run a bit faster. The check function itself never does any UI stuff, it's only concerned with factors and primes. To illustrate how much faster it is, going to 100000 from the start only takes about 1.5 seconds.

Seems to crash Firefox. I've tried 100,000 twice now and it crashed both times for some reason.
I also noticed that loopincr is always 1 if input is even which means that you check all numbers smaller than input if input itself is even. That includes even numbers. I think the intention might have been that you only ever check odd numbers? That's not really what it does, though. If I change it to only check odd numbers except for input itself, it still correctly finds prime numbers, but doesn't find any factors, I'm not sure why.

I think I might see where I messed up. It looks like I'm only ever checking loopincr one time when what I was intending to do, if I recall correctly, was to check if we're on an even number and if so then we increment by 1 then we later check if we're on an odd number and if so we increment by 2 instead to skip evens.
Also, for running parts of scripts in the background, take a look at Web Workers, they sound like exactly what you want. You would only need one Worker, I think, so it shouldn't be too complicated.

That might be worth looking into actually. Javascript doesn't seem to make it easy to separate UI and behind the scenes work or multiple parts of the behind the scenes stuff which could be pretty useful in a case like this.
💙💙💙
Image
Image



Return to “General Perversion”

Who is online

Users browsing this forum: No registered users and 1 guest