mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-16 06:05:58 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
366fe47ffa
@ -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
|
// C++ is _almost_ a superset of C and shares its basic syntax for
|
||||||
// variable declarations, primitive types, and functions.
|
// variable declarations, primitive types, and functions.
|
||||||
// However, C++ varies in some of the following ways:
|
|
||||||
|
|
||||||
// A main() function in C++ should return an int,
|
// Just like in C, your program's entry point is a function called
|
||||||
// though void main() is accepted by most compilers (gcc, clang, etc.)
|
// 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.
|
// This value serves as the program's exit status.
|
||||||
// See http://en.wikipedia.org/wiki/Exit_status for more information.
|
// See http://en.wikipedia.org/wiki/Exit_status for more information.
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
@ -51,6 +51,8 @@ int main(int argc, char** argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// However, C++ varies in some of the following ways:
|
||||||
|
|
||||||
// In C++, character literals are one byte.
|
// In C++, character literals are one byte.
|
||||||
sizeof('c') == 1
|
sizeof('c') == 1
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ contributors:
|
|||||||
- ["Eli Barzilay", "https://github.com/elibarzilay"]
|
- ["Eli Barzilay", "https://github.com/elibarzilay"]
|
||||||
- ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
|
- ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
|
||||||
- ["Duong H. Nguyen", "https://github.com/cmpitg"]
|
- ["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.
|
Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family.
|
||||||
@ -284,14 +285,47 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
|
|||||||
(= 3 3.0) ; => #t
|
(= 3 3.0) ; => #t
|
||||||
(= 2 1) ; => #f
|
(= 2 1) ; => #f
|
||||||
|
|
||||||
;; for object identity use `eq?'
|
;; `eq?' returns #t if 2 arguments refer to the same object (in memory),
|
||||||
(eq? 3 3) ; => #t
|
;; #f otherwise.
|
||||||
(eq? 3 3.0) ; => #f
|
;; In other words, it's a simple pointer comparison.
|
||||||
(eq? (list 3) (list 3)) ; => #f
|
(eq? '() '()) ; => #t, since there exists only one empty list in memory
|
||||||
|
(let ([x '()] [y '()])
|
||||||
|
(eq? x y)) ; => #t, same as above
|
||||||
|
|
||||||
;; for collections use `equal?'
|
(eq? (list 3) (list 3)) ; => #f
|
||||||
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
|
(let ([x (list 3)] [y (list 3)])
|
||||||
(equal? (list 'a 'b) (list 'b 'a)) ; => #f
|
(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
|
||||||
|
|
||||||
|
;; `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
|
;; 5. Control Flow
|
||||||
|
@ -19,7 +19,7 @@ var isDone: boolean = false;
|
|||||||
var lines: number = 42;
|
var lines: number = 42;
|
||||||
var name: string = "Anders";
|
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;
|
var notSure: any = 4;
|
||||||
notSure = "maybe a string instead";
|
notSure = "maybe a string instead";
|
||||||
notSure = false; // okay, definitely a boolean
|
notSure = false; // okay, definitely a boolean
|
||||||
@ -33,20 +33,27 @@ var list: Array<number> = [1, 2, 3];
|
|||||||
enum Color {Red, Green, Blue};
|
enum Color {Red, Green, Blue};
|
||||||
var c: Color = Color.Green;
|
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 {
|
function bigHorribleAlert(): void {
|
||||||
alert("I'm a little annoying box!");
|
alert("I'm a little annoying box!");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Functions are first class citizens, support the lambda "fat arrow" syntax and use type inference
|
// Functions are first class citizens, support the lambda "fat arrow" syntax and
|
||||||
//All examples are equivalent, the same signature will be infered by the compiler, and same JavaScript will be emitted
|
// use type inference
|
||||||
var f1 = function(i: number) : number { return i * i; }
|
|
||||||
var f2 = function(i: number) { return i * i; } //Return type infered
|
|
||||||
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
|
|
||||||
|
|
||||||
//Interfaces are structural, anything that has the properties is compliant with the interface
|
// 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; }
|
||||||
|
// Return type inferred
|
||||||
|
var f2 = function(i: 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;
|
||||||
|
|
||||||
|
// Interfaces are structural, anything that has the properties is compliant with
|
||||||
|
// the interface
|
||||||
interface Person {
|
interface Person {
|
||||||
name: string;
|
name: string;
|
||||||
// Optional properties, marked with a "?"
|
// Optional properties, marked with a "?"
|
||||||
@ -55,17 +62,19 @@ interface Person {
|
|||||||
move(): void;
|
move(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
//..Object that implements the "Person" interface
|
// 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
|
// Can be treated as a Person since it has the name and move properties
|
||||||
//..Objects that have the optional property:
|
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: () => {} };
|
||||||
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 {
|
interface SearchFunc {
|
||||||
(source: string, subString: string): boolean;
|
(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;
|
var mySearch: SearchFunc;
|
||||||
mySearch = function(src: string, sub: string) {
|
mySearch = function(src: string, sub: string) {
|
||||||
return src.search(sub) != -1;
|
return src.search(sub) != -1;
|
||||||
@ -76,10 +85,12 @@ class Point {
|
|||||||
// Properties
|
// Properties
|
||||||
x: number;
|
x: number;
|
||||||
|
|
||||||
//Constructor - the public/private keywords in this context will generate the boiler plate code
|
// Constructor - the public/private keywords in this context will generate
|
||||||
// for the property and the initialization in the constructor.
|
// 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
|
// In this example, "y" will be defined just like "x" is, but with less code
|
||||||
// Default values are also supported
|
// Default values are also supported
|
||||||
|
|
||||||
constructor(x: number, public y: number = 0) {
|
constructor(x: number, public y: number = 0) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
}
|
}
|
||||||
@ -120,25 +131,25 @@ module Geometry {
|
|||||||
|
|
||||||
var s1 = new Geometry.Square(5);
|
var s1 = new Geometry.Square(5);
|
||||||
|
|
||||||
//..Local alias for referencing a module
|
// Local alias for referencing a module
|
||||||
import G = Geometry;
|
import G = Geometry;
|
||||||
|
|
||||||
var s2 = new G.Square(10);
|
var s2 = new G.Square(10);
|
||||||
|
|
||||||
// Generics
|
// Generics
|
||||||
//..Classes
|
// Classes
|
||||||
class Tuple<T1, T2> {
|
class Tuple<T1, T2> {
|
||||||
constructor(public item1: T1, public item2: T2) {
|
constructor(public item1: T1, public item2: T2) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//..Interfaces
|
// Interfaces
|
||||||
interface Pair<T> {
|
interface Pair<T> {
|
||||||
item1: T;
|
item1: T;
|
||||||
item2: T;
|
item2: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
//..And functions
|
// And functions
|
||||||
var pairToTuple = function<T>(p: Pair<T>) {
|
var pairToTuple = function<T>(p: Pair<T>) {
|
||||||
return new Tuple(p.item1, p.item2);
|
return new Tuple(p.item1, p.item2);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user