From 2ae3d41c2749645fec4748c764ccd5c9405424e5 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 15:09:35 -0500 Subject: Fix formatting, close #990 Cleaned up mixed tabs/spaces Wrapped lines at 80 characters Fixed incorrect comment regarding property name --- typescript.html.markdown | 125 ++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/typescript.html.markdown b/typescript.html.markdown index 9f04169a..937ebda1 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -14,100 +14,111 @@ This article will focus only on TypeScript extra syntax, as oposed to [JavaScrip To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. ```js -//There are 3 basic types in TypeScript +// There are 3 basic types in TypeScript var isDone: boolean = false; var lines: number = 42; var name: string = "Anders"; -//..When it's impossible to know, there is the "Any" type +// When it's impossible to know, there is the "Any" type var notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean -//For collections, there are typed arrays and generic arrays +// For collections, there are typed arrays and generic arrays var list: number[] = [1, 2, 3]; -//Alternatively, using the generic array type +// Alternatively, using the generic array type var list: Array = [1, 2, 3]; -//For enumerations: +// For enumerations: enum Color {Red, Green, Blue}; var c: Color = Color.Green; -//Lastly, "void" is used in the special case of a function not returning anything +// Lastly, "void" is used in the special case of a function returning nothing function bigHorribleAlert(): void { alert("I'm a little annoying box!"); } -//Functions are first class citizens, support the lambda "fat arrow" syntax and use type inference -//All examples are equivalent, the same signature will be infered by the compiler, and same JavaScript will be emitted +// Functions are first class citizens, support the lambda "fat arrow" syntax and +// use type inference + +// The following are equivalent, the same signature will be infered by the +// compiler, and same JavaScript will be emitted var f1 = function(i: number) : number { return i * i; } -var f2 = function(i: number) { return i * i; } //Return type infered +// Return type inferred +var f2 = function(i: number) { return i * i; } var f3 = (i : number) : number => { return i * i; } -var f4 = (i: number) => { return i * i; } //Return type infered -var f5 = (i: number) => i * i; //Return type infered, one-liner means no return keyword needed +// Return type inferred +var f4 = (i: number) => { return i * i; } +// Return type inferred, one-liner means no return keyword needed +var f5 = (i: number) => i * i; -//Interfaces are structural, anything that has the properties is compliant with the interface +// Interfaces are structural, anything that has the properties is compliant with +// the interface interface Person { name: string; - //Optional properties, marked with a "?" + // Optional properties, marked with a "?" age?: number; - //And of course functions + // And of course functions move(): void; } -//..Object that implements the "Person" interface -var p : Person = { name: "Bobby", move : () => {} }; //Can be treated as a Person since it has the name and age properties -//..Objects that have the optional property: +// Object that implements the "Person" interface +// Can be treated as a Person since it has the name and move properties +var p : Person = { name: "Bobby", move : () => {} }; +// Objects that have the optional property: var validPerson : Person = { name: "Bobby", age: 42, move: () => {} }; -var invalidPerson : Person = { name: "Bobby", age: true }; //Is not a person because age is not a number +// Is not a person because age is not a number +var invalidPerson : Person = { name: "Bobby", age: true }; -//..Interfaces can also describe a function type +// Interfaces can also describe a function type interface SearchFunc { (source: string, subString: string): boolean; } -//..Only the parameters' types are important, names are not important. +// Only the parameters' types are important, names are not important. var mySearch: SearchFunc; mySearch = function(src: string, sub: string) { return src.search(sub) != -1; } -//Classes - members are public by default +// Classes - members are public by default class Point { - //Properties - x: number; - - //Constructor - the public/private keywords in this context will generate the boiler plate code - // for the property and the initialization in the constructor. - // In this example, "y" will be defined just like "x" is, but with less code - //Default values are also supported - constructor(x: number, public y: number = 0) { - this.x = x; - } - - //Functions - dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - - //Static members - static origin = new Point(0, 0); + // Properties + x: number; + + // Constructor - the public/private keywords in this context will generate + // the boiler plate code for the property and the initialization in the + // constructor. + // In this example, "y" will be defined just like "x" is, but with less code + // Default values are also supported + + constructor(x: number, public y: number = 0) { + this.x = x; + } + + // Functions + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + + // Static members + static origin = new Point(0, 0); } var p1 = new Point(10 ,20); var p2 = new Point(25); //y will be 0 -//Inheritance +// Inheritance class Point3D extends Point { - constructor(x: number, y: number, public z: number = 0) { - super(x, y); //Explicit call to the super class constructor is mandatory - } - - //Overwrite - dist() { - var d = super.dist(); - return Math.sqrt(d * d + this.z * this.z); - } + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // Explicit call to the super class constructor is mandatory + } + + // Overwrite + dist() { + var d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } } -//Modules, "." can be used as separator for sub modules +// Modules, "." can be used as separator for sub modules module Geometry { export class Square { constructor(public sideLength: number = 0) { @@ -120,32 +131,32 @@ module Geometry { var s1 = new Geometry.Square(5); -//..Local alias for referencing a module +// Local alias for referencing a module import G = Geometry; var s2 = new G.Square(10); -//Generics -//..Classes +// Generics +// Classes class Tuple { constructor(public item1: T1, public item2: T2) { } } -//..Interfaces +// Interfaces interface Pair { - item1: T; - item2: T; + item1: T; + item2: T; } -//..And functions +// And functions var pairToTuple = function(p: Pair) { - return new Tuple(p.item1, p.item2); + return new Tuple(p.item1, p.item2); }; var tuple = pairToTuple({ item1:"hello", item2:"world"}); -//Including references to a definition file: +// Including references to a definition file: /// ``` -- cgit v1.2.3 From 394e7ecb84c68983f0bb210a8286cca0ff29e6d1 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 15:35:00 -0500 Subject: whitespace typo, fix #989 --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript.html.markdown b/typescript.html.markdown index 937ebda1..662af494 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -46,7 +46,7 @@ function bigHorribleAlert(): void { var f1 = function(i: number) : number { return i * i; } // Return type inferred var f2 = function(i: number) { return i * i; } -var f3 = (i : number) : number => { return i * i; } +var f3 = (i: number) : number => { return i * i; } // Return type inferred var f4 = (i: number) => { return i * i; } // Return type inferred, one-liner means no return keyword needed -- cgit v1.2.3 From 69480d51b82fdc7d2ad3d035c5744d85f56af807 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 16:58:18 -0500 Subject: fix spacing issue --- typescript.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/typescript.html.markdown b/typescript.html.markdown index 662af494..27a1f71a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -43,14 +43,14 @@ function bigHorribleAlert(): void { // The following are equivalent, the same signature will be infered by the // compiler, and same JavaScript will be emitted -var f1 = function(i: number) : number { return i * i; } +var f1 = function(i: number): number { return i * i; } // Return type inferred var f2 = function(i: number) { return i * i; } -var f3 = (i: number) : number => { return i * i; } +var f3 = (i: number): number => { return i * i; } // Return type inferred var f4 = (i: number) => { return i * i; } // Return type inferred, one-liner means no return keyword needed -var f5 = (i: number) => i * i; +var f5 = (i: number) => i * i; // Interfaces are structural, anything that has the properties is compliant with // the interface @@ -64,11 +64,11 @@ interface Person { // Object that implements the "Person" interface // Can be treated as a Person since it has the name and move properties -var p : Person = { name: "Bobby", move : () => {} }; +var p: Person = { name: "Bobby", move: () => {} }; // Objects that have the optional property: -var validPerson : Person = { name: "Bobby", age: 42, move: () => {} }; +var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; // Is not a person because age is not a number -var invalidPerson : Person = { name: "Bobby", age: true }; +var invalidPerson: Person = { name: "Bobby", age: true }; // Interfaces can also describe a function type interface SearchFunc { @@ -84,7 +84,7 @@ mySearch = function(src: string, sub: string) { class Point { // Properties x: number; - + // Constructor - the public/private keywords in this context will generate // the boiler plate code for the property and the initialization in the // constructor. @@ -94,10 +94,10 @@ class Point { constructor(x: number, public y: number = 0) { this.x = x; } - + // Functions dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - + // Static members static origin = new Point(0, 0); } @@ -110,7 +110,7 @@ class Point3D extends Point { constructor(x: number, y: number, public z: number = 0) { super(x, y); // Explicit call to the super class constructor is mandatory } - + // Overwrite dist() { var d = super.dist(); -- cgit v1.2.3 From c88c28058b34c84de853694eb71d402fee937168 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Fri, 13 Mar 2015 23:40:45 +0800 Subject: Closes #1000 --- c++.html.markdown | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/c++.html.markdown b/c++.html.markdown index 67fa054c..1978d183 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -30,10 +30,10 @@ one of the most widely-used programming languages. // C++ is _almost_ a superset of C and shares its basic syntax for // variable declarations, primitive types, and functions. -// However, C++ varies in some of the following ways: -// A main() function in C++ should return an int, -// though void main() is accepted by most compilers (gcc, clang, etc.) +// Just like in C, your program's entry point is a function called +// main with an integer return type, +// though void main() is also accepted by most compilers (gcc, clang, etc.) // This value serves as the program's exit status. // See http://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) @@ -51,6 +51,8 @@ int main(int argc, char** argv) return 0; } +// However, C++ varies in some of the following ways: + // In C++, character literals are one byte. sizeof('c') == 1 -- cgit v1.2.3 From 8f904480c02a28b966ef8827d71bae534778995f Mon Sep 17 00:00:00 2001 From: Keyan Zhang Date: Fri, 13 Mar 2015 14:04:20 -0400 Subject: detailed explanation of eq?, eqv?, and equal? References: 1. http://docs.racket-lang.org/reference/booleans.html 2. http://docs.racket-lang.org/reference/eval-model.html 3. https://groups.google.com/forum/#!topic/plt-scheme/T1k49HMl450 4. http://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme 5. http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html --- racket.html.markdown | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 6abc8759..e345db8b 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Eli Barzilay", "https://github.com/elibarzilay"] - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"] - ["Duong H. Nguyen", "https://github.com/cmpitg"] + - ["Keyan Zhang", "https://github.com/keyanzhang"] --- Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. @@ -282,16 +283,49 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;; for numbers use `=' (= 3 3.0) ; => #t -(= 2 1) ; => #f +(= 2 1) ; => #f + +;; `eq?' returns #t if 2 arguments refer to the same object (in memory), +;; #f otherwise. +;; In other words, it's a simple pointer comparison. +(eq? '() '()) ; => #t, since there exists only one empty list in memory +(let ([x '()] [y '()]) + (eq? x y)) ; => #t, same as above -;; for object identity use `eq?' -(eq? 3 3) ; => #t -(eq? 3 3.0) ; => #f (eq? (list 3) (list 3)) ; => #f +(let ([x (list 3)] [y (list 3)]) + (eq? x y)) ; => #f — not the same list in memory! + +(let* ([x (list 3)] [y x]) + (eq? x y)) ; => #t, since x and y now point to the same stuff + +(eq? 'yes 'yes) ; => #t +(eq? 'yes 'no) ; => #f + +(eq? 3 3) ; => #t — be careful here + ; It’s better to use `=' for number comparisons. +(eq? 3 3.0) ; => #f + +(eq? (expt 2 100) (expt 2 100)) ; => #f +(eq? (integer->char 955) (integer->char 955)) ; => #f + +(eq? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f + +;; `eqv?' supports the comparison of number and character datatypes. +;; for other datatypes, `eqv?' and `eq?' return the same result. +(eqv? 3 3.0) ; => #f +(eqv? (expt 2 100) (expt 2 100)) ; => #t +(eqv? (integer->char 955) (integer->char 955)) ; => #t + +(eqv? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f -;; for collections use `equal?' -(equal? (list 'a 'b) (list 'a 'b)) ; => #t -(equal? (list 'a 'b) (list 'b 'a)) ; => #f +;; `equal?' supports the comparison of the following datatypes: +;; strings, byte strings, pairs, mutable pairs, vectors, boxes, +;; hash tables, and inspectable structures. +;; for other datatypes, `equal?' and `eqv?' return the same result. +(equal? 3 3.0) ; => #f +(equal? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #t +(equal? (list 3) (list 3)) ; => #t ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 5. Control Flow -- cgit v1.2.3 From 6034688980162cc5099dd2701b88e449182862f7 Mon Sep 17 00:00:00 2001 From: Antonio Lima Date: Sun, 15 Mar 2015 21:17:52 -0400 Subject: No-space array notation type[] --- java.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java.html.markdown b/java.html.markdown index ebe11bd3..10dd498c 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -103,15 +103,15 @@ public class LearnJava { // Arrays //The array size must be decided upon instantiation //The following formats work for declaring an array - // [] = new []; + //[] = new []; // [] = new []; - int [] intArray = new int[10]; - String [] stringArray = new String[1]; - boolean boolArray [] = new boolean[100]; + int[] intArray = new int[10]; + String[] stringArray = new String[1]; + boolean boolArray[] = new boolean[100]; // Another way to declare & initialize an array - int [] y = {9000, 1000, 1337}; - String names [] = {"Bob", "John", "Fred", "Juan Pedro"}; + int[] y = {9000, 1000, 1337}; + String names[] = {"Bob", "John", "Fred", "Juan Pedro"}; boolean bools[] = new boolean[] {true, false, false}; // Indexing an array - Accessing an element -- cgit v1.2.3