Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 68 69 [70] 71 72 ... 796

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

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1035 on: January 30, 2012, 03:53:49 am »

Fun Fact 1: You cannot install or repair Windows from the install disk if you don't have a partition table.

You should probably get yourself a copy of this. Which contains tools to repair partion tables and all sorts of fancy useful programs. It's saved my ass more times than I care to count, mostly because I like to tinker with my computer a lot. So 90% of all problems are self inflicted.
Logged
alternately, I could just take some LSD or something...

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1036 on: January 30, 2012, 04:56:08 am »

Max, I personally don't hate windows. They've done a lot of good stuff, I like Gates, unlike others I actually think they've built a pretty neat OS, I'm just not keen on being forced to use their stuff. For Example: I'm not trying to discourage people picking C# because I hate windows, but because I'm incredibly selfish and every C# programmer means more software gets written that I won't be able to use. While that's fine for all the software I never want to use, it seems to be becoming more and more common for software I DO want to use, so if I can do my own little part to combat that trend, I will. :P

That said, I think I'm going to do some Ruby tutorials in here this week. I've got this mysterious hankering to talk about blocks, closures, duck typing, and the concept of "everything is an object or a message".
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1037 on: January 30, 2012, 05:04:55 am »

Fun fact #5: There is an error with Win7 SP1, and there is no solution except re-installing Win7, or manually adding/removing exactly the right files in exactly the right previous updates, which you should only attempt if you're an MS Update MVP VIP MSCD BS MF.
Fun fact #6: PC's no longer come shipped with the OS on a disc, you have to make a backup yourself. I hope it works... But that "backup" is 6 Dvd's, no way that's going to install in 15 mins, invalidating Fun fact #4.

Also, there's no decent sudo command in Win7. I want XP back! (Oh and the laptop I'm doing this on is actually her (own) company laptop, so no illegal shit allowed. What you pirate privately, nobody cares, but for companies they're pretty strict).

Hmm, this is getting slightly off-topic. Programming: java sucks. Writing my resignation letter today. Back in my day, programming was writing code, not juggling version numbers in POM files. My C++ project compiles! And runs! But it does Nothing except print lines of debugging which is A-OK with me!
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1038 on: January 30, 2012, 05:34:47 am »

Perl...dang homework...who's good at Perl?

Oo! Oo! Pick me!
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1040 on: January 30, 2012, 06:18:50 am »

Oh right, I'm giving Perl lessons! Totally forgot about that. One Perl coming up right now!
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1041 on: January 30, 2012, 06:52:03 am »

Ruby, part 1

So, Ruby. Ruby is a fun language, and its pretty easy to get started. You can handle downloading and setting it up, I hope. This tutorial is going to be a bit strange, because I'm in a weird mood - gonna be using the language to look at the language, yo!

Ruby is an interpreted language, which lets us do some interesting things. It also means that unless you tie it to an external platform specific library, code you write on one system will work on any other system, so long as your using the same version of Ruby to run it.

So let's write a ruby program! Open up a text editor, type
Code: [Select]
puts "Hello World", save the file with a .rb extension, and then run it with ruby. You can actually cut the "uts" off the puts, and just type
Code: [Select]
p "Hello World" if you're REALLY lazy. They aren't exactly the same, but they'll both accomplish what we need here.

We're done!

Of course, it's only that simple on the surface - like with most languages, there's an awful lot going on there in that deceptively simply line. "puts", in this case, is a method. A method, in Ruby, is a set of instructions for how an object should handle a message. The message, in this case, is just passing along a simple string object.

But what is the object that the string is getting passed TO, the object from which we are calling the puts method, the object to which we are passing this relatively simple message?

That's a question that's important to ask in Ruby, because there is always, any time you send a message, an object somewhere responsible for the response. The only time you don't need to specify the object is when you are executing code inside of it - Ruby assumes any non-directed method call will reference the object its defined within. That means our code is being written inside of some already defined object, and runs when it gets instantiated - along with a lot more code that we can't see, including the method definition for "puts".

To find out a bit more about what's going on, let's load the Interactive Ruby Shell (irb) (Instructions on how to do so based on OS). This is one of the benefits of an interpreted language - it lets us code, quite literally, in real time. You should see something like
Code: [Select]
irb(main):001:0>
First, let's recreate our earlier success. Type
Code: [Select]
p "Hello World" and hit enter. The code executed, immediately, and we get...
Code: [Select]
"Hello World"
 => "Hello World"
It did it twice? Not quite; it did push out two lines, but they are actually quite different. irb does a few things to help our coding and debugging, so let me explain that now.
That first line is the actual results of our "puts" call - puts references the I/O object, and pushes it's arguments out to the console or whatever is handling IO. The second line, though, is all irb. Whenever you execute code in irb, it helpfully displays the return value of the last line of code executed. In this case, the method "p" returns the string in addition to outputting it to the console.
We can assign that returned value to a variable, and by simply typing the variable name irb will confirm it does, in fact, contain the string "Hello World".
Code: [Select]
1.9.3p0 :028 > x = p "Hello World"
   "Hello World"
   => "Hello World"
1.9.3p0 :029 > x
   => "Hello World"

If we'd used "puts" instead of "p", things would have been slightly different - "puts" doesn't return anything except a nil value, which we'll talk about more later.

What's important here, though, is that irb lets us learn things about the code we're running. For example... it let's us see what object our code is running within. To do that, type
Code: [Select]
selfand hit enter. You should see
 
Code: [Select]
=> main
That's the object our code is working in. So, let's have some fun with this object we've discovered. For easy reference later, we'll start by saving it to a variable.
Code: [Select]
app = self
Now, if we want to see all the public methods defined for app, we can type
Code: [Select]
app.methods. Don't worry about reading all of that - most of it isn't important, but it's good to know how to do. Perhaps the most important lesson to take away is this: To call a method (such as "methods") on an object, the syntax is simply "object reference" period "name of method defined by object".

If you DO read it, you'll notice "puts" isn't on the list, though! What's going on? Didn't I say that this was the object that was handling that message we sent earlier?

Well, let's test. Remember, it's object, period, method.
Code: [Select]
1.9.3p0 :081 > app.puts
NoMethodError: private method `puts' called for main:Object
from (irb):81
from /home/glyphgryph/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
Ah, that's it! Like many other languages, Ruby has the ability to make methods private. This means they can ONLY be executed within the defined object - it doesn't accept messages by reference. Even though we are technically still executing our code with the object, we're acting like we aren't, so it's telling us to fuck off.
(Also, notice and appreciate that wonderful backtrace, letting us know exactly where our problem occurred)

Now, you may be wondering, what IS "main"? The answer is complicated - and it depends on what you're using to run your code. But we CAN find out a little bit more about it. Lets try
Code: [Select]
app.class or, if you prefer
Code: [Select]
self.classgiving us
Code: [Select]
=> ObjectThis lets us know that "main" is an instance of the incredibly vague "Object". Object is one of the most basic ruby classes, and nearly every other object in the language inherits from it. It comes with or bundles up a helpful package of methods that we can use on any object we come across (because they are all, ultimately, Objects). This includes the "methods" and "class" methods we've been using to figure out what's going on.

Now, this is getting long, but before we go I want to do one more thing - engage in the act of CREATION. Yes, we're going to make a new object! Technically, of course, we already did - when we typed "Hello World", we implicitly created an object. But now we're going to make one on purpose! To do that, you simply take a class, and call the "new" method on it. This method returns a new instance of the class! Since we don't know how to create a class, yet, let's just use the one we've already discovered. Make sure to assign it to a variable so we can reference it later!

Code: [Select]
1.9.3p0 :094 > thing = Object.new
    => #<Object:0xa19d8f4>
This lets us know we've got an object, and tells us the object's type and what we'll call it's unique identifier. Success!

Of course, we can't do much with it yet - generic Objects are pretty... well, generic. They don't have a whole lot going on! But we'll get into some more complicated stuff next time around.

P.S.
Remember, those Object methods can be used on pretty much anything in Ruby. For example
Code: [Select]
"Hello World".classwill tell you the class of "Hello World" (A String).
« Last Edit: January 30, 2012, 12:06:06 pm by GlyphGryph »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1042 on: January 30, 2012, 07:51:21 am »

Nice, thanks glyph. Looking forward to the next one, your ruby-fanboyism in our haxe-days got me interested in ruby, but I haven't found the time/effort yet to actually learn it :)
(but it comes right after lisp, lua and haskell!)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1043 on: January 30, 2012, 09:22:56 am »

Fun fact #5: There is an error with Win7 SP1, and there is no solution except re-installing Win7, or manually adding/removing exactly the right files in exactly the right previous updates, which you should only attempt if you're an MS Update MVP VIP MSCD BS MF.
Fun fact #6: PC's no longer come shipped with the OS on a disc, you have to make a backup yourself. I hope it works... But that "backup" is 6 Dvd's, no way that's going to install in 15 mins, invalidating Fun fact #4.

Also, there's no decent sudo command in Win7. I want XP back! (Oh and the laptop I'm doing this on is actually her (own) company laptop, so no illegal shit allowed. What you pirate privately, nobody cares, but for companies they're pretty strict).

Hmm, this is getting slightly off-topic. Programming: java sucks. Writing my resignation letter today. Back in my day, programming was writing code, not juggling version numbers in POM files. My C++ project compiles! And runs! But it does Nothing except print lines of debugging which is A-OK with me!

I wasn't aware of #5.  #6 is only part true, though, in my circumstances.  The MSDNAA is a program sponsored by Microsoft that universities can subscribe to.  It allows for the free/cheap distribution of some MS software on a limited basis.  The version of Win7 available to me there is a single iso of the retail version of Win7Pro.  Computers might not ship with these things, but under my circumstances, they're still available.  This is how I could do it in such a short period of time.

Perl...dang homework...who's good at Perl?

Oo! Oo! Pick me!

Basically we have to parse a Java source file and generate javadoc for it.  Some advice on regular expressions and anything else you think would be useful to this end would be much appreciated.  But mostly, can you write something up on regular expressions?  Our lessons are always easier to read than textbook lessons.
Logged

Derekristow

  • Bay Watcher
    • View Profile
    • Steam ID
Re: if self.isCoder(): post() #Programming Thread
« Reply #1044 on: January 30, 2012, 10:35:33 am »

What's your game about?
Sorry, but I'd prefer not to say exact what it's about until it's closer to completion.  I have a tendency of the starting something, then not entirely seeing it through, so I don't want to get anyone excited about it until I have a fair bit to show.

It does have a collision engine that I think is kind of cool, though (although, I hadn't made a real one before so it doesn't take much).  I'll write a post about how it works in a few hours once I'm in my programming class, see if anyone can point out anything horrendously wrong/broken/slow about it.  I haven't had any issues yet, but it's pretty basic at the moment and could be better.
Logged
So my crundles are staying intact unless they're newly spawned... until they are exposed to anything that isn't at room temperature.  This mostly seems to mean blood, specifically, their own.  Then they go poof very quickly.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1045 on: January 30, 2012, 10:47:14 am »

your ruby-fanboyism

Oh god, it has become that hasn't it? I've become that which I once derided... I am awash with feelings of shame and guilt. And I traded my objectivity for a language with a worse run time than the Java code I always scoffed at for running so slowly, sure that I would never abandon my love of C++.

Yet... Ruby is just... so... much fun to work with. I can't help it.

I have gone to the dark side - I must be insufferable.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1046 on: January 30, 2012, 11:11:02 am »

I've got some time to kill, and someone mentioned wanting to learn lisp, so I'll pick this up again
Talking with a lisp
Chapter 2
Where we left off last time:
  • Whenever you use Common Lisp, there's a Common Lisp image running in the background that processes the code
  • We use a REPL to talk to the image, I've used the Slime repl for Emacs in this tutorial. A REPL is a Read, Evaluate, Print Loop
  • Feeding a literal, such as a string or a number to the REPL will result in it printing that literal
  • Common Lisp uses S-expressions, which have the form (<function name> <input arguments>)
  • We can output things to the REPL using (format t format-string input-arguments): (format t "Hello, ~a" "World")
  • We can define our own functions using (defun <name> (<input arguments>) <code>)
  • The result of evaluating the last expression in a function serves as the return value for that function
  • We can save code in .lisp files and load the code with the load function or by hitting C-c C-k
Now, I could go on about all the most common functions found in Common Lisp, such as arithmetic functions, list operations et cetera, but honestly, that would get a little dry. So instead, I'll point you to a few useful resources on these subjects:
Successful Lisp covers nearly all subjects in-depth and is organized very well.
Practical Common Lisp is less extensive than Successful Lisp but uses copious examples and exercises and is well-written
The Common Lisp Hyperspecs are like MSDN or Javadocs, but then for lisp. If there's a function you don't know how to use, you'll find it in here, but it can sometimes be a bit hard to translate to practice as they give little examples.
The Common Lisp Quick Reference Card are the hyperspecs in condensed form and very usefull for searching for that one function you need.


So, with that done, let's continue our tour of the Lisp system.


As the full name of the REPL indicates, lisp first reads, then evaluates. But things are a bit more complicated than that. We can instruct both the reader and the evaluator to process it's input before evaluating it. This is done via macros.
The following are examples of reader macros:


Code: [Select]
'(1 2 3) ;This is the list 1 2 3.
#(12 3) ;This is the vector 1 2 3
#+CCL'(1 2 3)#+SBCL#(1 2 3) ;This is the list 1 2 3 if we're using Clozure common lisp or the vector 1 2 3 if we're using SBCL


And these are examples of evaluator macros (commonly just referred to as macros)


Code: [Select]
(loop for x from 1 to 10 do (format t "~a" x)) ;prints the numbers 1 to 10
(defun foo () :bar) ;introduces the function foo, which returns the keyword :bar. Anything preceded by a semicolon is a keyword and evaluates to itself
(cond
  ((= 1 2) :false)
  ((= 1 1) :true)) ;Returns :false if 1 = 2 and :true if 1 = 1


So, what exactly is a macro and a reader macro?
Well, processing Common Lisp code is a multistep process. First, the reader reads it and converts it into a symbolic representation using reader macros and lookup tables for functions, symbols et cetera. Then, this symbolic representation is fed to the macro processor, which takes the macros in this representation as functions and uses that to convert the code to other code. So a reader macro is a function for the reader and a macro is a function for the macro expander. We can actually access the macroexpander using macroexpand-1 and macroexpand-all:


Code: [Select]
>(macroexpand-1 '(cond
                   ((= 1 2) :false)
                   ((= 1 1) :true)))
 (IF (= 1 2) (PROGN :FALSE) (COND ((= 1 1) :TRUE)))
 T
(macroexpand-all '(cond
                   ((= 1 2) :false)
                   ((= 1 1) :true)))
 (IF (= 1 2) (PROGN :FALSE) (IF (= 1 1) (PROGN :TRUE) NIL))


As you can see, the cond macro is expanded into a series of if statements. In this case we've immediately got ourselves a recursive macro, as macroexpand-1 reveals. Just as normal functions can be recursive, so can macros, as they are just functions that take code and produce code.


We can introduce our own macros and reader macros, I'll show you how to in the next tutorial.


For now, there is one question. If didn't get expanded, so it's not a macro. But it can't be a function, because it shouldn't process the false clause if the input is true and vice versa, it should short-cirquit. How does that work?
Well, there is one kind of element we can't introduce in Common Lisp and that are the special operators. If is one of the aproximately 20 of them and they're the most low-level elements of the language, on which everything is built. We can directly access all of them, but most, such as the tagbody-go construct are usually not needed but only used in the expansion of existing macros. Nothing prevents you from using them in corner cases though.
« Last Edit: January 30, 2012, 02:15:01 pm by Virex »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1047 on: January 30, 2012, 11:24:19 am »

MagmaMcFry's Perl Tutorial Part 2: Scalar Variables and I/O

In Perl, you use scalar variables by typing a $ followed by their names.

Code: [Select]
$foo = 4;           # Sets the scalar variable with the name "foo" to 4.
$bar = $foo + 3;    # Sets the scalar variable with the name "bar" to the sum of the contents of the scalar variable with the name "foo" and 3.
Perl variables don't need to be declared, you can just use them straight away. They are automatically initialized to undef (wait a bit longer).
Variable names ("identifiers")can start with a letter or an underscore, and may then continue with letters, digits and underscores, although there are specific exceptions. Perl identifiers are case-sensitive. The formal Perl regex definition for a Perl identifier looks like this:
Code: [Select]
/^(?:[a-z_]\w*|\d+|[^\w\s]|^[a-z_])$/iWait a moment until your eyes stop bleeding, and continue. Don't worry, it'll all get explained later.
The = is the assignment operator, as in so many other languages, and assigns whatever you get when
you evaluate the right-hand side to the variable(s) mentioned on the left-hand side.
The ; is a statement delimiter, separating statements.
Perl allows you to replace any whitespace in standard context with any other whitespace, like this:
Code: [Select]
$foo                  =4;$bar=$foo
           +
#42;   $baz        =
3
;
Only use this feature for good, never for evil. Did you notice the comment?

Now you say: "Wow, that's pretty boring. That's not awesome at all!".
Yes, yes. The interesting bits will come later.

For now, let's look at other assignment operators:

Code: [Select]
$x += 4;        # Increases $x by 4
$y *= 3;        # Multiplies $y with 3 and stores the result back in $y.
$p %= 4; # Replaces $p with its remainder after division by 4.
$g **= 0.5; # Replaces $g with its square root.
$foo .= $bar; # Adds $bar to $foo.
$long_text x= 1e79;     # Very bad idea.

Almost every binary operator has an assignment variant, even those you don't know yet.

Now double quotes have this really cool feature:

Code: [Select]
$x = "b";
'a $x c'   # Six characters: a, space, dollar sign, x, space, c.
"a $x c"   # The $x gets replaced by its value, so this is the same as "a b c".
"a$xc"     # Oops: This is not "abc", as you might want it to be, but the result of concatenating "a" and the value of $xc, which is undef. Result: "a".
"a${x}c"   # This is what you need.

Now I've told you about undef thrice without explaining it, so let's go.
undef is a scalar value that evaluates to "" if you need it to be a string,
and to 0 if you need it to be a number. It has another property, but that'll be explained later.

Now let's start learning how to make programs actually do something that you can notice.
To output strings to standard output (the console, if you run the program directly), just use print:
Code: [Select]
print "Hello World!\n";
Bam, there's your complete program. In other languages, you need an int main() {} or something. Perl just executes the whole file.

To get input from the user, type the following:
Code: [Select]
$input_line = <STDIN>;   # Reads a line from standard input. If you're running the program directly, it will read from console input.
                         # The program will wait until you hit newline, and then <STDIN> will
                         # return a string with everything you typed, including the newline.

Now let's write a simple program that multiplies two numbers and prints the result.

Code: [Select]
print "Enter a number: ";
$num1 = <STDIN>;
print "Enter another number: ";
$num2 = <STDIN>;
$result = $num1 * $num2;
print "The result of multiplying $num1 and $num2 is $result.\n";

Let's look at a test run of this program, if you want to know the product of 3 and 4:

Code: [Select]
Enter a number: 3
Enter another number: 4
The result of multiplying 3
 and 4
 is 12.
Whoops, what happened here? The multiplication is correct, but the formatting is all wrong! Remember what the <STDIN> does? It returns a string including the newline that you type to signal that your input is finished. This means that the contents of $num1 were actually "3\n" and that $num2 contained "4\n". When Perl interprets those as numbers because the multiplication operator needs numbers, then "3\n" will be converted to 3 and "4\n" to 4, and the result is 12. But $num1 still contains the unconverted version of your input, and that's because the formatting is all wrong.
Now there are several ways to remove the newline from the end of a string, but I'll show you only a few. The generally most used is the chomp operator. I don't like it that much. Basic usage: Use chomp on a variable to remove the trailing newline in the string contained in the variable.
Code: [Select]
$num1 = <STDIN>;
chomp $num1;

# There is a quicker way for lazy people:
chomp($num1 = <STDIN>);
Easier way: Because you intend for the program user to input numbers, you can just force what comes out of <STDIN> to be converted to a number:
Code: [Select]
$num1 = 0 + <STDIN>;
But it's your decision how to do what you want to do; it's your program after all and
There Is More Than One Way To Do It.

So, let's fix our number program:
Code: [Select]
print "Enter a number: ";
$num1 = 0 + <STDIN>;
print "Enter another number: ";
chomp($num2 = <STDIN>);
$result = $num1 * $num2;
print "The result of multiplying $num1 and $num2 is $result.\n";
I used both ways. Now if you run this program with the same input, $num1 will contain the number 3 and $num2 will contain the string "4". And the program works perfectly.

Code: [Select]
Enter a number: 3
Enter another number: 4
The result of multiplying 3 and 4 is 12.

That's all for today. Next issue: Comparison and boolean operators and conditional statements.

Stargrasper, one advanced regex tutorial coming right up.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1048 on: January 30, 2012, 11:36:14 am »

Virex, looking forward to more Lisp stuff! Always wanted to play around with Lisp beyond that one class in college.

I'll probably pick up Clojure - compiled dynamic Lisp that runs on the JVM. All the benefits of Java with the beauty of Lisp. What's not to love?
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1049 on: January 30, 2012, 11:51:33 am »

Yet... Ruby is just... so... much fun to work with. I can't help it.

Agreed.  Its ridiculous on how fast you can get things done in it too.  I tend to view Ruby as the next step after Perl, a language that is just fantastic for getting small things done. 

« Last Edit: January 30, 2012, 12:34:08 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout
Pages: 1 ... 68 69 [70] 71 72 ... 796