From 2e9f5a642bc7a90045830892af1ade48ea613a2a Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 10:53:43 -0700 Subject: [PATCH 01/27] Small edits to python version --- python.html.markdown | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/python.html.markdown b/python.html.markdown index 168f1ea1..615ee249 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -106,7 +106,8 @@ li[-1] #=> 4 # Looking out of bounds is an IndexError li[4] # Raises an IndexError -# You can look at ranges with slice syntax. It's an closed/open range for you mathy types. +# You can look at ranges with slice syntax. +# (It's a closed/open range for you mathy types.) li[1:3] #=> [2, 4] # Omit the beginning li[:3] #=> [1, 2, 4] @@ -233,7 +234,8 @@ while x < 4: # Handle exceptions with a try/except block try: - raise IndexError("This is an index error") # Use raise to raise an error + # Use raise to raise an error + raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. @@ -252,20 +254,26 @@ add(5, 6) #=> 11 and prints out "x is 5 and y is 6" # Another way to call functions is with keyword arguments add(y=6, x=5) # Keyword arguments can arrive in any order. -# You can define functions that take a variable number of positional arguments +# You can define functions that take a variable number of +# positional arguments def varargs(*args): return args varargs(1, 2, 3) #=> (1,2,3) -# You can define functions that take a variable number of keyword arguments +# You can define functions that take a variable number of +# keyword arguments, as well def keyword_args(**kwargs): return kwargs # Let's call it to see what happens keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} +# You can do both at once, if you like +def all_the_args(*args, **kwargs): + pass + # Python has first class functions def create_adder(x): From 26d6fe55e382517ed3015ee137afa3c057b4a02d Mon Sep 17 00:00:00 2001 From: Julia Evans Date: Thu, 27 Jun 2013 14:35:33 -0400 Subject: [PATCH 02/27] Fix typo 8-1 is 7 =) --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index 615ee249..eb8f5cb4 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -26,7 +26,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # Math is what you would expect 1 + 1 #=> 2 -8 - 1 #=> 9 +8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 From 0ab2ec2d737dab34f5c962b53e49f96b74b829da Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:48:31 +1200 Subject: [PATCH 03/27] Fix spelling error concatinated -> concatenated --- php.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index 18324736..4dafe6ad 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -43,8 +43,8 @@ $integer = 0123; // octal number (equivalent to 83 decimal) $integer = 0x1A; // hexadecimal number (equivalent to 26 decimal) // Floats (aka doubles) -$float = 1.234; -$float = 1.2e3; +$float = 1.234; +$float = 1.2e3; $float = 7E-10; // Arithmetic @@ -74,7 +74,7 @@ $sgl_quotes END; // Nowdoc syntax is available in PHP 5.3.0 // Manipulation -$concatinated = $sgl_quotes + $dbl_quotes; +$concatenated = $sgl_quotes + $dbl_quotes; ``` ### Compound @@ -237,7 +237,7 @@ while ($i < 5) { if ($i == 3) { break; // Exit out of the while loop and continue. } - + echo $i++; } @@ -247,7 +247,7 @@ while ($i < 5) { if ($i == 3) { continue; // Skip this iteration of the loop } - + echo $i++; } ``` @@ -338,11 +338,11 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as class MyClass { function myFunction() { } - + function function youCannotOverrideMe() { } - + public static function myStaticMethod() { } @@ -362,12 +362,12 @@ PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic ```php class MyClass { private $property; - + public function __get($key) { return $this->$key; } - + public function __set($key, $value) { $this->$key = $value; @@ -471,4 +471,4 @@ Visit the [official PHP documentation](http://www.php.net/manual/) for reference If you're interested in up-to-date best practices, visit [PHP The Right Way](http://www.phptherightway.com/). -If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/). \ No newline at end of file +If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/). From 124273ffbe9ac370b99ca7fecd24d2656552ca9a Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:48:53 +1200 Subject: [PATCH 04/27] Correct form of string concatenation. --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 4dafe6ad..ce96b457 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -74,7 +74,7 @@ $sgl_quotes END; // Nowdoc syntax is available in PHP 5.3.0 // Manipulation -$concatenated = $sgl_quotes + $dbl_quotes; +$concatenated = $sgl_quotes . $dbl_quotes; ``` ### Compound From 85eee3929a1ccb5225f2e1d826a466e7351bbf39 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:49:58 +1200 Subject: [PATCH 05/27] Mention print is a language construct --- php.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/php.html.markdown b/php.html.markdown index ce96b457..6dcf7d59 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -97,6 +97,7 @@ $associative["One"]; // Holds the value 1 echo('Hello World!'); // Prints Hello World! to stdout. Stdout is the web page if running in a browser. print('Hello World!'); // The same as echo echo 'Hello World!'; // echo is actually a language construct, so you can drop the parentheses. +print 'Hello World!'; // So is print echo 100; echo $variable; echo function_result(); // Output the result of a function call that returns a value. More on functions later. From 34852cc8be15e4ba643001dac8c0c0ca1ac48dca Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:56:52 +1200 Subject: [PATCH 06/27] Move reference operator to site next to variable, as per docs --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 6dcf7d59..57470390 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -114,7 +114,7 @@ echo function_result(); // Output the result of a function call that returns a v $a = 1; $b = 2; $a = $b; // A now contains the same value sa $b -$a =& $b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. +$a = &$b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. ``` ### Comparison From c3cc11f2a81411acf5e500285836c8f7744546e1 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:57:25 +1200 Subject: [PATCH 07/27] Add description of typecasting --- php.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/php.html.markdown b/php.html.markdown index 57470390..d08b9220 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -144,7 +144,11 @@ echo $string + $string; // Also outputs 2 because the + operator converts the st $string = 'one'; echo $string + $string; // Outputs 0 because the + operator cannot cast the string 'one' to a number +``` +Type casting can be used to treat a variable as another type temporarily by using cast operators in parentheses. + +```php $boolean = (boolean) $integer; // $boolean is true $zero = 0; From c5fca52d14fad914a65cdb66c51dfa9e33f3b8b8 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:57:52 +1200 Subject: [PATCH 08/27] Make if statement result clearer --- php.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index d08b9220..a9604df3 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -190,9 +190,9 @@ if (/* test */) { } - +This is displayed if the test is truthy. - +This is displayed otherwise. ``` From 91957c7cc2b1b0ecf131071ca7c7a2808d241f34 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:03:08 +1200 Subject: [PATCH 09/27] Make comparisons clearer when type juggling --- php.html.markdown | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index a9604df3..8f319930 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -111,24 +111,29 @@ echo function_result(); // Output the result of a function call that returns a v ### Assignment ```php -$a = 1; -$b = 2; -$a = $b; // A now contains the same value sa $b -$a = &$b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. +$x = 1; +$y = 2; +$x = $y; // A now contains the same value sa $y +$x = &$y; // A now contains a reference to $y. Changing the value of $x will change the value of $y also, and vice-versa. ``` ### Comparison ```php +// These comparisons will always be true, even if the types aren't the same. $a == $b // TRUE if $a is equal to $b after type juggling. -$a === $b // TRUE if $a is equal to $b, and they are of the same type. $a != $b // TRUE if $a is not equal to $b after type juggling. $a <> $b // TRUE if $a is not equal to $b after type juggling. -$a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. $a < $b // TRUE if $a is strictly less than $b. $a > $b // TRUE if $a is strictly greater than $b. $a <= $b // TRUE if $a is less than or equal to $b. $a >= $b // TRUE if $a is greater than or equal to $b. + +// The following will only be true the values match and they are the same type. +$a === $b // TRUE if $a is equal to $b, and they are of the same type. +$a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. +1 == '1' // TRUE +1 === '1' // FALSE ``` ## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php) From be46dc081a255d85a1b1331b4fcd949196699b7c Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:11:27 +1200 Subject: [PATCH 10/27] Add example of currying --- php.html.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/php.html.markdown b/php.html.markdown index 8f319930..ae636b0a 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -300,6 +300,23 @@ function outer_function ($arg_1 = null) { // $arg_1 is optional // inner_function() does not exist and cannot be called until outer_function() is called ``` +This enables [currying](http://en.wikipedia.org/wiki/Currying) in PHP. + +```php +function foo ($x, $y, $z) { + echo "$x - $y - $z"; +} + +function bar ($x, $y) { + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; +} + +$bar = bar('A', 'B'); +$bar('C'); +``` + ### [Variable](http://www.php.net/manual/en/functions.variable-functions.php) ```php From e051d0ec65dfbf7b97324e6b4e4812afd5a3dd03 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:15:50 +1200 Subject: [PATCH 11/27] Clarify anonymous functions as callbacks as arguments --- php.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index ae636b0a..af4e2f01 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -330,7 +330,11 @@ $function_name(); // will execute the my_function_name() function Similar to variable functions, functions may be anonymous. ```php -my_function(function () { +function my_function($callback) { + $callback('My argument'); +} + +my_function(function ($my_argument) { // do something }); From c8800bff13262b1d37b4f926a35410509ef8bbc9 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:17:37 +1200 Subject: [PATCH 12/27] Replace tabs with spaces --- php.html.markdown | 76 +++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index af4e2f01..ec9c77e7 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -124,7 +124,7 @@ $x = &$y; // A now contains a reference to $y. Changing the value of $x will cha $a == $b // TRUE if $a is equal to $b after type juggling. $a != $b // TRUE if $a is not equal to $b after type juggling. $a <> $b // TRUE if $a is not equal to $b after type juggling. -$a < $b // TRUE if $a is strictly less than $b. +$a < $b // TRUE if $a is strictly less than $b. $a > $b // TRUE if $a is strictly greater than $b. $a <= $b // TRUE if $a is less than or equal to $b. $a >= $b // TRUE if $a is greater than or equal to $b. @@ -171,27 +171,27 @@ $var = null; // Null value ```php if (/* test */) { - // Do something + // Do something } if (/* test */) { - // Do something + // Do something } else { - // Do something else + // Do something else } if (/* test */) { - // Do something + // Do something } elseif(/* test2 */) { - // Do something else, only if test2 + // Do something else, only if test2 } if (/* test */) { - // Do something + // Do something } elseif(/* test2 */) { - // Do something else, only if test2 + // Do something else, only if test2 } else { - // Do something default + // Do something default } @@ -205,15 +205,15 @@ This is displayed otherwise. ```php switch ($variable) { - case 'one': - // Do something if $variable == 'one' + case 'one': + // Do something if $variable == 'one' break; case 'two': case 'three': - // Do something if $variable is either 'two' or 'three' + // Do something if $variable is either 'two' or 'three' break; default: - // Do something by default + // Do something by default } ``` @@ -223,42 +223,42 @@ switch ($variable) { ```php $i = 0; while ($i < 5) { - echo $i++; + echo $i++; } $i = 0; do { - echo $i++; + echo $i++; } while ($i < 5); for ($x = 0; $x < 10; $x++) { - echo $x; // Will echo 0 - 9 + echo $x; // Will echo 0 - 9 } $wheels = ["bicycle" => 2, "car" => 4]; foreach ($wheels as $vehicle => $wheel_count) { - echo "A $vehicle has $wheel_count wheels"; + echo "A $vehicle has $wheel_count wheels"; } // This loop will stop after outputting 2 $i = 0; while ($i < 5) { if ($i == 3) { - break; // Exit out of the while loop and continue. + break; // Exit out of the while loop and continue. } - echo $i++; + echo $i++; } // This loop will output everything except 3 $i = 0; while ($i < 5) { - if ($i == 3) { - continue; // Skip this iteration of the loop + if ($i == 3) { + continue; // Skip this iteration of the loop } - echo $i++; + echo $i++; } ``` @@ -268,7 +268,7 @@ Functions are created with the ```function``` keyword. ```php function my_function($my_arg) { - $my_variable = 1; + $my_variable = 1; } // $my_variable and $my_arg cannot be accessed outside of the function @@ -288,12 +288,12 @@ A valid function name starts with a letter or underscore, followed by any number ```php function my_function_name ($arg_1, $arg_2) { // $arg_1 and $arg_2 are required - // Do something with $arg_1 and $arg_2; + // Do something with $arg_1 and $arg_2; } // Functions may be nested to limit scope function outer_function ($arg_1 = null) { // $arg_1 is optional - function inner_function($arg_2 = 'two') { // $arg_2 will default to 'two' + function inner_function($arg_2 = 'two') { // $arg_2 will default to 'two' } } @@ -335,12 +335,12 @@ function my_function($callback) { } my_function(function ($my_argument) { - // do something + // do something }); // Closure style $my_function = function() { - // Do something + // Do something }; $my_function(); @@ -352,9 +352,9 @@ Classes are defined with the ```class``` keyword. ```php class MyClass { - const MY_CONST = 'value'; + const MY_CONST = 'value'; static $staticVar = 'something'; - public $property = 'value'; // Properties must declare their visibility + public $property = 'value'; // Properties must declare their visibility } echo MyClass::MY_CONST; // Outputs "value"; @@ -367,7 +367,7 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as ```php class MyClass { - function myFunction() { + function myFunction() { } function function youCannotOverrideMe() @@ -392,16 +392,16 @@ PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic ```php class MyClass { - private $property; + private $property; public function __get($key) { - return $this->$key; + return $this->$key; } public function __set($key, $value) { - $this->$key = $value; + $this->$key = $value; } } @@ -415,12 +415,12 @@ Classes can be abstract (using the ```abstract``` keyword), extend other classes ```php interface InterfaceOne { - public function doSomething(); + public function doSomething(); } interface InterfaceTwo { - public function doSomething(); + public function doSomething(); } abstract class MyAbstractClass implements InterfaceOne @@ -481,15 +481,15 @@ Traits are available since PHP 5.4.0 and are declared using the ```trait``` keyw ```php trait MyTrait { - public function myTraitMethod() + public function myTraitMethod() { - // Do something + // Do something } } class MyClass { - use MyTrait; + use MyTrait; } $cls = new MyClass(); From 23e822bcc1c3d0c2fdee2eed474c9debc36818b1 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:19:53 +1200 Subject: [PATCH 13/27] Correct syntax error declaring final method --- php.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index ec9c77e7..491255e9 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -367,10 +367,11 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as ```php class MyClass { - function myFunction() { + function myFunction() + { } - function function youCannotOverrideMe() + final function youCannotOverrideMe() { } From caae7d9696b62ccb2ba263a43e3a94d494589584 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:21:09 +1200 Subject: [PATCH 14/27] Use sinlge line breaks for clarity --- php.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 491255e9..c6c70cbb 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -411,7 +411,8 @@ echo $x->property; // Will use the __get() method to retrieve the value of $prop $x->property = 'Something'; // Will use the __set() method to set the value of property ``` -Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword. +Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and +implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword. ```php interface InterfaceOne From aa18e7301c66eec70c40c11ed043205c78bbf3ab Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:13:02 -0700 Subject: [PATCH 15/27] edits --- php.html.markdown | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index 33bf2859..753f6ab1 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -159,7 +159,7 @@ $a > $b // TRUE if $a is strictly greater than $b. $a <= $b // TRUE if $a is less than or equal to $b. $a >= $b // TRUE if $a is greater than or equal to $b. -// The following will only be true the values match and they are the same type. +// The following will only be true if the values match and are the same type. $a === $b // TRUE if $a is equal to $b, and they are of the same type. $a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. 1 == '1' // TRUE @@ -334,8 +334,8 @@ number of letters, numbers, or underscores. There are three ways to declare func ```php Date: Thu, 27 Jun 2013 18:18:05 -0700 Subject: [PATCH 16/27] Minor fixes to python doc --- python.html.markdown | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index eb8f5cb4..bf3739ba 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -233,12 +233,20 @@ while x < 4: x += 1 # Shorthand for x = x + 1 # Handle exceptions with a try/except block + +# Works on Python 2.6 and up: try: # Use raise to raise an error raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. +# Works for Python 2.7 and down: +try: + raise IndexError("This is an index error") +except IndexError, e: # No "as", comma instead + pass + #################################################### ## 4. Functions @@ -272,7 +280,13 @@ keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} # You can do both at once, if you like def all_the_args(*args, **kwargs): - pass + print args + print kwargs +""" +all_the_args(1, 2, a=3, b=4) prints: + [1, 2] + {"a": 3, "b": 4} +""" # Python has first class functions From 1fed90cba669b64af4e39e53d167b3c5e94ea1af Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:22:30 -0700 Subject: [PATCH 17/27] Fixed any syntax errors --- python.html.markdown | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/python.html.markdown b/python.html.markdown index bf3739ba..07a83042 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -81,7 +81,10 @@ some_var = 5 # Convention is to use lower_case_with_underscores some_var #=> 5 # Accessing a previously unassigned variable is an exception -some_other_var # Will raise a NameError +try: + some_other_var +except NameError: + print "Raises a name error" # Lists store sequences @@ -103,8 +106,12 @@ li.append(3) # li is now [1, 2, 4, 3] again. li[0] #=> 1 # Look at the last element li[-1] #=> 4 + # Looking out of bounds is an IndexError -li[4] # Raises an IndexError +try: + li[4] # Raises an IndexError +except IndexError: + print "Raises an IndexError" # You can look at ranges with slice syntax. # (It's a closed/open range for you mathy types.) @@ -132,7 +139,10 @@ len(li) #=> 6 # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 -tup[0] = 3 # Raises a TypeError +try: + tup[0] = 3 # Raises a TypeError +except TypeError: + print "Tuples cannot be mutated." # You can do all those list thingies on tuples too len(tup) #=> 3 @@ -295,7 +305,7 @@ def create_adder(x): return x + y return adder -add_10 = create_adder(10): +add_10 = create_adder(10) add_10(3) #=> 13 # There are also anonymous functions @@ -357,3 +367,5 @@ j.get_species() #=> "H. neanderthalensis" # Call the static method Human.grunt() #=> "*grunt*" +``` + From e37b1745a7ef0cfcb4328d8264b3673bc445746f Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:27:14 -0700 Subject: [PATCH 18/27] Fixed a typo --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index 07a83042..42801657 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -154,7 +154,7 @@ tup[:2] #=> (1, 2) a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3 # Tuples are created by default if you leave out the parentheses d, e, f = 4, 5, 6 -# Now look how easy it is to swap to values +# Now look how easy it is to swap two values e, d = d, e # d is now 5 and e is now 4 From ff14723ee6a4486a9e567f56dfb2eb7c69df582a Mon Sep 17 00:00:00 2001 From: "js.sevestre" Date: Fri, 28 Jun 2013 09:52:39 +0200 Subject: [PATCH 19/27] Add inequality and chained comparisons --- python.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index 42801657..300a5519 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -49,11 +49,24 @@ False not True #=> False not False #=> True - # Equality is == 1 == 1 #=> True 2 == 1 #=> False +# Inequality is != +1 != 1 #=> False +2 != 1 #=> True + +# More comparisons +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +# Comparisons can be chained ! +1 < 2 < 3 #=> True +2 < 3 < 2 #=> False + # Strings are created with " or ' "This is a string." 'This is also a string.' From e6010063819b812085b62abc37badc2eff5907d7 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 28 Jun 2013 01:31:09 -0700 Subject: [PATCH 20/27] Took a run at C --- c.html.markdown | 336 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 c.html.markdown diff --git a/c.html.markdown b/c.html.markdown new file mode 100644 index 00000000..a8df2e1b --- /dev/null +++ b/c.html.markdown @@ -0,0 +1,336 @@ +--- +language: c +author: Adam Bard +author_url: http://adambard.com/ +--- + +Ah, C. Still the language of modern high-performance computing. + +C is the lowest-level language most programmers will ever use, but +it more than makes up for it with raw speed. Just be aware of its manual +memory management and C will take you as far as you need to go. + +```c +// Single-line comments start with // +/* +Multi-line comments look like this. +*/ + +// Import headers with #include +#include +#include + +// Declare function signatures in advance in a .h file, or at the top of +// your .c file. +void function_1(); +void function_2(); + +// Your program's entry point is a function called +// main with an integer return type. +int main(){ + +// print output using printf, for "print formatted" +// %d is an integer, \n is a newline +printf("%d\n", 0); // => Prints 0 +// All statements must end with a semicolon + +/////////////////////////////////////// +// Types +/////////////////////////////////////// + +// Variables must always be declared with a type. + +// 32-bit integer +int x_int = 0; + +// 16-bit integer +short x_short = 0; + +// 8-bit integer, aka 1 byte +char x_char = 0; +char y_char = 'y'; // Char literals are quoted with '' + +long x_long = 0; // Still 32 bytes for historical reasons +long long x_long_long = 0; // Guaranteed to be at least 64 bytes + +// 32-bit floating-point decimal +float x_float = 0.0; + +// 64-bit floating-point decimal +double x_double = 0.0; + +// Integer types may be unsigned +unsigned char ux_char; +unsigned short ux_short; +unsigned int ux_int; +unsigned long long ux_long_long; + +// Arrays must be initialized with a concrete size. +char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes +int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes + + +// You can initialize an array to 0 thusly: +char my_array[20] = {0}; + +// Indexing an array is like other languages -- or, +// rather, other languages are like C +my_array[0]; // => 0 + +// Arrays are mutable; it's just memory! +my_array[1] = 2; +printf("%d\n", my_array[1]); // => 2 + +// Strings are just lists of chars terminated by a null (0x00) byte. +char a_string[20] = "This is a string"; + +/* +You may have noticed that a_string is only 16 chars long. +Char #17 is a null byte, 0x00 aka \0. +Chars #18, 19 and 20 have undefined values. +*/ + +printf("%d\n", a_string[16]); + +/////////////////////////////////////// +// Operators +/////////////////////////////////////// + +int i1 = 1, i2 = 2; // Shorthand for multiple declaration +float f1 = 1.0, f2 = 2.0; + +// Arithmetic is straightforward +i1 + i2; // => 3 +i2 - i1; // => 1 +i2 * i1; // => 2 +i1 / i2; // => 0 (0.5, but truncated towards 0) + +f1 / f2; // => 0.5, plus or minus epsilon + +// Modulo is there as well +11 % 3; // => 2 + +// Comparison operators are probably familiar, but +// there is no boolean type in c. We use ints instead. +// 0 is false, anything else is true +3 == 2; // => 0 (false) +3 != 2; // => 1 (true) +3 > 2; // => 1 +3 < 2; // => 0 +2 <= 2; // => 1 +2 >= 2; // => 1 + +// Logic works on ints +!3; // => 0 (Logical not) +!0; // => 1 +1 && 1; // => 1 (Logical and) +0 && 1; // => 0 +0 || 1; // => 1 (Logical or) +0 || 0; // => 0 + +// Bitwise operators! +~0x0F; // => 0xF0 (bitwise negation) +0x0F & 0xF0; // => 0x00 (bitwise AND) +0x0F | 0xF0; // => 0xFF (bitwise OR) +0x04 | 0x0F; // => 0x0A (bitwise XOR) +0x01 << 1; // => 0x02 (bitwise left shift (by 1)) +0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) + +/////////////////////////////////////// +// Control Structures +/////////////////////////////////////// + +if(0){ + printf("I am never run\n"); +}else if(0){ + printf("I am also never run\n"); +}else{ + printf("I print\n"); +} + +// While loops exist +int ii = 0; +while(ii < 10){ + printf("%d, ", ii++); // ii++ increments ii in-place, after using its value. +} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +int kk = 0; +do{ + printf("%d, ", kk); +}while(++kk < 10); // ++kk increments kk in-place, before using its value +// => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +// For loops too +int jj; +for(jj=0; jj < 10; jj++){ + printf("%d, ", jj); +} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +/////////////////////////////////////// +// Typecasting +/////////////////////////////////////// + +// Everything in C is stored somewhere in memory. You can change +// the type of a variable to choose how to read its data + +int x_hex = 0x01; // You can assign vars with hex literals + +// Casting between types will attempt to preserve their numeric values +printf("%d\n", x_hex); // => Prints 1 +printf("%d\n", (short) x_hex); // => Prints 1 +printf("%d\n", (char) x_hex); // => Prints 1 + +// Types will overflow without warning +printf("%d\n", (char) 257); // => 1 (Max char = 255) +printf("%d\n", (short) 65537); // => 1 (Max short = 65535) + +/////////////////////////////////////// +// Pointers +/////////////////////////////////////// + +// You can retrieve the memory address of your variables, +// then mess with them. + +int x = 0; +printf("%p\n", &x); // Use & to retrive the address of a variable +// (%p formats a pointer) +// => Prints some address in memory; + +int x_array[20]; // Arrays are a good way to allocate a contiguous block of memory +int xx; +for(xx=0; xx<20; xx++){ + x_array[xx] = 20 - xx; +} // Initialize x_array to 20, 19, 18,... 2, 1 + +// Pointer types end with * +int* x_ptr = x_array; +// This works because arrays are pointers to their first element. + +// Put a * in front to de-reference a pointer and retrieve the value, +// of the same type as the pointer, that the pointer is pointing at. +printf("%d\n", *(x_ptr)); // => Prints 20 +printf("%d\n", x_array[0]); // => Prints 20 + +// Pointers are incremented and decremented based on their type +printf("%d\n", *(x_ptr + 1)); // => Prints 19 +printf("%d\n", x_array[1]); // => Prints 19 + +// Array indexes are such a thin wrapper around pointer +// arithmatic that the following works: +printf("%d\n", 0[x_array]); // => Prints 20; +printf("%d\n", 2[x_array]); // => Prints 18; + +// The above is equivalent to: +printf("%d\n", *(0 + x_ptr)); +printf("%d\n", *(2 + x_ptr)); + +// You can give a pointer a block of memory to use with malloc +int* my_ptr = (int*) malloc(sizeof(int) * 20); +for(xx=0; xx<20; xx++){ + *(my_ptr + xx) = 20 - xx; +} // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) + +// Dereferencing memory that you haven't allocated gives +// unpredictable results +printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? + +// When you're done with a malloc'd block, you need to free it +free(my_ptr); + +// Strings can be char arrays, but are usually represented as char +// pointers: +char* my_str = "This is my very own string"; + +printf("%d\n", *my_str); // 84 (The ascii value of 'T') + +function_1(); +} // end main function + +/////////////////////////////////////// +// Functions +/////////////////////////////////////// + +// Function declaration syntax: +// () + +int add_two_ints(int x1, int x2){ + return x1 + x2; // Use return a return a value +} + +/* +Pointers are passed-by-reference (duh), so functions +can mutate their values. + +Example: in-place string reversal +*/ + +// A void function returns no value +void str_reverse(char* str_in){ + char tmp; + int ii=0, len = strlen(str_in); // Strlen is part of the c standard library + for(ii=0; ii ".tset a si sihT" +*/ + +/////////////////////////////////////// +// User-defined types and structs +/////////////////////////////////////// + +// Typedefs can be used to create type aliases +typedef int my_type; +my_type my_type_var = 0; + +// Structs are just collections of data +struct rectangle { + int width; + int height; +}; + + +void function_1(){ + + struct rectangle my_rec; + + // Access struct members with . + my_rec.width = 10; + my_rec.height = 20; + + // You can declare pointers to structs + struct rectangle* my_rec_ptr = &my_rec; + + // Use dereferencing to set struct pointer members... + (*my_rec_ptr).width = 30; + + // ... or use the -> shorthand + my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10; +} + +// You can apply a typedef to a struct for convenience +typedef struct rectangle rect; + +int area(rect r){ + return r.width * r.height; +} + +``` + +## Further Reading + +Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) + +Other than that, Google is your friend. From 6634622d54c5a26dca9d1963974ec735261c13cd Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 28 Jun 2013 02:01:53 -0700 Subject: [PATCH 21/27] Fixed c thing --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c.html.markdown b/c.html.markdown index a8df2e1b..4ac05299 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -132,7 +132,7 @@ f1 / f2; // => 0.5, plus or minus epsilon ~0x0F; // => 0xF0 (bitwise negation) 0x0F & 0xF0; // => 0x00 (bitwise AND) 0x0F | 0xF0; // => 0xFF (bitwise OR) -0x04 | 0x0F; // => 0x0A (bitwise XOR) +0x04 ^ 0x0F; // => 0x0B (bitwise XOR) 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) 0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) From 9e17e878854531c75205147f30842850a75a5a20 Mon Sep 17 00:00:00 2001 From: Liteye Date: Fri, 28 Jun 2013 22:59:25 +0800 Subject: [PATCH 22/27] add conditional expressions, get and setdefault for dictionary and a little tip about fuction calling. Signed-off-by: Liteye --- python.html.markdown | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python.html.markdown b/python.html.markdown index 300a5519..982333ca 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -99,6 +99,10 @@ try: except NameError: print "Raises a name error" +# Conditional Expressions can be used when assigning +some_var = a if a > b else b +# If a is greater than b, then a is assigned to some_var. +# Otherwise b is assigned to some_var. # Lists store sequences li = [] @@ -192,6 +196,17 @@ filled_dict.values() #=> [3, 2, 1] "one" in filled_dict #=> True 1 in filled_dict #=> False +# Trying to look up a non-existing key will raise a KeyError +filled_dict["four"] #=> KeyError + +# Use get method to avoid the KeyError +filled_dict.get("one", 4) #=> 1 +filled_dict.get("four", 4) #=> 4 + +# Setdefault method is a safe way to add new key-value pair into dictionary +filled_dict.setdefault("five", 5) #filled_dict["five"] is set to 5 +filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5 + # Sets store ... well sets empty_set = set() @@ -311,6 +326,12 @@ all_the_args(1, 2, a=3, b=4) prints: {"a": 3, "b": 4} """ +# You can also use * and ** when calling a function +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +foo(*args) # equivalent to foo(1, 2, 3, 4) +foo(**kwargs) # equivalent to foo(a=3, b=4) +foo(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) # Python has first class functions def create_adder(x): From 2e805c8c020719da0aab40987b7c064c864db9c8 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 28 Jun 2013 12:59:45 -0700 Subject: [PATCH 23/27] Updates --- c.html.markdown | 2 ++ python.html.markdown | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/c.html.markdown b/c.html.markdown index 4ac05299..73b60f75 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -333,4 +333,6 @@ int area(rect r){ Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) +Another good resource is [Learn C the hard way](http://c.learncodethehardway.org/book/) + Other than that, Google is your friend. diff --git a/python.html.markdown b/python.html.markdown index 982333ca..39a2349d 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -395,7 +395,7 @@ print j.say("hello") #prints out "Joel: hello" i.get_species() #=> "H. sapiens" # Change the shared attribute -i.species = "H. neanderthalensis" +Human.species = "H. neanderthalensis" i.get_species() #=> "H. neanderthalensis" j.get_species() #=> "H. neanderthalensis" From f7352567f4f76a26ebfe09365faa8bc790463dd3 Mon Sep 17 00:00:00 2001 From: mrshankly Date: Fri, 28 Jun 2013 21:29:03 +0100 Subject: [PATCH 24/27] Corrected the last element of li in the lists explanation. --- python.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python.html.markdown b/python.html.markdown index 39a2349d..0d9a3ed6 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -122,7 +122,7 @@ li.append(3) # li is now [1, 2, 4, 3] again. # Access a list like you would any array li[0] #=> 1 # Look at the last element -li[-1] #=> 4 +li[-1] #=> 3 # Looking out of bounds is an IndexError try: @@ -145,7 +145,7 @@ del li[2] # li is now [1, 2, 3] li + other_li #=> [1, 2, 3, 4, 5, 6] - Note: li and other_li is left alone # Concatenate lists with extend -li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] +li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Check for existence in a list with in 1 in li #=> True @@ -255,14 +255,14 @@ prints: """ for animal in ["dog", "cat", "mouse"]: # You can use % to interpolate formatted strings - print "%s is a mammal" % animal + print "%s is a mammal" % animal """ While loops go until a condition is no longer met. prints: 0 1 - 2 + 2 3 """ x = 0 From fe5f72d868313987cb3d6a55de707876046d912d Mon Sep 17 00:00:00 2001 From: Kyle Partridge Date: Fri, 28 Jun 2013 13:55:16 -0700 Subject: [PATCH 25/27] added some clarification on the default value for dictionary.get --- python.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python.html.markdown b/python.html.markdown index 39a2349d..a3ced659 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -200,6 +200,10 @@ filled_dict.values() #=> [3, 2, 1] filled_dict["four"] #=> KeyError # Use get method to avoid the KeyError +filled_dict.get("one") #=> 1 +filled_dict.get("four") #=> None + +# The get method supports a default argument when the value is missing filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 From 8167fcd1876f5497838aa814f36daaeeb569f7a2 Mon Sep 17 00:00:00 2001 From: Miguel Lloreda Date: Fri, 28 Jun 2013 17:15:49 -0400 Subject: [PATCH 26/27] Fixed misspellings --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 73b60f75..15bfa05e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -198,7 +198,7 @@ printf("%d\n", (short) 65537); // => 1 (Max short = 65535) // then mess with them. int x = 0; -printf("%p\n", &x); // Use & to retrive the address of a variable +printf("%p\n", &x); // Use & to retrieve the address of a variable // (%p formats a pointer) // => Prints some address in memory; @@ -222,7 +222,7 @@ printf("%d\n", *(x_ptr + 1)); // => Prints 19 printf("%d\n", x_array[1]); // => Prints 19 // Array indexes are such a thin wrapper around pointer -// arithmatic that the following works: +// arithmetic that the following works: printf("%d\n", 0[x_array]); // => Prints 20; printf("%d\n", 2[x_array]); // => Prints 18; From 89d8d8a84e88cb4b0e147cd2531f2d0ced282f3f Mon Sep 17 00:00:00 2001 From: Daniel Velkov Date: Fri, 28 Jun 2013 15:56:52 -0700 Subject: [PATCH 27/27] Added clojure dependency version (take 4 (range)) doesn't work in versions < 1.2. Added a comment pointing that out. --- clojure.html.markdown | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clojure.html.markdown b/clojure.html.markdown index 5086d2c2..24250a87 100644 --- a/clojure.html.markdown +++ b/clojure.html.markdown @@ -12,6 +12,9 @@ state as it comes up. This combination allows it to handle concurrent processing very simply, and often automatically. +(You need a version of Clojure 1.2 or newer) + + ```clojure ; Comments start with semicolons.