├── README.md ├── bash ├── booleans ├── comments ├── conditionals ├── functions ├── intro ├── objects ├── primitives ├── strings ├── syntax └── variables ├── c ├── arrays ├── booleans ├── comments ├── conditionals ├── functions ├── intro ├── loops ├── maps ├── numbers ├── objects ├── primitives ├── strings ├── syntax └── variables ├── go ├── comments └── variables ├── java ├── comments ├── intro ├── primitives ├── syntax └── variables ├── javascript ├── arrays ├── comments ├── conditionals ├── intro ├── numbers ├── primitives ├── syntax └── variables ├── perl ├── booleans ├── comments ├── intro ├── primitives └── syntax ├── python ├── booleans ├── comments ├── dictionaries ├── intro ├── list ├── numbers ├── primitives ├── syntax └── variables ├── ruby ├── arrays ├── booleans ├── comments ├── hashes ├── intro ├── numbers ├── syntax └── variables ├── swift ├── comments └── intro └── topics.json /README.md: -------------------------------------------------------------------------------- 1 | # [Matrix](http://bento.io/matrix) 2 | An interactive tool for quickly learning programming languages. Built by [Bento](http://bento.io/). 3 | 4 | Curated by [@jonhmchan](http://twitter.com/jonhmchan). Like Bento or this project? [Leave me a tip](https://gratipay.com/JonHMChan). 5 | 6 | ## About 7 | Matrix was built with the philosophy that programming languages are very similar, and with the question of seeing what those similarities might look like. It also meant to be a learning aid such that programmers that know one language can quickly learn another. This repository holds all the data that is used by Matrix, and is actively being edited. 8 | 9 | ## Contributing 10 | Please read the notes below. Once changes are merged here, it will take some time for those changes to reflect on Bento because the repository needs to be pulled and the application refreshed. 11 | 12 | ## How this repository is structured 13 | Every programming language that is here is separated into a folder. If you see that a programming language that you know is not in this repository, feel free to add it here. For each folder there are multiple topics, as outlined in the topics.json file at the root of this repository. If you are going to add new information on a topic, make sure that the filename is exactly as it is shown in the topics.json file, otherwise it will not appear. 14 | 15 | Some things to note: 16 | * Only programming languages with similar features as those here will be merged in. These are languages that have many of the basic features one can expect: loops, variables, conditionals, etc. In some cases there are structures in a programming language that do not quite cleanly fall into any of the topics. For example, in Python, there are both lists and sets which may both be quite like arrays. You are free to put similar, but not exact topics together. Refrain from creating a new entry in the topics file unless it is a primary feature of *all* the languages that would appear here. 17 | * Be as concise and plain as possible. These are meant to be very quick references to get people programming in a language quickly. You can expect the users of this content to be intermediate programmers - people that are familiar with loops, conditionals, etc. That being said, it is not necessary to give context to every portion of a topic. 18 | * Stress practicality over formality. The content here is meant to get people productive in a language as quickly as possible. For example, in Go, there are arrays in the strict sense, but these are hardly ever used over slices in practice. The stance held for this project would be to have the content focus on slices and basic operations on that rather than its underlying data structure. 19 | -------------------------------------------------------------------------------- /bash/booleans: -------------------------------------------------------------------------------- 1 | # true and false are built-in commands that evaluate to their boolean values 2 | # however, in bash, true is 0 and false is 1 (due to exit codes) 3 | true 4 | false 5 | 6 | # the exit code of the last command to execute is stored in the special variable $? 7 | echo $? # echo the exit code of the last command, which is usually 0 (true) on success 8 | -------------------------------------------------------------------------------- /bash/comments: -------------------------------------------------------------------------------- 1 | # single line comments begin with a single hash (#) 2 | 3 | # there are no real multi-line comments in bash, 4 | # but there are a few workarounds: 5 | 6 | < /* Required for the boolean type, but only present in C99+ */ 4 | 5 | /*To declare booleans do as follows*/ 6 | bool facts = true; 7 | bool opinions = false; 8 | 9 | /* C uses && for logical AND and || for logical OR */ 10 | 11 | if (2 == 2 && 2 <= 3) { } /* will run since 2 equals itself AND is less than or equal to 3 */ 12 | if (5 != 4 && 5 >= 7) { } /* won't run since only one of the conditions is true, which is 5 isn't equal to 4 */ 13 | if (!(9 > 5 && 6 < 10)) { } /* won't run since exclamation point negates both true conditions making both false */ 14 | 15 | if (1 == 1 || 3 != 7) { } /* will run since both conditions are true, and you only need one */ 16 | if (2 <= 3 || 6 >= 9) { } /* will still run since you only need one condition to be met */ 17 | if (9 > 10 || 4 >= 8) { } /* won't execute since both conditions are false */ 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /c/comments: -------------------------------------------------------------------------------- 1 | // single line comments begin with two forward slashes 2 | 3 | // however, single line comments are not present in C89 4 | // you can use them in C99/MSVC/GNU89+, though 5 | 6 | /* multiline comments begin with a forward slash asterisk 7 | and end with an asterisk and another forward slash 8 | unlike single line comments, they can be used with any C standard 9 | */ -------------------------------------------------------------------------------- /c/conditionals: -------------------------------------------------------------------------------- 1 | /* C has both if-else and switch-style conditionals */ 2 | 3 | /* Because any non-zero value evaluates to true, C's conditionals can be very succinct: */ 4 | if (!strcmp(str1, str2)) /* strcmp returns 0 on equality, like many other library functions */ 5 | { 6 | printf("The strings are equal!\n"); 7 | } 8 | else if (strcmp(str1, str2) > 0) 9 | { 10 | printf("str1 is greater than str2!\n"); 11 | } 12 | else /* logical conclusion, the only state remaining is less-than */ 13 | { 14 | printf("str2 is greater than str1!\n"); 15 | } 16 | 17 | 18 | /* switch statements take a numeric or character value */ 19 | /* cases are kept from overflowing with break statements */ 20 | switch(c) 21 | { 22 | case 'a': 23 | printf("The letter \'a\'.\n"); 24 | break; 25 | case 'b': 26 | printf("The letter \'b\'.\n"); 27 | break; 28 | default: 29 | printf("Any other letter.\n"); 30 | } -------------------------------------------------------------------------------- /c/functions: -------------------------------------------------------------------------------- 1 | /* functions in C must either be declared or prototyped above main */ 2 | int func(void); /* function prototypes */ 3 | void another_func(int num); 4 | void third_func(int *num); 5 | 6 | /* main function here */ 7 | 8 | int func(void) /* 'func' takes no arguments (void) and returns an int */ 9 | { 10 | printf("Inside a function.\n"); 11 | return 0; 12 | } 13 | 14 | void another_func(int num) /* 'another_func' takes one int and returns nothing */ 15 | { 16 | printf("%s%d\n", "Inside a function with int: ", num); 17 | } 18 | 19 | /* C is pass-by-value, but it is possible to pass-by-reference with pointers: */ 20 | void third_func(int *num) /* 'third_func' takes one int pointer and returns nothing */ 21 | { 22 | *num += 10; /* 'num' is dereferenced and incremented by 10 23 | } -------------------------------------------------------------------------------- /c/intro: -------------------------------------------------------------------------------- 1 | /* C is an ALGOL-derived programming language developed by Dennis Ritchie at Bell Labs */ 2 | /* It is commonly used for systems and embedded programming */ 3 | /* Most operating systems contain a significant amount of C in their kernels */ 4 | -------------------------------------------------------------------------------- /c/loops: -------------------------------------------------------------------------------- 1 | /* there are three major loops in C: for, while, and do-while */ 2 | 3 | /* a for loop iterates n times until its condition fails: */ 4 | for (int i = 0; i < 10; i++) /* C99 style. In C89, 'i' must be declared before */ 5 | { 6 | printf("%d\n", i); /* i is printed 10 times (0, 1, 2, ..., 9) 7 | } 8 | 9 | /* a while loop repeats until its condition fails */ 10 | while (i >= 0) 11 | { 12 | printf("%d\n", i); 13 | } 14 | 15 | /* a do-while loop repeats at least once, until its condition fails */ 16 | do 17 | { 18 | int c = getchar(); /* int is used to accomodate the EOF character */ 19 | putchar(c); 20 | } while (c != '\n') 21 | 22 | /* you can also have infinite loops: */ 23 | for (;;) { } /* blank statements, making the loop evaluate infinitely */ 24 | while (1) { } /* true can also be used in C99 */ -------------------------------------------------------------------------------- /c/maps: -------------------------------------------------------------------------------- 1 | /* Unfortunately, as an older language with a (relatively) small standard library, 2 | C does not have complex integral data structures like maps, hashes, and linked lists. 3 | However, many of those structures can be found in third-party, open-source libraries. 4 | */ -------------------------------------------------------------------------------- /c/numbers: -------------------------------------------------------------------------------- 1 | /* C has plenty of number primitives, which are all either floating-point or integral */ 2 | /* Their size depends on the architecture of your machine expect for char, which is always one byte. */ 3 | 3; /* this can be a char, short, int, long, or long long all signed or unsigned */ 4 | 1.5; /* this can be a float, double, or long double */ 5 | 6 | /* basic arithmetic uses the standard symbols: */ 7 | 1 + 1; /* == 2 */ 8 | 8 - 1; /* == 7 */ 9 | 10 * 2; /* == 20 */ 10 | 35 / 5; /* == 7 */ 11 | 12 | /* unless explictly cast, uneven division trims according to the data type */ 13 | 5 / 2; /* == 2 if stored in an integral type */ 14 | 5 / 2; /* == 2.5 if cast into a floating point type */ 15 | 16 | /* parentheses can be used to delimit order of operation */ 17 | (1 + 3) * 2; /* == 8 */ 18 | 19 | /* provides special values and functions for mathematics */ 20 | HUGE_VAL /* represents a double too large to represent */ 21 | -HUGE_VAL 22 | NAN /* used by some implementations to denote an impossible result */ 23 | 24 | M_PI /* contains a close representation of PI (POSIX) */ 25 | floor(1.999) /* == 1.0 */ -------------------------------------------------------------------------------- /c/objects: -------------------------------------------------------------------------------- 1 | /* C is a procedural language, and does not have objects */ 2 | /* However, it does have data structures that represent data one-to-one in memory */ 3 | 4 | /* FILE pointers can be used to access files: */ 5 | FILE *my_file = fopen("/the/file", "r"); /* open a file for reading */ 6 | 7 | /* you can also define structures */ 8 | struct apple 9 | { 10 | int x, y, z; 11 | }; 12 | 13 | typedef struct 14 | { 15 | int w, x, y, z; 16 | } banana; 17 | 18 | /* declaring structures: */ 19 | struct apple foo; /* declares an apple structure*/ 20 | banana bar; /* typedef'd structures do not require a 'struct' during declaration */ 21 | 22 | /* struct elements can then be accessed with the dot operator: */ 23 | foo.x = 5; 24 | bar.w = 100; -------------------------------------------------------------------------------- /c/primitives: -------------------------------------------------------------------------------- 1 | /* numbers in c */ 2 | 1700 /* base-10 int or long */ 3 | 0170 /* base-8 int or long */ 4 | 0x17 /* base-16 int or long */ 5 | 17.5 /* IEEE 754 float or double */ 6 | 7 | /* strings are enclosed by double quotes */ 8 | "this is a string" /* strings have an implicit NULL character at the end */ 9 | 10 | /* characters are enclosed by single quotes */ 11 | 'a' /* there is no NULL character here */ 12 | 13 | /* C89 does not have booleans, but C99 does with stdbool.h */ 14 | true 15 | false 16 | 17 | /* NULL is used to represent nothing, or 0 */ 18 | NULL 19 | '\0' /* ASCII equivalent of NULL */ -------------------------------------------------------------------------------- /c/strings: -------------------------------------------------------------------------------- 1 | /* in C, character strings are just pointers to the first character in a sequence, 2 | terminated by a null character ('\0'). Because of that, they are both simple and 3 | dangerous to use (at times). 4 | */ 5 | 6 | /* my_string is declared on the stack */ 7 | char *my_string = "This is my string!"; /* initialization adds a null character implicitly */ 8 | 9 | int my_len = strlen(my_string); /* returns the length of the string (17), not including the null character */ 10 | 11 | /* because my_string was declared with pointer notation, it is located in rodata, 12 | or the read-only data section. Because of that, attempting to modify it will result in a 13 | segmentation fault. 14 | */ 15 | strncpy(my_string, "This is my string.", my_len); /* segfaults */ 16 | 17 | /* my_other_string is declared with array notation (because arrays degrade to pointers), 18 | and length is determined at compile time. The primary difference is that my_other_string 19 | is located in rwdata, unlike my_string. Therefore, it will not segfault whenever a function 20 | attempts to write to it. 21 | */ 22 | char my_other_string[] = "This is my other string!"; 23 | strncpy(my_other_string, "This is my other string.", strlen(my_other_string)); /* works as intended */ 24 | 25 | /* Of course, there are many other pitfalls to C-strings. It's very easy to accidentally overwrite 26 | the null character, making your string unterminated and therefore run until the next null byte is 27 | encountered in memory. It's also easy to abuse unsafe functions like strcpy and sprintf, which 28 | don't check the size of the buffer they write to and create overflow bugs. 29 | */ -------------------------------------------------------------------------------- /c/syntax: -------------------------------------------------------------------------------- 1 | /* statements in C end with a semicolon, and can be chained together */ 2 | step_one(); 3 | step_two(); step_three(); step_four(); 4 | 5 | /* scope is defined by braces, and braces can be placed arbitrarily */ 6 | void hello_world(void) 7 | { 8 | printf("Hello, World\n"); 9 | } 10 | 11 | { 12 | hello_world(); /* this function call is within its own scope due to the braces */ 13 | } 14 | 15 | hello_world(); /* this function is within the scope of the calling function's braces */ -------------------------------------------------------------------------------- /c/variables: -------------------------------------------------------------------------------- 1 | int x; /* variables are declared on the stack by type */ 2 | x; /* because x is not initialized, it contains garbage data */ 3 | x = 5; /* x is now initialized to 5 */ 4 | 5 | char *y = "bento"; /* y is a character pointer (string) initialized with "bento" */ -------------------------------------------------------------------------------- /go/comments: -------------------------------------------------------------------------------- 1 | // This is a single-line comment 2 | 3 | /* 4 | This is a multi-line comment. 5 | It starts with a slash and star then ends with a star and slash. 6 | */ 7 | 8 | // Doc comments precede every function in a package 9 | func SomeFunction() { 10 | return 11 | } 12 | 13 | // Every exported (capitalized) function should have a doc comment -------------------------------------------------------------------------------- /go/variables: -------------------------------------------------------------------------------- 1 | var x int // You declare new variables with the var statement, then the type 2 | x = 3 // Assigning a value to the variable 3 | 4 | x := 3 // Short hand: declares and assigns with type inference 5 | 6 | y, z int = 5, 6 // Multiple assignment 7 | 8 | const c string = "constant" // A constant, value cannot be changed 9 | const a = 5000 // Constants do not have a type except when explicit 10 | 11 | // Variables that are not used will throw an error (more on that later) -------------------------------------------------------------------------------- /java/comments: -------------------------------------------------------------------------------- 1 | //Two slashes indicate a single line comment 2 | 3 | /* a slash 4 | followed by an asterisk 5 | represents a multi-line comment. 6 | It ends with an asterisk followed by a slash */ 7 | -------------------------------------------------------------------------------- /java/intro: -------------------------------------------------------------------------------- 1 | /* Java is a high-level, virtualized language invented by James Gosling in 1995. 2 | It is commonly used due to its extreme portability and large standard API. 3 | */ -------------------------------------------------------------------------------- /java/primitives: -------------------------------------------------------------------------------- 1 | // Java has lots of types of varying size for primitives 2 | // However, unsigned integers were unsupported until Java 8. 3 | 1700 // short, int, or long, base-10 4 | 0170 // short, int, or long, base-8 5 | 0x17 // short, int, or long, base-16 6 | 0b11 // short, int, or long, base-2 7 | 8 | // Strings are enclosed with double-quotes 9 | "this is a string literal" 10 | 11 | // Characters in Java are UTF-16 and enclosed with single-quotes 12 | 'a' 13 | 14 | 15 | // Java has boolean barewords due to its strict typing 16 | true 17 | false 18 | 19 | 20 | // null is used to represent uninstantiated objects 21 | null -------------------------------------------------------------------------------- /java/syntax: -------------------------------------------------------------------------------- 1 | // Like many other languages, all statements in Java end with a semicolon. 2 | String str = "this is a string"; 3 | int x = str.length(); 4 | 5 | // All methods have their own (local) scope, and classes can be scoped variably. 6 | public void myMethod() // the method itself has a public scope within the class 7 | { 8 | String str = "Hello, world!"; // 'str' only exists within this method 9 | System.out.println(str); 10 | } 11 | 12 | // The variable 'str' does not exist here -------------------------------------------------------------------------------- /java/variables: -------------------------------------------------------------------------------- 1 | //To declare variables in Java you need to declare the variable type, followed by the variable name. 2 | //Declare variables as follows 3 | 4 | int number; //Declares int type variable called number 5 | boolean g; //Declares boolean type variable called g 6 | 7 | //on a different line you would then assign the declared variable a value as such 8 | number = 78; 9 | g = true; 10 | 11 | //you can however declare a variable and assign it on the same line as follows 12 | int number = 78; //declares int type variable number and assigns the value 78 to it 13 | boolean g = true; //declares boolean type variable g and assigns the value true to it 14 | -------------------------------------------------------------------------------- /javascript/arrays: -------------------------------------------------------------------------------- 1 | // This is an array 2 | var array = ["High", "school", "hackers", "are", "awesome"]; 3 | 4 | // You can access an array like this 5 | array[2]; // "hackers" 6 | 7 | // Arrays have a length property 8 | array.length; // 5 9 | 10 | // Arrays have built-in functions too 11 | array.push('!'); // the value of array is now ["High", "school", "hackers", "are", "awesome", "!"] 12 | array.pop(); // the value of the array is back to ["High", "school", "hackers", "are", "awesome"] 13 | array.indexOf("hackers"); // returns 2 14 | array.join(' ') // returns "High school hackers are awesome" -------------------------------------------------------------------------------- /javascript/comments: -------------------------------------------------------------------------------- 1 | // This is a single-line comment 2 | 3 | console.log('boop'); // This is a single line comment after a statement 4 | 5 | /* 6 | This is a multi-line comment. 7 | It starts with a slash and star then ends with a star and slash. 8 | */ -------------------------------------------------------------------------------- /javascript/conditionals: -------------------------------------------------------------------------------- 1 | // This is a Javascript if, else if, and else statement 2 | 3 | if (conditionOne) { 4 | // if condition evaluates to true, do the stuff here 5 | console.log('conditionOne all the way'); 6 | } else if (conditionTwo) { 7 | // if conditionOne is false, but conditionTwo is true, do the stuff here 8 | console.log('conditionTwo all the way'); 9 | } else { 10 | // if neither all of the conditions above are false, do the stuff here 11 | console.log('I support no one'); 12 | } 13 | 14 | // You can also use a switch statements, which compares an expression's value 15 | // to the values of different cases. 16 | 17 | day = "Thursday" 18 | 19 | // After the following code block, there will have been a console.log(":)"); 20 | switch (day) { 21 | case "Monday": 22 | console.log(":'("); 23 | break; 24 | case "Tuesday": 25 | console.log(":("); 26 | break; 27 | case "Wednesday": 28 | console.log(":/"); 29 | break; 30 | case "Thursday": 31 | console.log(":)"); 32 | break; 33 | case "Friday": 34 | console.log(":D"); 35 | } 36 | 37 | // Switch statements can also contain defaults, like so: 38 | 39 | switch (day) { 40 | case "Monday": 41 | console.log(":'("); 42 | break; 43 | default: 44 | console.log(":/"); 45 | } -------------------------------------------------------------------------------- /javascript/intro: -------------------------------------------------------------------------------- 1 | // JavaScript is a programming language used in web applications 2 | // It is most widely used to provide interaction on the browser 3 | // However, it is also used as a backend language with Node -------------------------------------------------------------------------------- /javascript/numbers: -------------------------------------------------------------------------------- 1 | // JavaScript has one number type (which is a 64-bit IEEE 754 double). 2 | // don't freak out about the lack of ints: doubles have a 52-bit 3 | // mantissa, which is enough to store integers up to about 9✕10¹⁵ precisely. 4 | 3; // = 3 5 | 1.5; // = 1.5 6 | 7 | // All the basic arithmetic works as you'd expect. 8 | 1 + 1; // = 2 9 | 8 - 1; // = 7 10 | 10 * 2; // = 20 11 | 35 / 5; // = 7 12 | 13 | // Including uneven division. 14 | 5 / 2; // = 2.5 15 | 16 | //Also Modulus 17 | 40 % 12; 18 | //this will return 4 which is the remainder of the division. 19 | 20 | // Precedence is enforced with parentheses. 21 | (1 + 3) * 2; // = 8 22 | 23 | // There are three special not-a-real-number values: 24 | Infinity; // result of e.g. 1/0 25 | -Infinity; // result of e.g. -1/0 26 | NaN; // result of e.g. 0/0 27 | 28 | // There are a lot more things you can do with Math 29 | Math.PI; // = 3.141592653589793 30 | Math.floor(1.618); // = 1 31 | -------------------------------------------------------------------------------- /javascript/primitives: -------------------------------------------------------------------------------- 1 | // numbers like you'd expect 2 | 5; 3 | 99; 4 | 3.1416; 5 | // numbers are all of the same type (64-bit IEEE 754 double) 6 | // for the most part this should be okay 7 | 8 | // an explicit type for something that is not a number 9 | NaN; // occurs when you do something weird like add undefined to a number 10 | 11 | // strings can be enclosed with double or single quotes 12 | "a string"; // double quotes 13 | 'another string'; // single quotes 14 | 15 | // booleans 16 | //booleans can be either number based like 1 = 1 or finding the length of something and comparing. 17 | 1 == 1; 18 | //result returns true 19 | true; 20 | 100 == 1200; 21 | //the result returns false 22 | false; 23 | //comparing lengths boolean 24 | "HS Hackers".length == "Are awesome!!!!".length; 25 | //this would return false, since "HS Hackers" w/ the space is (10), while "Are awesome!!!!" returns (15). 26 | 27 | // undefined type, means a variable is declared but not assigned 28 | var x; // = undefined 29 | 30 | // null, means no value is present, subtlely different from undefined 31 | null; // = null 32 | null === undefined; // = false 33 | 34 | // more on false-like, empty-like values later 35 | -------------------------------------------------------------------------------- /javascript/syntax: -------------------------------------------------------------------------------- 1 | // Statements in javascript end with a semicolon 2 | doSomething(); 3 | 4 | // But they don't always have to 5 | doSomething() // This works just fine 6 | 7 | // But be careful. There are some weird things that can happen when there's no 8 | // semicolon because javascript automatically tries to insert them whenever 9 | // there's a newline. So it's better to use semicolons in practice. -------------------------------------------------------------------------------- /javascript/variables: -------------------------------------------------------------------------------- 1 | var x; // You declare new variables with the var statement 2 | x // undefined 3 | 4 | x = 5; // x now has the value of 5 5 | var y = "bento" // y is initialized and now has the string "bento" assigned to it 6 | 7 | var log = console.log; //you can assign shorter var to strings that already exist like console.log() which basically logs info to the console for debugging. 8 | //an example of using log would be 9 | log("This is now console.log!"); 10 | -------------------------------------------------------------------------------- /perl/booleans: -------------------------------------------------------------------------------- 1 | # perl does not have boolean barewords, but can evaluate variables to true or false 2 | 3 | my $truevar = 1; # 1 will always evaluate to true 4 | my $falsevar = 0; # 0 will always evaluate to false 5 | 6 | # other things that evaluate to false: 7 | '0' 8 | undef 9 | '' 10 | () 11 | ('') 12 | # all other data evaluates to true 13 | -------------------------------------------------------------------------------- /perl/comments: -------------------------------------------------------------------------------- 1 | # single-line comments in Perl begin with a hash 2 | 3 | # there are a number of ways to write multi-line comments: 4 | 5 | =for comment 6 | This is a multiline comment using POD. 7 | =cut 8 | 9 | # Acme::Comment can also be used to import other languages comment styles: 10 | 11 | use Acme::Comment type=>'C++'; 12 | /* 13 | This is now a valid multiline comment in Perl. 14 | */ -------------------------------------------------------------------------------- /perl/intro: -------------------------------------------------------------------------------- 1 | # Perl is a scripting language invented by Larry Wall in 1987. 2 | # It is commonly used for system scripting and data processing. 3 | # Perl's strengths include regular expressions and the CPAN, a massive module system. 4 | -------------------------------------------------------------------------------- /perl/primitives: -------------------------------------------------------------------------------- 1 | # perl is loosely typed, and only has three primitives 2 | # those three primitives are scalars, arrays, and hashes 3 | 4 | # all of the following are valid scalars 5 | 15 6 | "this is a scalar" 7 | 'x' 8 | 0x999 9 | 0.12 10 | 11 | # arrays can contain multiple types and ranges 12 | (800, 5, 8, 8, 2300); 13 | (1..5, a..z); 14 | ("Cat", "Dog", "Pineapple", 10); 15 | 16 | # hashes are key-value pairs 17 | ('Cat' => 1, 'Dog' => 2, 'Winnebago' => 3); 18 | -------------------------------------------------------------------------------- /perl/syntax: -------------------------------------------------------------------------------- 1 | # statements in perl end in a semicolon 2 | print "Hello!\n"; 3 | 4 | # scope is defined by braces that can be placed arbitrarily 5 | { 6 | my $x = 5; # x declared and initialized in a local scope 7 | } 8 | # x does not exist here 9 | 10 | -------------------------------------------------------------------------------- /python/booleans: -------------------------------------------------------------------------------- 1 | #Booleans in python can be assigned to variables as follows 2 | honesty = True 3 | fake = False 4 | 5 | #Boolean operators are as follows 6 | 7 == 7 #This is True because 7 equals itself 7 | 2 != 2 #This is False because 2 does equal itself 8 | 3 > 1 #True because 3 is greater than 1 9 | 6 < 1 #False because 6 isn't less than 1 10 | 2 >= 2 #True because 2 is greater than or EQUAL to itself 11 | 2 <= 2 #True becuase 2 is less than or EQUAL to itself 12 | 13 | #Also if you add the word 'not' infront of a statement it negates it so 14 | not (2 == 2) #False because 2 equals 2 is true and negate that making false 15 | not (2 > 3) #True because 2 is greater than 3 is false and negate that making it true 16 | 17 | #There are also AND and OR conditons. 18 | (2 == 2 and 2 < 4) #2 True statements evaluate to True 19 | (2 != 3 and 2 < 1) #One True AND one False statement evaluate to False when using the and statement 20 | (5 > 9 and 4 < 3) #2 False statements evaluate to False 21 | 22 | (6 == 6 or 6 < 11) #2 True statements evaluate to True 23 | (8 < 10 or 2 < 13) #one True OR one false statement evaluate to True when using the or statement 24 | (1 > 7 or 5 <= 4) #2 False statements evaluate to False 25 | -------------------------------------------------------------------------------- /python/comments: -------------------------------------------------------------------------------- 1 | # All comments start with the pound/hash sign 2 | 3 | # So multiline comments are really just... 4 | # multiple single line comments one after the other 5 | 6 | """ Multiline strings can be written with three double quotes 7 | and be used as a multi-line comment. It's not really 8 | all that common though 9 | """ -------------------------------------------------------------------------------- /python/dictionaries: -------------------------------------------------------------------------------- 1 | #Dictionaries are a type of data structure in python which have key-value pairs, 2 | #and are essentially the same as associative arrays in other programming languages. 3 | 4 | #To create a dictionary, assign a variable a value with curly-braces 5 | dict = {'key1': 100, 'key2': 125, 'key3': 415} 6 | 7 | #to add a key-value pair to an existing dictionary do 8 | dict['key4'] = 509 #key4 is the Key and 509 is the value 9 | 10 | #to remove a Key-value pair do 11 | del dict['key1'] 12 | 13 | #to see just the keys without their paired value use the method 14 | dict.keys() # note than in the above example key1 wouldn't be printed since it was deleted. 15 | -------------------------------------------------------------------------------- /python/intro: -------------------------------------------------------------------------------- 1 | #Python is an object-oriented and high-level programming language. 2 | #Currently there are two prominent Python versions: Python 2.7 and Python 3. 3 | #Although some differences do exist between the two, there is still a strong resemblance. 4 | #Python was made for simplicity and clarity, yet is still capable of creating powerful applications. 5 | #Some major uses for Python among others, would be web applications through the Django framework, 6 | #and for microcomputers such as Raspberry Pi. 7 | 8 | -------------------------------------------------------------------------------- /python/list: -------------------------------------------------------------------------------- 1 | #This is a list in Python 2 | 3 | exampleList = ["Apple", "Orange", "Banana"] 4 | 5 | #Be sure to englulf the indices of the list in square brackets, and separate them with commas. 6 | -------------------------------------------------------------------------------- /python/numbers: -------------------------------------------------------------------------------- 1 | #There's a lot you can do with numbers in Python 2 | 3 | import math # imports math module, we will use this later 4 | 5 | 5 # prints 5 6 | 3 + 5 # adds 3 and 5 to get 8 7 | 5 - 1 # subtracts 1 from 5 to get 4 8 | 6 * 7 # multiplies 6 and 7 to get 42 9 | 9 / 3 # divides 9 by 3 to get 3 10 | 9 / 2 # divides 9 by 2 to get 4. You don't get 4.5 because dividing 2 int types can't translate into a float therefore do 11 | 9.0 / 2.0 # TA-DA! now you should get 4.5 as your answer 12 | 2**5 # 2 to the power of 5 to get 32 13 | 11 % 2 # finds the remainder of 11 divided by 2 which is 1 14 | 2 == 2 # prints true since equals 2 15 | 2 < 1 # prints false since 2 isn't less than 1 16 | 2 <=2 # prints true since 2 is less than or EQUAL to itself 17 | 2 > 1 # prints true since 2 is greater than 1 18 | 2 >= 2 # prints true since 2 is greater than or EQUAL to itself 19 | 3 != 8 # prints true since 3 is not equal to 8 20 | abs(-3) # prints the absolute value of negative 3, which is 3 21 | 22 | #Now time to use the math module, you must import math in order to execute these commands 23 | 24 | math.floor(18.9) #this rounds down any number, even if it should be rounded up so 18.9 --> 18.0 25 | math.ceil(12.4) #this rounds up any number, even if it should be rounded down so 12.4 --> 13.0 26 | math.sqrt(81) #this takes the square root of the number in the parameters so 81 --> 9.0 27 | math.pi #prints pi which is 3.1415926535897931 28 | math.factorial(9) #Finds the factorial of which is 362880 29 | math.e #prints the mathematical constant e which is 2.7182818284590451 30 | math.pow(3,4) #prints 3 to the 4th power which is 81.0 note: An alternitive would be doing 3**4 31 | -------------------------------------------------------------------------------- /python/primitives: -------------------------------------------------------------------------------- 1 | # numbers in python 2 | 99 # int type 3 | 3.1416 # float type 4 | 5 | # strings can be enclosed with double or single quotes 6 | "a string" # double quotes 7 | 'another string' # single quotes 8 | 9 | # booleans, note the case sensitivity 10 | True 11 | False 12 | 13 | # none type representing an empty value 14 | None -------------------------------------------------------------------------------- /python/syntax: -------------------------------------------------------------------------------- 1 | # statements in python are terminated with a new line, no semicolons or anything 2 | stepOne() 3 | stepTwo() 4 | 5 | # instead of braces, scope is determined with nested indents 6 | # for example look at the following function 7 | def helloWorld(): 8 | print "Hello World" 9 | 10 | helloWorld() # breaking out of the function scope -------------------------------------------------------------------------------- /python/variables: -------------------------------------------------------------------------------- 1 | #You declare new variables as follows 2 | x = 5 # x is equal to 5 3 | y = "Hello" # y equals string Hello 4 | 5 | #You may declare multiple variables on the same line as follows 6 | x, y, z = 3, 4, 5 7 | 8 | #In the above example x is equivalent to 3, y is equivalent to 4, and z is equivalent to 5 9 | -------------------------------------------------------------------------------- /ruby/arrays: -------------------------------------------------------------------------------- 1 | #There are two common ways to create arrays in Ruby 2 | #The first way to do so is 3 | 4 | arr = [1,2,3,4,5] #the arr Array has five elements ranging from 1-5 5 | 6 | #the other way to create an array is as follows 7 | example = Array.new(5) #this creates an array called example with five EMPTY elements, determined by the integer you enntered as a parameter. 8 | 9 | #using the second way, to make an array with numbers 1-5 you would do 10 | example = Array.new(5) { |element| element += 1 } #this creates an array called example with elements ranging from 1-5 11 | 12 | -------------------------------------------------------------------------------- /ruby/booleans: -------------------------------------------------------------------------------- 1 | #Booleans in ruby can be assigned to variables as follows 2 | truth = true 3 | lies = false 4 | 5 | #Boolean operators are as follows 6 | 2 == 2 #This is true because 2 equals itself 7 | 2 != 2 #This is false because 2 does equal itself 8 | 2 > 1 #true because 2 is greater than 1 9 | 2 < 1 #false because 2 isn't less than 1 10 | 2 >= 2 #true because 2 is greater than or EQUAL to itself 11 | 2 <= 2 #true becuase 2 is less than or EQUAL to itself 12 | 13 | #Also if you add an exclamation point infront of a statement it negates it so 14 | !(2 == 2) #false because 2 equals 2 is true and negate that making false 15 | !(2 > 3) #true because 2 is greater than 3 is false and negate that making it true 16 | 17 | #There are also AND and OR conditons. 2 ampersands represnt "and", 2 vertical bars represent "or" 18 | (2 == 2 && 2 < 4) #2 true statements evaluate to true 19 | (2 != 3 && 2 < 1) #One true AND one false statement evaluate to false when using double ampersand 20 | (5 > 7 && 4 < 3) #2 false statements evaluate to false 21 | 22 | (6 == 6 || 6 < 8) #2 true statements evaluate to true 23 | (8 < 10 || 2 < 1) #1 true OR one false statement evaluate to true when using two vertical bars 24 | (1 > 7 || 5 < 4) #2 false statements evaluate to false 25 | -------------------------------------------------------------------------------- /ruby/comments: -------------------------------------------------------------------------------- 1 | #Comments are ignored by the ruby interpreter 2 | #In order to write a single-line comment use the number sign 3 | 4 | =begin 5 | This is a multi-line comment. 6 | Anything between the =begin and the =end 7 | will be ignored by the ruby interpreter 8 | =end 9 | 10 | 11 | -------------------------------------------------------------------------------- /ruby/hashes: -------------------------------------------------------------------------------- 1 | #In order to create a hash in Ruby do as follows 2 | 3 | exHash = { 4 | :first => 1, #in older ruby versions the norm was to use an equals sign followed by a greater than sign. BE SURE TO ADD COMMAS between Key-value pairs 5 | second: 3 #the current norm is separate the key from the value using a colon similar to a dictionary in Python 6 | } 7 | 8 | #Additionally you can create a hash by doing 9 | sampleHash = Hash.new 10 | sampleHash["one"] = 1 #Adds Key one paired with value 1 to the hash sampleHash 11 | -------------------------------------------------------------------------------- /ruby/intro: -------------------------------------------------------------------------------- 1 | #Ruby is an object oriented language most commonly used for creating 2 | #web applications. It is closely related to python and Perl. It is 3 | #dynamically typed, and is designed to make programmers happy. 4 | #The most common framework that Ruby is used in is Rails, a robust 5 | #choice to use for the backend of a web app. -------------------------------------------------------------------------------- /ruby/numbers: -------------------------------------------------------------------------------- 1 | #There are two basic number types in ruby 2 | #The first is Integer 3 | #The second is Float 4 | 5 | 3 #returns 3 6 | 2.7 #returns 2.7 7 | 8 | #Basic arithmetic works, too 9 | 10 | 3+2 #returns 5 11 | 2+2.5 #returns 4.5 12 | 13 | 3-2 #returns 1 14 | 3-2.5 #returns 0.5 15 | 16 | 3*2 #returns 6 17 | 4*1.5 #returns 6 18 | 19 | 4/2 #returns 2 20 | 5/2 #also returns 2, but only because it can only return an int value 21 | 5.0/2 #returns 2.5, as you would expect 22 | 5/2.0 #also returns 2.5, as you would expect 23 | 5.0/2.0 #also returns 2.5 24 | 25 | 26 | #modulus also works as it would in other languages. 27 | 5%2 #this would return 1, as the remainder of 5%2 is 1 28 | 4%2 #returns 0, as 4 is divisible by 2 29 | 30 | #exponents 31 | 5**2 #returns 25 -------------------------------------------------------------------------------- /ruby/syntax: -------------------------------------------------------------------------------- 1 | #Ruby's syntax is somewhat different from other languages. 2 | 3 | #In terms of control flow, ruby utilize whitespace nor curly braces 4 | #However, ruby requires you to type the keyword end at the end of some blocks. 5 | 6 | a = true 7 | if a == true 8 | puts "a is true!" 9 | else #end is not necessary with else and elsif. 10 | puts "a is false" 11 | end #however end is necessary for each if statement. 12 | 13 | b = false 14 | if b == true 15 | puts "b is true!" 16 | end 17 | 18 | #Loops are somewhat different in ruby as well. 19 | 20 | array = [1,2,3,4,5] 21 | array.each do |x| #looping through an array 22 | puts x #prints out each number on a seperate line. 23 | end 24 | 25 | (1..5).each do |x| #replacement for for loop 26 | puts x #does same thing as above example 27 | end 28 | 29 | (1..5).each { |x| puts x } #does the same thing as above examples 30 | #but only on one line :) -------------------------------------------------------------------------------- /ruby/variables: -------------------------------------------------------------------------------- 1 | # To declare new variables in ruby do as follows 2 | x = true # x has the boolean value of true 3 | y = "Ruby String!" # y equals the string Ruby String! 4 | 5 | #You may declare multiple variables on the same line as follows 6 | x, y, z = 6, 7, 8 7 | 8 | #In the above example x is equivalent to 6, y is equivalent to 7, and z is equivalent to 8 9 | 10 | =begin 11 | Note: if the variable name begins with a capital letter 12 | Ruby will treat it as a Constant. What this means is, that 13 | the value of that variable shouldn't change. However, if you do 14 | reassign that variable with a different value, ruby will allow 15 | you to do so, but will give you a warning. Do not be alarmed by it. 16 | So for example as follows. 17 | =end 18 | 19 | ExConstant = 9 # ExConstant is equal to 9 20 | ExConstant = 11 # ExConstant is equal to 11 now 21 | 22 | =begin 23 | The ruby interpreter will give you this message: 24 | warning: already initialized constant ExConstant 25 | warning: previous definition of ExConstant was here 26 | => 11 27 | 28 | You may ignore this message, since ExConstant's value was infact changed to 11 29 | =end 30 | -------------------------------------------------------------------------------- /swift/comments: -------------------------------------------------------------------------------- 1 | //One line comments in swift start with two forward slashes 2 | 3 | /* 4 | Multi line comments start with a forward slash and an asterisk 5 | and end with an asterisk and a forward slash 6 | */ -------------------------------------------------------------------------------- /swift/intro: -------------------------------------------------------------------------------- 1 | //Swift is apple's new programming language designed to be SUPER FAST 2 | //Swift will end up replacing Objective-C, and is able to be used to 3 | //make iOS and OSX apps. It features many aspects of modern programming 4 | //languages, such as closures, automatic memory management, and tuples. 5 | 6 | //AKA OBJECTIVE-C WITHOUT THE C -------------------------------------------------------------------------------- /topics.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "key": "intro", 4 | "name": "Introduction", 5 | "description": "Basic information about the language", 6 | "keywords": [] 7 | }, 8 | { 9 | "key": "comments", 10 | "name": "Comments", 11 | "description": "How to insert non-executable comments in your code", 12 | "keywords": ["comments", "multiline", "multi-line"] 13 | }, 14 | { 15 | "key": "syntax", 16 | "name": "Syntax", 17 | "description": "Language specific patterns on how to write and organize code", 18 | "keywords": ["semicolons", "tabs", "spaces", "brackets", "styles"] 19 | }, 20 | { 21 | "key": "primitives", 22 | "name": "Primitive Types", 23 | "description": "Basic types like numbers, booleans, strings", 24 | "keywords": ["numbers", "floats", "integers", "booleans", "strings", "true", "false", "basics"] 25 | }, 26 | { 27 | "key": "variables", 28 | "name": "Variables", 29 | "description": "How to declare variables and use them", 30 | "keywords": ["variables", "initialize", "declares", "assignment", "multiple", "types", "inference", "dynamic", "static", "constants"] 31 | }, 32 | { 33 | "key": "numbers", 34 | "name": "Numbers and Math", 35 | "description": "Number types and basic operations", 36 | "keywords": ["integers", "floats", "double", "long", "signed", "unsigned", "bytes"] 37 | }, 38 | { 39 | "key": "booleans", 40 | "name": "Booleans", 41 | "description": "True and false types, boolean functions, and comparisons", 42 | "keywords": ["true", "false", "less", "equal", "greater", "and", "or", "&&", "||", "=="] 43 | }, 44 | { 45 | "key": "strings", 46 | "name": "Strings", 47 | "description": "Common string operations", 48 | "keywords": [] 49 | }, 50 | { 51 | "key": "conditionals", 52 | "name": "Conditionals and Switches", 53 | "description": "If/else logic, handling multiple cases", 54 | "keywords": [] 55 | }, 56 | { 57 | "key": "arrays", 58 | "name": "Arrays and Lists", 59 | "description": "Sequentially ordered data, sets, multidimensional arrays", 60 | "keywords": [] 61 | }, 62 | { 63 | "key": "maps", 64 | "name": "Maps, Hashes, and Associative Arrays", 65 | "description": "Structured data as key, value pairs", 66 | "keywords": [] 67 | }, 68 | { 69 | "key": "loops", 70 | "name": "Loops", 71 | "description": "How to repeat instructions, control flow", 72 | "keywords": [] 73 | }, 74 | { 75 | "key": "functions", 76 | "name": "Functions", 77 | "description": "Defining and reusing code blocks", 78 | "keywords": [] 79 | }, 80 | { 81 | "key": "objects", 82 | "name": "Objects", 83 | "description": "Basics of object oriented features", 84 | "keywords": [] 85 | } 86 | ] --------------------------------------------------------------------------------