Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 526 527 [528] 529 530 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907941 times)

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7905 on: September 22, 2015, 05:19:52 pm »

Yeah, with something like the Internet, "Millions of people use it" is actually a very damn good reason not to change things. Breaking one of the biggest financial engines of the modern age in order to suit philosophical decisions arrived at after the fact is not going to happen. Things like that don't change unless they're broken; and being hard to use doesn't qualify as broken. That's why, even if DVORAK is demonstrated tomorrow to retroactively cure carpal tunnel and give the user free blowjobs, the world will keep on using QWERTY.
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7906 on: September 22, 2015, 06:06:31 pm »

I dunno, that'd convince me to take a moment and learn DVORAK. :P
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7908 on: September 22, 2015, 07:06:14 pm »

I dunno, that'd convince me to take a moment and learn DVORAK. :P
Me too. Carpal tunnel is basically Hitler.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7909 on: September 22, 2015, 07:29:13 pm »

Anyone know a tip for if and if else statements?
Not entirely sure what you're asking. You mean like this:
Code: [Select]
if (condition)
    something;
else if (condition)
    something;
else
    something;

Thanks i'll be testing this out and modifying as I go along. I'm paranoid that I haven't been using my head enough since answers to problems don't come in as fast as I want them to.




Why are you calculating the answer before taking the inputs? You do realize everything is calculated in sequence, rather than retroactively?

Where is userRequest defined?

Why did you put an "endl" at the end of " cin >> radiousC"?

Why is "cout << circleAnswer<< endl;" not inside of an else block, if you want it to print error instead of the answer? According to the code, it will print "Error" and then the answer, onto the same line even. But I guess that was your question in the first place.
1. Pi will always be 3.14 and the exponent of 2 will always be their. All you need for a circle formula that isn't constant is the radius. Or am I missing something?
2. Defined in the original program. I decided not to copy everything and make a huge post.
3. IDK. I just could and it's become a habit.
4. ...Yeah...I just started. That's my only excuse.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7910 on: September 22, 2015, 08:22:17 pm »

The problem is a thing call logic flow. Flow starts at the top and goes to the bottom, taking into account loops etc. Any equation you use is computed at the location in the code where it appears.

so if you have (just pesudocode):
Code: [Select]
x = 1
y = 2
f = function(x,y)
cin << x
print f
Then 'f' will have been computed as function(1, 2) regardless of what x value the user inputs, because that happens after the f-value is calcuated.

Think of it like a cake recipe more than maths. i.e. the function would be equivalent to "mix the eggs and milk together", and the cin would be like "decide how many eggs to use". Putting "decide how many eggs to use" after the mixing step would be a clear error.

also, pseudo-code can help a lot when learning to code. with that you write out all the logic in English, or more like English than normal code, such that you can read what is happening (eventually you will be able to interpret the language punctuation in English and read out what the logic is doing without needing to explicitly do this).

e.g. pseudo-code like:

Code: [Select]
If radius is greater than zero, then
    print Circle Answer
else
   print "Error"

Makes it very clear what needs to go on, and you can check that your program is in fact structured to make this happen. Also, it's good to write pseudo-code as comments, then fill in the blanks to create your code:


Code: [Select]
//If radius is greater than zero, then
if(radius > 0)
//    print Circle Answer
    cout << circleAnswer;
else
//   print "Error"
   cout << "Error!";

So, your pseudocode becomes the comments for your program. It's a good practice for you to try falling into, because if you make a typo you can check that the pseudo-code actually matches what you implemented if you look back at the code much later and don't remember whether something is correct.
« Last Edit: September 22, 2015, 08:37:39 pm by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7911 on: September 22, 2015, 08:27:07 pm »

I don't understand though.

If X and Y are defined already (with X being redefined) shoudn't printing f (I'm assuming it's the same as cout << f;) perfrom the function as the numbers are their?
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7912 on: September 22, 2015, 08:36:53 pm »

x is redefined after f is defined.

f is not a shortcut for function(x,y), it is the result of function(x,y) at the moment that f is defined.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7913 on: September 22, 2015, 08:38:34 pm »

f was computed with the original value of x. if you changed x after calculating f, it won't change f as well. i'll put comments:

x = 1 // x now stores value "1"
f = x  // f now stores a copy of x. so it's also "1"
x = 2 // x is overwritten with "2".
print f // prints the value that was stored in f. which is "1".
f = x // overwrites the f value with current x, i.e. "2".
print f // prints the new f value, which is "2"

This is basically how all programming languages work, so you just need to get a handle on the order in which steps occur. Top to bottom and all that. Your problem in the circle program was calculating circleAnswer before you got the input. So the circleAnswer never gets updated properly based on the change in x.
« Last Edit: September 22, 2015, 08:55:09 pm by Reelya »
Logged

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7914 on: September 23, 2015, 12:32:00 pm »

Does anybody know a, well, "structured" markup language for document creation?
It bothers me to no end that in LaTeX (which has painful syntax to start with, I mean, putting an exclamation mark into an automatically indexed title was a feat requiring so much confusing boilerplate that I have to wonder when it's gonna break for some stupid reason and how am I going to fix it then?), Scribble, texinfo and the like I need to keep track of levels of sections and subsections by hand. I wanna be able to just begin and end sections, with subsections being automatically determined by being inside another section. Preferably something with as few different escape sequences as possible…
Logged
Taste my Paci-Fist

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7915 on: September 23, 2015, 02:40:34 pm »

I'm going to try DocBook for now.
Logged
Taste my Paci-Fist

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7916 on: September 23, 2015, 06:06:42 pm »

Apologies for the triple post here…
I don't know what these typesetting language guys are thinking while designing their languages. Every frikkin last one of them is awful for one reason or another, especially for having tons upon tons of special characters.
Take a look at DocBook, for example. It's XML, so it's got the ampersand as a special character to enter other special characters, like this:
Code: [Select]
&amp;Writing XML is a pain, mainly because most of the code is taken up by tags, all of them duplicate because someone thought it was a good idea to make the closing part of a tag named. Why that is is beyond me, as there is no programmatic ambiguity about which tag a close belongs to is unique from the structure alone. But, whatever, this is probably about some kind of easier error checking by making the writer formulate which tag he wants to close, which is kind of a strange thing to have as a supposed tool for the writer to signal his intent because most XML editors do that stuff automatically either way, so all that it leaves us with is visual clutter.

Ahem…
Now I found a nice Common Lisp library which can help me with that kind of stuff, called xml-emitter. For emitting a tag, you need to wrap some forms into another form which does the actual emitting into a stream.
The XML description looks something like this:
Code: [Select]
(with-tag ("section")
  (simple-tag "title" "The pains of typesetting")
  (simple-tag "para" "For some reason people think that designing languages for typesetting
should include as many barriers as possible, such as making characters, which the user
might want to typeset, prone to breaking the whole document."))
Of course this is more verbose than XML, but it allows me to write wrappers like this:
Code: [Select]
(defmacro titled (tag title &body body)
  `(with-tag (,tag)
     (with-tag ("title")
       ,(if (stringp title)
            `(xml-out ,title)
            title))
     ,@body))

(defmacro deftitled (name tag)
  `(defmacro ,name (title &body body)
     `(titled ,,tag ,title ,@body)))

(deftitled book "book")

(deftitled part "part")

(deftitled chapter "chapter")

(defmacro section (title &body body)
  `(titled "section" ,title
     ,@(mapcar (lambda (para)
                 (if (stringp para)
                     `(simple-tag "para" ,para)
                     para))
               body)))
who then allow me to write my document like this:
Code: [Select]
(book "Why, Oh Why!"
  (part "The Woes Of LaTeX"
    (chapter "That stupid Macro syntax"
      (section "Who thought making macros hardly distinguishable from text was a good idea?"
        "There seems to be something wrong with…"
        …))))
Now, this is concise and structured. Woohoo! I got what I want!

Only, I don't, because ampersand.
You see, I am trying to write a specification for my programming language Poslin. Poslin has an operation called `&`. xml-emitter automatically does all the escaping for me, so this code in Common Lisp
Code: [Select]
(section "&"
  …)
produces the following XML
Code: [Select]
<section>
  <title>&amp;</title>
  …
</section>
Unfortunately this then is converted to TeX (I think) and in some special circumstances, the ampersand produced by the DocBook compiler isn't correctly escaped in the TeX sources, which leads to some really strange error messages and generally this is very unpleasant all around because, dammit, why do these people see the need to make something like this so damn hard? It's not even like I can escape that thing somehow by hand now, because then the ampersand is correctly escaped by the DocBook compiler and I end up with superfluous characters in the output.
Logged
Taste my Paci-Fist

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7917 on: September 23, 2015, 06:12:23 pm »

Interesting discussions since I last checked this thread.


So apparently vim is the Dwarf Fortress of text editors. Is this thing worth learning, or does anybody have a suggestion for a lightweight coding-focused text editor on linux?

The main reason to learn Vim is for shelling into a remote system.  Once you've made that choice, it's a very powerful, very customizable editor suitable for almost any programming language.  (Java being the main exception.  A good auto-completing IDE is almost a requirement for overcoming Java's deficiencies as a language.)

If you don't expect to be dealing with servers, though, the learning curve may be steeper than you want to tackle.  Yes, it's about on par with Dwarf Fortress, except that you don't even have a list of valid keys on the side of the screen.  On the other hand, there are helpful patterns in the command structure, many of them enter muscle memory over time, and you get to define your own.

As far as something more lightweight, my co-workers have been transitioning from Sublime Text to Atom.  It even seems to have a Vim mode plugin, so I might get around to trying it.


Looked at that site, stumbled over Perl 6, looked it up, and holy shit it's powerful. Perl 5 is already very powerful and concise, but Perl 6 takes the cake and also every other confectionery, and they're still not done adding features after 15 years of development.

So I've been using Perl 5 extensively and reading Perl 5 books for ten years now, and I'm now an expert on all things Perl 5 (of which there are lots). This includes being able to read Perl 5 well, because apparently Perl 5 is hard to read for people who haven't done a lot of it. But Perl 6, that's a whole new shipment of alphabet soup. Perl 6 has every single feature you could possibly imagine in a programming language, and if it doesn't have a feature, then it has a feature to let you add that feature in three lines of code or fewer.

Perl 5 is hard to read and write if you haven't done a lot of it in the last six months.  After switching to Python, because I was so much more productive with it, I have occasionally wanted to write a Perl script because it was the right tool for the job, and keep getting tripped up over the right way to index a hash, among other things.  That last big program I wrote, that became so much more after rewriting it in a sane language?  I can't quite follow the logic flow anymore, and no longer have a system that runs it correctly.


The reason I liked it in Python is that it wouldn't flip a shit when trying to concatenate strings with various other things. And when using a strong-typed language, "forgot the type declaration" is usually the first stupid mistake I make.
This is one reason to perhaps learn a typed language before learning a non-typed language. Like learning to drive a manual before an automatic.

I sometimes accidentally write "for(int i=0; i< n; i++)" in Javascript because I'm so used to putting the type in, rather than your problem.

"The interpreter does everything for you" is both a blessing and a curse for learning to program.

The same can be said of "The compiler does everything for you."  When first learning to program, having the compiler take forever to spit out a lengthy list of massively confusing garbage because you forgot a semicolon is highly frustrating; then, when it actually compiles, it's far too easy to think that it's done even though your logic is terribly flawed in some nearly invisible way.  Languages that let you poke around with a REPL are much more helpful in some respects, and Python's help() function is often exactly what I need.

Then again, I wouldn't mind a statically typed language so much if it inferred the type from initialization and/or usage, and didn't take a noticable time to compile.  Unfortunately, those goals may not be compatible.


In other words, I really hate makefiles.  The C and C++ compilation process is just complicated if your IDE doesn't manage the makefiles for you.  VS is fantastic.  Using a raw text editor or minimal IDE is not, and I don't know of any good fully integrated IDEs for Linux that work well with C++.  My experience with Eclipse doesn't lead me to believe that it works well with C++.

It took me a good hour to realize that I'd modified a header file and added some properties to one of the structs, but because of the simplistic makefile I was using that didn't trigger all of the modules that included it to be rebuilt.  Some did, some didn't.  Fun ensued.

I've had good experience with hand-built makefiles for small projects, but the auto-generated ones used by most projects seem excessively inefficient and sometimes just wrong.  Then again, getting dependency file generation to work correctly is its own nightmare.

One of the main reasons I prefer Python over C/C++ is its sane module system.  When you import a module, you get that module as it is, not as it was the last time you compiled the importing module.  You can try to import a module whether it exists or not; if it doesn't, you can even detect that condition at runtime and switch to a fallback.  Importing a module automatically compiles it if necessary.  You can also import individual names from a module, with the same behavior.  And "global" variables live in their module's namespace, so they can be read or manipulated by other modules, but not accidentally.

Yes, all of that could be done in a statically-compiled language.  Header files are simply bad design.


enforcing Get and Set operators also prevents one of the most common bugs:

if(object.value = true)
  do_thing();

where you accidentally put a single-equals. Using a Get/Set paradigm would give a compiler error here, thus avoiding shipping with the bug. Writing Get/Set operators is pretty quick. Tracking down such a bug in 1000's of lines of code can take a while.

I once used a language where the single-equals operator was assignment at the statement level, but comparison within an expression.  It also had double-equals (==) and colon-equals (:=) operators for disambiguation, but the simple case was almost always correct.  Why don't more languages do that?


There's also optimization. Say you have a bunch of bools. You make Get/Set operators for each bool. Next, you want to optimize memory use, so you want to pack all the bools into bits in one internal int. If you wrote Get and Set operators for the bools, you can modify those functions to do your fancy bit packing for you. If you were directly editing a bool, you'd have to modify the client code in many files to change this internal storage behaviour.

This is where the property syntax of more modern languages is useful.  A simple instance variable can be changed into a property without modifying client code, but calling getter and setter methods invisibly.  (Granted, this re-enables the accidental assignment bug...)

What really baffles me is when a programmer creates public property-type getter and setter methods that do nothing but get and set a private instance variable with a nearly identical name.  That's exactly like having a public variable, but more verbose and slightly slower.  Yet some programmers do it by default...


Is there even any Flash in common use, besides sites that specifically gather unique Flash content (Newgrounds, Kongregate, boorus, etc.)? I didn't like Youtube's HTML5 player at first, but now I'd never go back to the flash player.

At my previous job, we used it to let the users record audio.  I'm pretty sure they still use it, considering that some of their clients are over a decade behind in browser installations, and HTML media capture still isn't widely implemented.


I dunno, that'd convince me to take a moment and learn DVORAK. :P
Me too. Carpal tunnel is basically Hitler.

Sadly, Vim is the main reason I stopped using Dvorak.  Muscle memory was already too strong, and the standard movement keys were scattered all over the place.

To stave off carpal tunnel, I use an AlphaGrip.  It's not perfect, and takes quite a bit of time to get used to, but it's generally more comfortable.


The problem is a thing call logic flow. Flow starts at the top and goes to the bottom, taking into account loops etc. Any equation you use is computed at the location in the code where it appears.

so if you have (just pesudocode):
Code: [Select]
x = 1
y = 2
f = function(x,y)
cin << x
print f
Then 'f' will have been computed as function(1, 2) regardless of what x value the user inputs, because that happens after the f-value is calcuated.

Think of it like a cake recipe more than maths. i.e. the function would be equivalent to "mix the eggs and milk together", and the cin would be like "decide how many eggs to use". Putting "decide how many eggs to use" after the mixing step would be a clear error.

I like the recipe analogy.  It reminds me of something I heard once, about programming being the art of breaking something down into instructions so simple that a dumb computer can follow them.

But yes, the disconnect here is the difference between declarative ("Define f as the result of applying function to x and y") and imperative ("Apply function to the current contents of x and y, and store the result in f") languages.  Math is almost always declarative, where programming is usually imperative.
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7918 on: September 23, 2015, 07:48:27 pm »

Same problem as before. It's the same calculator I need to make but i'm trying to make it so that I can get the answers I need before making it stupid proof (i.e giving input validations in) since I think it'll help me later.

Spoiler: the code (click to show/hide)

I really hate if statements and unfortunately I think my class is stepping into high gear mode.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7919 on: September 23, 2015, 08:28:35 pm »

I really hate if statements

That's a bit premature. I'm about 100% sure that your problem is with something that isn't actually if statements.
Pages: 1 ... 526 527 [528] 529 530 ... 796