Skip to content

Commit

Permalink
Merge 'master' of github.com:adambard/learnxinyminutes-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
evuez committed Oct 16, 2015
2 parents 375f0c1 + 25e49b8 commit 85adff2
Show file tree
Hide file tree
Showing 19 changed files with 606 additions and 234 deletions.
13 changes: 11 additions & 2 deletions bash.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,26 @@ else
echo "Your name is your username"
fi

# NOTE: if $Name is empty, bash sees the above condition as:
if [ -ne $USER ]
# which is invalid syntax
# so the "safe" way to use potentially empty variables in bash is:
if [ "$Name" -ne $USER ] ...
# which, when $Name is empty, is seen by bash as:
if [ "" -ne $USER ] ...
# which works as expected

# There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# To use && and || with if statements, you need multiple pairs of square brackets:
if [ $Name == "Steve" ] && [ $Age -eq 15 ]
if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ]
then
echo "This will run if $Name is Steve AND $Age is 15."
fi

if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]
then
echo "This will run if $Name is Daniya OR Zach."
fi
Expand Down
61 changes: 58 additions & 3 deletions c.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ contributors:
- ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"]
- ["Jakub Trzebiatowski", "http://cbs.stgn.pl"]
- ["Marco Scannadinari", "https://marcoms.github.io"]
- ["Zachary Ferguson", "https://github.io/zfergus2"]
- ["himanshu", "https://github.com/himanshu81494"]

---

Ah, C. Still **the** language of modern high-performance computing.
Expand Down Expand Up @@ -54,6 +54,8 @@ int function_2(void);
// Must declare a 'function prototype' before main() when functions occur after
// your main() function.
int add_two_ints(int x1, int x2); // function prototype
// although `int add_two_ints(int, int);` is also valid (no need to name the args),
// it is recommended to name arguments in the prototype as well for easier inspection

// Your program's entry point is a function called
// main with an integer return type.
Expand All @@ -74,6 +76,9 @@ int main (int argc, char** argv)
///////////////////////////////////////
// Types
///////////////////////////////////////

// All variables MUST be declared at the top of the current block scope
// we declare them dynamically along the code for the sake of the tutorial

// ints are usually 4 bytes
int x_int = 0;
Expand Down Expand Up @@ -232,7 +237,7 @@ int main (int argc, char** argv)
0 || 1; // => 1 (Logical or)
0 || 0; // => 0

// Conditional expression ( ? : )
// Conditional ternary expression ( ? : )
int e = 5;
int f = 10;
int z;
Expand Down Expand Up @@ -302,6 +307,8 @@ int main (int argc, char** argv)
for (i = 0; i <= 5; i++) {
; // use semicolon to act as the body (null statement)
}
// Or
for (i = 0; i <= 5; i++);

// branching with multiple choices: switch()
switch (a) {
Expand Down Expand Up @@ -678,8 +685,56 @@ typedef void (*my_fnp_type)(char *);
// , | left to right //
//---------------------------------------------------//

```
/******************************* Header Files **********************************

Header files are an important part of c as they allow for the connection of c
source files and can simplify code and definitions by seperating them into
seperate files.

Header files are syntaxtically similar to c source files but reside in ".h"
files. They can be included in your c source file by using the precompiler
command #include "example.h", given that example.h exists in the same directory
as the c file.
*/

/* A safe guard to prevent the header from being defined too many times. This */
/* happens in the case of circle dependency, the contents of the header is */
/* already defined. */
#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */
#define EXAMPLE_H /* Define the macro EXAMPLE_H. */

/* Other headers can be included in headers and therefore transitively */
/* included into files that include this header. */
#include <string.h>

/* Like c source files macros can be defined in headers and used in files */
/* that include this header file. */
#define EXAMPLE_NAME "Dennis Ritchie"
/* Function macros can also be defined. */
#define ADD(a, b) (a + b)

/* Structs and typedefs can be used for consistency between files. */
typedef struct node
{
int val;
struct node *next;
} Node;

/* So can enumerations. */
enum traffic_light_state {GREEN, YELLOW, RED};

/* Function prototypes can also be defined here for use in multiple files, */
/* but it is bad practice to define the function in the header. Definitions */
/* should instead be put in a c file. */
Node createLinkedList(int *vals, int len);

/* Beyond the above elements, other definitions should be left to a c source */
/* file. Excessive includeds or definitions should, also not be contained in */
/* a header file but instead put into separate headers or a c file. */

#endif /* End of the if precompiler directive. */

```
## 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)
Expand Down
6 changes: 3 additions & 3 deletions cs-cz/python3.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Poznámka: Tento článek je zaměřen na Python 3. Zde se můžete [naučit sta
-5 // 3 # => -2
-5.0 // 3.0 # => -2.0

# Pokud použiteje desetinné číslo, výsledek je jím také
# Pokud použijete desetinné číslo, výsledek je jím také
3 * 2.0 # => 6.0

# Modulo
Expand Down Expand Up @@ -420,7 +420,7 @@ next(iterator) # Vyhodí StopIteration
## 4. Funkce
####################################################

# Pro vytvoření nové funkce použijte def
# Pro vytvoření nové funkce použijte klíčové slovo def
def secist(x, y):
print("x je {} a y je {}".format(x, y))
return x + y # Hodnoty se vrací pomocí return
Expand Down Expand Up @@ -520,7 +520,7 @@ class Clovek(object):
# podtržítka na začátku a na konci značí, že se jedná o atribut nebo
# objekt využívaný Pythonem ke speciálním účelům, ale můžete sami
# definovat jeho chování. Metody jako __init__, __str__, __repr__
# a další se nazývají "magické metody". Nikdy nepoužívejte toto
# a další se nazývají "magické metody". Nikdy nepoužívejte toto
# speciální pojmenování pro běžné metody.
def __init__(self, jmeno):
# Přiřazení parametru do atributu instance jmeno
Expand Down
8 changes: 4 additions & 4 deletions csharp.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ on a new line! ""Wow!"", the masses cried";
// List<datatype> <var name> = new List<datatype>();
List<int> intList = new List<int>();
List<string> stringList = new List<string>();
List<int> z = new List<int> { 9000, 1000, 1337 }; // intialize
List<int> z = new List<int> { 9000, 1000, 1337 }; // initialize
// The <> are for generics - Check out the cool stuff section
// Lists don't default to a value;
Expand Down Expand Up @@ -460,7 +460,7 @@ on a new line! ""Wow!"", the masses cried";
{
// OPTIONAL PARAMETERS
MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
MethodSignatures(3, another: 3); // explicity set a parameter, skipping optional ones
MethodSignatures(3, another: 3); // explicitly set a parameter, skipping optional ones
// BY REF AND OUT PARAMETERS
int maxCount = 0, count; // ref params must have value
Expand All @@ -481,7 +481,7 @@ on a new line! ""Wow!"", the masses cried";
// in case variable is null
int notNullable = nullable ?? 0; // 0
// ?. is an operator for null-propogation - a shorthand way of checking for null
// ?. is an operator for null-propagation - a shorthand way of checking for null
nullable?.Print(); // Use the Print() extension method if nullable isn't null
// IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is:
Expand Down Expand Up @@ -650,7 +650,7 @@ on a new line! ""Wow!"", the masses cried";
{
return _cadence;
}
set // set - define a method to set a proprety
set // set - define a method to set a property
{
_cadence = value; // Value is the value passed in to the setter
}
Expand Down
14 changes: 14 additions & 0 deletions css.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ selected:link { }
/* or an element in focus */
selected:focus { }

/* any element that is the first child of its parent */
selector:first-child {}

/* any element that is the last child of its parent */
selector:last-child {}

/* Just like pseudo classes, pseudo elements allow you to style certain parts of a document */

/* matches a virtual first child of the selected element */
selector::before {}

/* matches a virtual last child of the selected element */
selector::after {}

/* At appropriate places, an asterisk may be used as a wildcard to select every
element */
* { } /* all elements */
Expand Down
67 changes: 39 additions & 28 deletions d.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ about [D](http://dlang.org/). The D programming language is a modern, general-pu
multi-paradigm language with support for everything from low-level features to
expressive high-level abstractions.
D is actively developed by Walter Bright and Andrei Alexandrescu, two super smart, really cool
dudes. With all that out of the way, let's look at some examples!
D is actively developed by a large group of super-smart people and is spearheaded by
[Walter Bright](https://en.wikipedia.org/wiki/Walter_Bright) and
[Andrei Alexandrescu](https://en.wikipedia.org/wiki/Andrei_Alexandrescu).
With all that out of the way, let's look at some examples!
```c
import std.stdio;
Expand All @@ -36,9 +38,10 @@ void main() {
writeln(i);
}
auto n = 1; // use auto for type inferred variables
// 'auto' can be used for inferring types.
auto n = 1;
// Numeric literals can use _ as a digit seperator for clarity
// Numeric literals can use '_' as a digit separator for clarity.
while(n < 10_000) {
n += n;
}
Expand All @@ -47,13 +50,15 @@ void main() {
n -= (n / 2);
} while(n > 0);
// For and while are nice, but in D-land we prefer foreach
// The .. creates a continuous range, excluding the end
// For and while are nice, but in D-land we prefer 'foreach' loops.
// The '..' creates a continuous range, including the first value
// but excluding the last.
foreach(i; 1..1_000_000) {
if(n % 2 == 0)
writeln(i);
}
// There's also 'foreach_reverse' when you want to loop backwards.
foreach_reverse(i; 1..int.max) {
if(n % 2 == 1) {
writeln(i);
Expand All @@ -69,16 +74,18 @@ are passed to functions by value (i.e. copied) and classes are passed by referen
we can use templates to parameterize all of these on both types and values!

```c
// Here, T is a type parameter. Think <T> from C++/C#/Java
// Here, 'T' is a type parameter. Think '<T>' from C++/C#/Java.
struct LinkedList(T) {
T data = null;
LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think <T>

// Use '!' to instantiate a parameterized type. Again, think '<T>'.
LinkedList!(T)* next;
}

class BinTree(T) {
T data = null;

// If there is only one template parameter, we can omit parens
// If there is only one template parameter, we can omit the parentheses.
BinTree!T left;
BinTree!T right;
}
Expand All @@ -93,37 +100,34 @@ enum Day {
Saturday,
}

// Use alias to create abbreviations for types

// Use alias to create abbreviations for types.
alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

// We can create function templates as well!

T max(T)(T a, T b) {
if(a < b)
return b;

return a;
}

// Use the ref keyword to ensure pass by referece.
// That is, even if a and b are value types, they
// will always be passed by reference to swap
// Use the ref keyword to ensure pass by reference. That is, even if 'a' and 'b'
// are value types, they will always be passed by reference to 'swap()'.
void swap(T)(ref T a, ref T b) {
auto temp = a;

a = b;
b = temp;
}

// With templates, we can also parameterize on values, not just types
// With templates, we can also parameterize on values, not just types.
class Matrix(uint m, uint n, T = int) {
T[m] rows;
T[n] columns;
}

auto mat = new Matrix!(3, 3); // We've defaulted type T to int
auto mat = new Matrix!(3, 3); // We've defaulted type 'T' to 'int'.

```
Expand All @@ -133,21 +137,20 @@ have the syntax of POD structures (`structure.x = 7`) with the semantics of
getter and setter methods (`object.setX(7)`)!
```c
// Consider a class parameterized on a types T, U
// Consider a class parameterized on types 'T' & 'U'.
class MyClass(T, U) {
T _data;
U _other;
}
// And "getter" and "setter" methods like so
// And "getter" and "setter" methods like so:
class MyClass(T, U) {
T _data;
U _other;
// Constructors are always named `this`
// Constructors are always named 'this'.
this(T t, U u) {
// This will call the setter methods below.
data = t;
other = u;
}
Expand All @@ -170,16 +173,24 @@ class MyClass(T, U) {
_other = u;
}
}
// And we use them in this manner
// And we use them in this manner:
void main() {
auto mc = MyClass!(int, string);
auto mc = new MyClass!(int, string)(7, "seven");
// Import the 'stdio' module from the standard library for writing to
// console (imports can be local to a scope).
import std.stdio;
// Call the getters to fetch the values.
writefln("Earlier: data = %d, str = %s", mc.data, mc.other);
mc.data = 7;
mc.other = "seven";
// Call the setters to assign new values.
mc.data = 8;
mc.other = "eight";
writeln(mc.data);
writeln(mc.other);
// Call the getters again to fetch the new values.
writefln("Later: data = %d, str = %s", mc.data, mc.other);
}
```

Expand Down
Loading

0 comments on commit 85adff2

Please sign in to comment.