From 4c1a57714b6ee0db2ed93dd04811333e2c4de326 Mon Sep 17 00:00:00 2001 From: eugene Date: Wed, 3 Jul 2013 11:57:52 -0400 Subject: added Objective-C --- objvectiv-C.html.markdown | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 objvectiv-C.html.markdown diff --git a/objvectiv-C.html.markdown b/objvectiv-C.html.markdown new file mode 100644 index 00000000..02975eb6 --- /dev/null +++ b/objvectiv-C.html.markdown @@ -0,0 +1,67 @@ +--- +language: Objectiv-C +author: Eugene Yagrushkin +author_url: www.about.me/yagrushkin +filename: learnc.Objectiv-C +--- + +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. +It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. + +```Objective-C +// Single-line comments start with // + +/* +Multi-line comments look like this. +*/ + +// Import headers with #import +#import +#import "SomeAppDelegate.h" + +// Declare your class in a header(.h) file: + +@interface UserObject : NSObject{ +// instance variables +} + +// Class method + + (NSString*) ClassMethod; + +// Instance method + - (NSString*) instanceMethodWithParmeter:(NSString*)string; + +@end + +// Add class methods in an implementation (.m) file: + +@implementation UserObject + ++ (NSString*) ClassMethod{ + return @"SomeString"; +} + +- (NSString*) instanceMethodWithParmeter:(NSString*)string; +{ + return [NSString stringWithString:string]; +} + +@end + +// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. +UserObject *someObject = [[UserObject alloc] init]; + +// The Objective-C model of object-oriented programming is based on message passing to object instances. +// In Objective-C one does not simply call a method; one sends a message. + +[someObject instanceMethodWithParmeter@"Steve Jobs"]; + + +``` +## Further Reading + +[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) + +[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) + +[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 3f814e58e9ab19348a985cf02e1fbdac407f3a6d Mon Sep 17 00:00:00 2001 From: Eugene Yagrushkin Date: Wed, 10 Jul 2013 14:20:46 -0400 Subject: Update objvectiv-C.html.markdown --- objvectiv-C.html.markdown | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/objvectiv-C.html.markdown b/objvectiv-C.html.markdown index 02975eb6..787a9219 100644 --- a/objvectiv-C.html.markdown +++ b/objvectiv-C.html.markdown @@ -15,10 +15,32 @@ It's is a general-purpose, object-oriented programming language that adds Smallt Multi-line comments look like this. */ +##Basic types +// all the primitive variable types are the same as in C +// char, int, long, double, float + + +// Simple, common classes +// number +NSNumber *firstNumber = @1; +NSNumber *secondNumber = @23.0; +NSNumber *boolNumber = @YES; + +// string +NSString *aString = @"some string"; + +// array +NSArray *array = @[ @1, @2]; + +// dictionary +NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; + // Import headers with #import #import #import "SomeAppDelegate.h" +##Coding classes + // Declare your class in a header(.h) file: @interface UserObject : NSObject{ @@ -43,19 +65,29 @@ Multi-line comments look like this. - (NSString*) instanceMethodWithParmeter:(NSString*)string; { - return [NSString stringWithString:string]; + return @"New string"; } +- (NSString*) otherMethodWithString:(NSString*)string; +{ + return [NSString stringWithString:string]; +} @end // Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. UserObject *someObject = [[UserObject alloc] init]; +##Calling Methods + // The Objective-C model of object-oriented programming is based on message passing to object instances. // In Objective-C one does not simply call a method; one sends a message. -[someObject instanceMethodWithParmeter@"Steve Jobs"]; +[someObject instanceMethodWithParmeter:@"Steve Jobs"]; + +##Nested Messages +// nested messages look like this: +[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; ``` ## Further Reading -- cgit v1.2.3 From 082dffc69714418456966bd7932f8e1b1fc8fcb0 Mon Sep 17 00:00:00 2001 From: pdn Date: Sun, 11 Aug 2013 01:49:33 -0700 Subject: Adding Common Lisp --- common-lisp.html.markdown | 531 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 531 insertions(+) create mode 100644 common-lisp.html.markdown diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown new file mode 100644 index 00000000..757b6a14 --- /dev/null +++ b/common-lisp.html.markdown @@ -0,0 +1,531 @@ +--- + +language: commonlisp +filename: commonlisp.lisp +contributors: + - ["Paul Nathan", "https://github.com/pnathan"] +--- + +ANSI Common Lisp is a general purpose, multi-paradigm programming +language suited for a wide variety of industry applications. It is +frequently referred to a programmable programming language. + +The classic starting point is [Practical Common Lisp and freely available.](http://www.gigamonkeys.com/book/) + +Another popular and recent book is +[Land of Lisp](http://landoflisp.com/). + + + +```commonlisp + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; 0. Syntax +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; General form. + +;; Lisp has two fundamental pieces of syntax: the ATOM and the +;; S-expression. Typically, grouped S-expressions are called `forms`. + +10 ; an atom; it evaluates to itself + +:THING ;Another atom; evaluating to the symbol :thing. + +t ; another atom, denoting true. + +(+ 1 2 3 4) ; an s-expression + +'(4 :foo t) ;another one + + +;;; Comments + +;; Single line comments start with a semicolon; use two for normal +;; comments, three for section comments, and four for file-level +;; comments. + +#| Block comments + can span multiple lines and... + #| + they can be nested! + |# +|# + +;;; Environment. + +;; A variety of implementations exist; most are +;; standard-conformant. CLISP is a good starting one. + +;; Libraries are managed through Quicklisp.org's Quicklisp system. + +;; Common Lisp is usually developed with a text editor and a REPL +;; (Read Evaluate Print Loop) running at the same time. The REPL +;; allows for interactive exploration of the program as it is "live" +;; in the system. + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; 1. Primitive Datatypes and Operators +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; Symbols + +'foo ; => FOO + +(intern "AAAA") ; => AAAA + +;;; Numbers +9999999999999999999999 ; integers +#b111 ; binary => 7 +#o111 ; octal => 73 +#x111 ; hexadecimal => 273 +3.14159 ; floating point +1/2 ; ratios +#C(1 2) ; complex numbers + + +;; Function application is written (f x y z ...) +;; where f is a function and x, y, z, ... are operands +;; If you want to create a literal list of data, use ' to stop it from +;; being evaluated - literally, "quote" the data. +'(+ 1 2) ; => (+ 1 2) +;; You can also call a function manually: +(funcall #'+ 1 2 3) ; => 6 +;; Some arithmetic operations +(+ 1 1) ; => 2 +(- 8 1) ; => 7 +(* 10 2) ; => 20 +(expt 2 3) ; => 8 +(mod 5 2) ; => 1 +(/ 35 5) ; => 7 +(/ 1 3) ; => 1/3 +(+ #C(1 2) #C(6 -4)) ; => #C(7 -2) + +;;; Booleans +t ; for true (any not-nil value is true) +nil ; for false +(not nil) ; => t +(and 0 t) ; => t +(or 0 nil) ; => 0 + +;;; Characters +#\A ; => #\A +#\λ ; => #\GREEK_SMALL_LETTER_LAMDA +#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA + +;;; Strings are fixed-length simple-arrays of characters. +"Hello, world!" +"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character + +;; Strings can be concatenated too! +(concatenate 'string "Hello " "world!") ; => "Hello world!" + +;; A string can be treated like a list of characters +(elt "Apple" 0) ; => #\A + +;; format can be used to format strings: +(format nil "~a can be ~a" "strings" "formatted") + +;; Printing is pretty easy +(format t "Common Lisp is groovy. Dude.\n") + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 2. Variables +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; You can create a global (dynamically scoped) using defparameter +;; a variable name can use any character except: ()[]{}",'`;#|\ +(defparameter *some-var* 5) +*some-var* ; => 5 + +;; You can also use unicode characters. Not very easy to use though... +(defparameter *foo#\u03BBooo* nil) + + +;; Accessing a previously unassigned variable is an undefined +;; behavior (but possible). Don't do it. + +;; Local binding: `me` is bound to "dance with you" only within the +;; (let ...). Let always returns the value of the last `form` in the +;; let form. + +(let ((me "dance with you")) + me) +;; => "dance with you" + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. Structs and Collections +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Structs +(defstruct dog name breed age) +(defparameter *rover* + (make-dog :name "rover" + :breed "collie" + :age 5)) +*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5) +(dog-p *rover*) ; => t ;; ewww) +(dog-name *rover*) ; => "rover" + +;;; Pairs +;; `cons' constructs pairs, `car' and `cdr' extract the first +;; and second elements +(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB) +(car (cons 'SUBJECT 'VERB)) ; => SUBJECT +(cdr (cons 'SUBJECT 'VERB)) ; => VERB + +;;; Lists + +;; Lists are linked-list data structures, made of `cons' pairs and end +;; with a `nil' (or '()) to mark the end of the list +(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3) +;; `list' is a convenience variadic constructor for lists +(list 1 2 3) ; => '(1 2 3) +;; and a quote can also be used for a literal list value +'(1 2 3) ; => '(1 2 3) + +;; Can still use `cons' to add an item to the beginning of a list +(cons 4 '(1 2 3)) ; => '(4 1 2 3) + +;; Use `append' to add lists together +(append '(1 2) '(3 4)) ; => '(1 2 3 4) + +;; Lists are a very basic type, so there is a wide variety of functionality for +;; them, a few examples: +(mapcar #1+ '(1 2 3)) ; => '(2 3 4) +(mapcar #'+ '(1 2 3) '(10 20 30)) ; => '(11 22 33) +(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4) +(every #'evenp '(1 2 3 4)) ; => nil +(some #'oddp '(1 2 3 4)) ; => T +(butlast '(subject verb object)) ; => (SUBJECT VERB) + + +;;; Vectors + +;; Vectors are fixed-length arrays +#(1 2 3) ; => #(1 2 3) + +;; Use concatenate to add vectors together +(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) + +;;; Arrays + +;; Both vectors and strings are special-cases of arrays. + +;; 2D arrays + +(make-array (list 2 2)) + +; => #2A((0 0) (0 0)) + +(make-array (list 2 2 2)) + +; => #3A(((0 0) (0 0)) ((0 0) (0 0))) + + +; access the element at 1,1,1, +(aref (make-array (list 2 2 2)) 1 1 1) + +; => 0 + +;;; Sets are just lists: + +(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1) +(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4 +(union '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1 4 5 6 7) +(adjoin 4 '(1 2 3 4)) ; => (1 2 3 4) + +;;; Dictionaries are implemented as hash tables. + +;; Create a hash table +(defparameter m (hash-table)) + +;; set a value +(setf (gethash 'a hash-table 1)) + +;; Retrieve a value +(gethash 'a m) ; => 1 + +;; Retrieving a non-present value returns a nil + (gethash m 'd) ;=> nil + +;; You can provide a default value for missing keys +(gethash m 'd :not-found) ; => :NOT-FOUND + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. Functions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Use `lambda' to create anonymous functions. +;; A function always returns the value of its last expression +(lambda () "Hello World") ; => # + +;; Use funcall to call lambda functions +(funcall (lambda () "Hello World")) ; => "Hello World" + +;; De-anonymize the function +(defun hello-world () "Hello World") +(hello-world) ; => "Hello World" + +;; The () in the above is the list of arguments for the function +(defun hello (name) + (format nil "Hello, ~a " name)) +(hello "Steve") ; => "Hello, Steve" + +;; Functions can have optional arguments; they default to nil + +(defun hello (name &optional from) + (if from + (format t "Hello, ~a, from ~a" name from) + (format t "Hello, ~a" name))) + + (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas + +;; And the defaults can be set... +(defun hello (name &optional (from "The world")) + (format t "Hello, ~a, from ~a" name from)) + + +;; And of course, keywords are allowed as well... usually more +;; flexible than &optional. + +(defun generalized-greeter (name &key (from "the world") (honorific "Mx")) + (format t "Hello, ~a ~a, from ~a" honorific name from)) + +(generalized-greeter "Jim") ; => Hello, Mx Jim, from the world + +(generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr") +; => Hello, Mr Jim, from the alpacas you met last summer + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 4. Equality +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Common Lisp has a sophisticated equality system. + +;; for numbers use `=' +(= 3 3.0) ; => t +(= 2 1) ; => nil + +;; for object identity (approximately) use `eq?' +(eql 3 3) ; => t +(eql 3 3.0) ; => nil +(eql (list 3) (list 3)) ; => nil + +;; for collections use `equal' +(equal (list 'a 'b) (list 'a 'b)) ; => t +(equal (list 'a 'b) (list 'b 'a)) ; => nil + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 5. Control Flow +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; Conditionals + +(if t ; test expression + "this is true" ; then expression + "this is false") ; else expression +; => "this is true" + +;; In conditionals, all non-nil values are treated as true +(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO) +(if (member 'Groucho '(Harpo Groucho Zeppo)) + 'yep + 'nope) +; => 'YEP + +;; `cond' chains a series of tests to select a result +(cond ((> 2 2) (error "wrong!")) + ((< 2 2) (error "wrong again!")) + (t 'ok)) ; => 'OK + +;; Typecase switches on the type of the value +(typecase 1 + (string :string) + (integer :int)) + +; => :int + +;;; Iteration + +;; Of course recursion is supported: + +(defun walker (n) + (if (= n 0) + :walked + (walker (1- n)))) + +(walker) ; => :walked + +;; Most of the time, we use DOLIST or LOOP + + +(dolist (i '(1 2 3 4)) + (format t "~a" i)) + +; => 1234 + +(loop for i from 0 below 10 + collect i) + +; => (0 1 2 3 4 5 6 7 8 9) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 6. Mutation +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Use `setf' to assign a new value to an existing variable. This was +;; demonstrated earlier in the hash table example. + +(let ((variable 10)) + (setf variable 10)) + ; => 10 + + +;; Good Lisp style is to minimize destructive functions and to avoid +;; mutation when reasonable. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 7. Classes and Objects +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; No more Animal classes, let's have Human-Powered Mechanical +;; Conveyances. + +(defclass human-powered-conveyance () + ((velocity + :accessor velocity + :initarg :velocity) + (average-efficiency + :accessor average-efficiency) + :initarg :average-efficiency) + (:documentation "A human powered conveyance")) + +(defclass bicycle (human-powered-conveyance) + ((wheel-size + :accessor wheel-size + :initarg :wheel-size + :documentation "Diameter of the wheel.") + (height + :accessor height + :initarg :height))) + +(defclass recumbent (bicycle) + ((chain-type + :accessor chain-type + :initarg :chain-type))) + +(defclass unicycle (human-powered-conveyance) nil) + +(defclass canoe (human-powered-conveyance) + ((number-of-rowers + :accessor number-of-rowers + :initarg :number-of-rowers))) + +;; Calling DESCRIBE on the human-powered-conveyance class in the REPL gives: + +(describe 'human-powered-conveyance) + +; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE +; [symbol] +; +; HUMAN-POWERED-CONVEYANCE names the standard-class #: +; Documentation: +; A human powered conveyance +; Direct superclasses: STANDARD-OBJECT +; Direct subclasses: UNICYCLE, BICYCLE, CANOE +; Not yet finalized. +; Direct slots: +; VELOCITY +; Readers: VELOCITY +; Writers: (SETF VELOCITY) +; AVERAGE-EFFICIENCY +; Readers: AVERAGE-EFFICIENCY +; Writers: (SETF AVERAGE-EFFICIENCY) + +;; Note the reflective behavior available to you! Common Lisp is +;; designed to be an interactive system + +;; To define a method, let's find out what our circumference of the +;; bike turns out to be using the equation: C = d * pi + +(defmethod circumference ((object bicycle)) + (* 3.14159 (wheel-size object))) + +;; Let's suppose we find out that the efficiency value of the number +;; of rowers in a canoe is roughly logarithmic. This should probably be set +;; in the constructor/initializer. + +;; Here's how to initialize your instance after Common Lisp gets done +;; constructing it: + +(defmethod initialize-instance :after ((object canoe) &rest args) + (setf (average-efficiency object) (log (1+ (number-of-rowers object))))) + +;; Then to construct an instance and check the average efficiency... + +(average-efficiency (make-instance 'canoe :number-of-rowers 15)) +; => 2.7725887 + + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 8. Macros +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Macros let you extend the syntax of the language + +;; Common Lisp doesn't come with a WHILE loop- let's add one. +;; If we obey our assembler instincts, we wind up with: + +(defmacro while (condition &body body) + "While `condition` is true, `body` is executed. + +`condition` is tested prior to each execution of `body`" + (let ((block-name (gensym))) + `(tagbody + (when (not ,condition) + (go ,block-name)) + (progn + ,@body) + ,block-name))) + +;; Let's look at the high-level version of this: + + +(defmacro while (condition &body body) + "While `condition` is true, `body` is executed. + +`condition` is tested prior to each execution of `body`" + `(loop while ,condition + do + ,@body)) + +;; However, with a modern compiler, this is not required; the LOOP +;; form compiles equally well and is easier to read. + +;; Note that ` is used, as well as , and @. ` is a quote-type operator +;; known as quasiquote; it allows the use of ,. , allows "unquoting" +;; variables. @ interpolates lists. + +;; Gensym creates a unique symbol guaranteed to not exist elsewhere in +;; the system. This is because macros are expanded at compile time and +;; variables declared in the macro can collide with variables used in +;; regular code. + +;; See Practical Common Lisp for more information on macros. + + +## Further Reading + +[Keep moving on to the Practical Common Lisp book.](http://www.gigamonkeys.com/book/) + + +## Credits. + +Lots of thanks to the Scheme people for rolling up a great starting +point which could be easily moved to Common Lisp. -- cgit v1.2.3 From f39aa6adfb98086e98773f7871c3ba70f97c5771 Mon Sep 17 00:00:00 2001 From: pdn Date: Sun, 11 Aug 2013 10:39:13 -0700 Subject: Fixes based on pkh's comments. --- common-lisp.html.markdown | 251 ++++++++++++++++++++++++++++++---------------- 1 file changed, 162 insertions(+), 89 deletions(-) diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index 757b6a14..9f2c9957 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -71,16 +71,21 @@ t ; another atom, denoting true. ;;; Symbols -'foo ; => FOO +'foo ; => FOO Notice that the symbol is upper-cased automatically. + +;; Intern manually creates a symbol from a string. (intern "AAAA") ; => AAAA +(intern "aaa") ; => |aaa| + ;;; Numbers 9999999999999999999999 ; integers #b111 ; binary => 7 #o111 ; octal => 73 #x111 ; hexadecimal => 273 -3.14159 ; floating point +3.14159s0 ; single +3.14159d0 ; double 1/2 ; ratios #C(1 2) ; complex numbers @@ -93,42 +98,42 @@ t ; another atom, denoting true. ;; You can also call a function manually: (funcall #'+ 1 2 3) ; => 6 ;; Some arithmetic operations -(+ 1 1) ; => 2 -(- 8 1) ; => 7 -(* 10 2) ; => 20 -(expt 2 3) ; => 8 -(mod 5 2) ; => 1 -(/ 35 5) ; => 7 -(/ 1 3) ; => 1/3 +(+ 1 1) ; => 2 +(- 8 1) ; => 7 +(* 10 2) ; => 20 +(expt 2 3) ; => 8 +(mod 5 2) ; => 1 +(/ 35 5) ; => 7 +(/ 1 3) ; => 1/3 (+ #C(1 2) #C(6 -4)) ; => #C(7 -2) -;;; Booleans -t ; for true (any not-nil value is true) -nil ; for false -(not nil) ; => t -(and 0 t) ; => t -(or 0 nil) ; => 0 + ;;; Booleans +t ; for true (any not-nil value is true) +nil ; for false - and the empty list +(not nil) ; => t +(and 0 t) ; => t +(or 0 nil) ; => 0 -;;; Characters -#\A ; => #\A -#\λ ; => #\GREEK_SMALL_LETTER_LAMDA -#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA + ;;; Characters +#\A ; => #\A +#\λ ; => #\GREEK_SMALL_LETTER_LAMDA +#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA -;;; Strings are fixed-length simple-arrays of characters. +;;; Strings are fixed-length arrays of characters. "Hello, world!" "Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character ;; Strings can be concatenated too! (concatenate 'string "Hello " "world!") ; => "Hello world!" -;; A string can be treated like a list of characters +;; A string can be treated like a sequence of characters (elt "Apple" 0) ; => #\A ;; format can be used to format strings: (format nil "~a can be ~a" "strings" "formatted") -;; Printing is pretty easy -(format t "Common Lisp is groovy. Dude.\n") +;; Printing is pretty easy; ~% is the format specifier for newline. +(format t "Common Lisp is groovy. Dude.~%") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -136,22 +141,26 @@ nil ; for false ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; You can create a global (dynamically scoped) using defparameter ;; a variable name can use any character except: ()[]{}",'`;#|\ + +;; Dynamically scoped variables should have earmuffs in their name! + (defparameter *some-var* 5) *some-var* ; => 5 -;; You can also use unicode characters. Not very easy to use though... -(defparameter *foo#\u03BBooo* nil) +;; You can also use unicode characters. +(defparameter *AΛB* nil) -;; Accessing a previously unassigned variable is an undefined -;; behavior (but possible). Don't do it. +;; Accessing a previously unbound variable is an +;; undefined behavior (but possible). Don't do it. + ;; Local binding: `me` is bound to "dance with you" only within the ;; (let ...). Let always returns the value of the last `form` in the ;; let form. (let ((me "dance with you")) - me) + me) ;; => "dance with you" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -165,9 +174,12 @@ nil ; for false :breed "collie" :age 5)) *rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5) + (dog-p *rover*) ; => t ;; ewww) (dog-name *rover*) ; => "rover" +;; Dog-p, make-dog, and dog-name are all created by defstruct! + ;;; Pairs ;; `cons' constructs pairs, `car' and `cdr' extract the first ;; and second elements @@ -188,17 +200,21 @@ nil ; for false ;; Can still use `cons' to add an item to the beginning of a list (cons 4 '(1 2 3)) ; => '(4 1 2 3) -;; Use `append' to add lists together +;; Use `append' to - surprisingly - append lists together (append '(1 2) '(3 4)) ; => '(1 2 3 4) -;; Lists are a very basic type, so there is a wide variety of functionality for +;; Or use concatenate - + +(concatenate + +;; Lists are a very central type, so there is a wide variety of functionality for ;; them, a few examples: -(mapcar #1+ '(1 2 3)) ; => '(2 3 4) -(mapcar #'+ '(1 2 3) '(10 20 30)) ; => '(11 22 33) -(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4) -(every #'evenp '(1 2 3 4)) ; => nil -(some #'oddp '(1 2 3 4)) ; => T -(butlast '(subject verb object)) ; => (SUBJECT VERB) +(mapcar #'1+ '(1 2 3)) ; => '(2 3 4) +(mapcar #'+ '(1 2 3) '(10 20 30)) ; => '(11 22 33) +(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4) +(every #'evenp '(1 2 3 4)) ; => nil +(some #'oddp '(1 2 3 4)) ; => T +(butlast '(subject verb object)) ; => (SUBJECT VERB) ;;; Vectors @@ -217,60 +233,96 @@ nil ; for false (make-array (list 2 2)) +;; (make-array '(2 2)) works as well. + ; => #2A((0 0) (0 0)) (make-array (list 2 2 2)) ; => #3A(((0 0) (0 0)) ((0 0) (0 0))) +;; Caution- the default initial values are +;; implementation-defined. Here's how to define them: -; access the element at 1,1,1, +(make-array '(2) :initial-element 'unset) + +; => #(UNSET UNSET) + +;; And, to access the element at 1,1,1 - (aref (make-array (list 2 2 2)) 1 1 1) ; => 0 -;;; Sets are just lists: +;;; Naively, sets are just lists: (set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1) (intersection '(1 2 3 4) '(4 5 6 7)) ; => 4 (union '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1 4 5 6 7) (adjoin 4 '(1 2 3 4)) ; => (1 2 3 4) +;; But you'll want to use a better data structure than a linked list +;; for performant work! + ;;; Dictionaries are implemented as hash tables. ;; Create a hash table -(defparameter m (hash-table)) +(defparameter *m* (make-hash-table)) ;; set a value -(setf (gethash 'a hash-table 1)) +(setf (gethash 'a *m*) 1) ;; Retrieve a value -(gethash 'a m) ; => 1 +(gethash 'a *m*) ; => 1, t -;; Retrieving a non-present value returns a nil - (gethash m 'd) ;=> nil +;; Detail - Common Lisp has multiple return values possible. gethash +;; returns t in the second value if anything was found, and nil if +;; not. + +;; Retrieving a non-present value returns nil + (gethash *m* 'd) ;=> nil, nil ;; You can provide a default value for missing keys -(gethash m 'd :not-found) ; => :NOT-FOUND +(gethash *m* 'd :not-found) ; => :NOT-FOUND + +;; Let's handle the multiple return values here in code. + +(multiple-value-bind + (a b) + (gethash 'd *m*) + (list a b)) +; => (NIL NIL) + +(multiple-value-bind + (a b) + (gethash 'a *m*) + (list a b)) +; => (1 T) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Use `lambda' to create anonymous functions. -;; A function always returns the value of its last expression -(lambda () "Hello World") ; => # +;; A function always returns the value of its last expression. +;; The exact printable representation of a function will vary... + +(lambda () "Hello World") ; => # ;; Use funcall to call lambda functions (funcall (lambda () "Hello World")) ; => "Hello World" +;; Or Apply +(apply (lambda () "Hello World") nil) ; => "Hello World" + ;; De-anonymize the function -(defun hello-world () "Hello World") +(defun hello-world () + "Hello World") (hello-world) ; => "Hello World" ;; The () in the above is the list of arguments for the function (defun hello (name) - (format nil "Hello, ~a " name)) + (format nil "Hello, ~a " name)) + (hello "Steve") ; => "Hello, Steve" ;; Functions can have optional arguments; they default to nil @@ -286,6 +338,12 @@ nil ; for false (defun hello (name &optional (from "The world")) (format t "Hello, ~a, from ~a" name from)) +(hello "Steve") +; => Hello, Steve, from The world + +(hello "Steve" "the alpacas") +; => Hello, Steve, from the alpacas + ;; And of course, keywords are allowed as well... usually more ;; flexible than &optional. @@ -302,18 +360,18 @@ nil ; for false ;; 4. Equality ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Common Lisp has a sophisticated equality system. +;; Common Lisp has a sophisticated equality system. A couple are covered yere. ;; for numbers use `=' (= 3 3.0) ; => t (= 2 1) ; => nil -;; for object identity (approximately) use `eq?' +;; for object identity (approximately) use `eql` (eql 3 3) ; => t (eql 3 3.0) ; => nil (eql (list 3) (list 3)) ; => nil -;; for collections use `equal' +;; for lists, strings, and bit-vectors use `equal' (equal (list 'a 'b) (list 'a 'b)) ; => t (equal (list 'a 'b) (list 'b 'a)) ; => nil @@ -342,8 +400,8 @@ nil ; for false ;; Typecase switches on the type of the value (typecase 1 - (string :string) - (integer :int)) + (string :string) + (integer :int)) ; => :int @@ -352,9 +410,9 @@ nil ; for false ;; Of course recursion is supported: (defun walker (n) - (if (= n 0) - :walked - (walker (1- n)))) + (if (zerop 0) + :walked + (walker (1- n)))) (walker) ; => :walked @@ -362,12 +420,12 @@ nil ; for false (dolist (i '(1 2 3 4)) - (format t "~a" i)) + (format t "~a" i)) ; => 1234 (loop for i from 0 below 10 - collect i) + collect i) ; => (0 1 2 3 4 5 6 7 8 9) @@ -380,8 +438,8 @@ nil ; for false ;; demonstrated earlier in the hash table example. (let ((variable 10)) - (setf variable 10)) - ; => 10 + (setf variable 2)) + ; => 2 ;; Good Lisp style is to minimize destructive functions and to avoid @@ -395,34 +453,44 @@ nil ; for false ;; Conveyances. (defclass human-powered-conveyance () - ((velocity - :accessor velocity - :initarg :velocity) - (average-efficiency - :accessor average-efficiency) - :initarg :average-efficiency) - (:documentation "A human powered conveyance")) + ((velocity + :accessor velocity + :initarg :velocity) + (average-efficiency + :accessor average-efficiency) + :initarg :average-efficiency) + (:documentation "A human powered conveyance")) + +;; defclass, followed by name, followed by the superclass list, +;; followed by slot list, followed by optional qualities such as +;; :documentation. + +;; When no superclass list is set, the empty list defaults to the +;; standard-object class. This *can* be changed, but not until you +;; know what you're doing. Look up the Art of the Metaobject Protocol +;; for more information. (defclass bicycle (human-powered-conveyance) - ((wheel-size - :accessor wheel-size - :initarg :wheel-size - :documentation "Diameter of the wheel.") - (height - :accessor height - :initarg :height))) + ((wheel-size + :accessor wheel-size + :initarg :wheel-size + :documentation "Diameter of the wheel.") + (height + :accessor height + :initarg :height))) (defclass recumbent (bicycle) - ((chain-type - :accessor chain-type - :initarg :chain-type))) + ((chain-type + :accessor chain-type + :initarg :chain-type))) (defclass unicycle (human-powered-conveyance) nil) (defclass canoe (human-powered-conveyance) - ((number-of-rowers - :accessor number-of-rowers - :initarg :number-of-rowers))) + ((number-of-rowers + :accessor number-of-rowers + :initarg :number-of-rowers))) + ;; Calling DESCRIBE on the human-powered-conveyance class in the REPL gives: @@ -438,7 +506,7 @@ nil ; for false ; Direct superclasses: STANDARD-OBJECT ; Direct subclasses: UNICYCLE, BICYCLE, CANOE ; Not yet finalized. -; Direct slots: +(defparameter *foo#\u03BBooo* nil) ; Direct slots: ; VELOCITY ; Readers: VELOCITY ; Writers: (SETF VELOCITY) @@ -450,14 +518,16 @@ nil ; for false ;; designed to be an interactive system ;; To define a method, let's find out what our circumference of the -;; bike turns out to be using the equation: C = d * pi +;; bike wheel turns out to be using the equation: C = d * pi (defmethod circumference ((object bicycle)) - (* 3.14159 (wheel-size object))) + (* pi (wheel-size object))) + +;; pi is defined in Lisp already for us! ;; Let's suppose we find out that the efficiency value of the number -;; of rowers in a canoe is roughly logarithmic. This should probably be set -;; in the constructor/initializer. +;; of rowers in a canoe is roughly logarithmic. This should probably be set +;; in the constructor/initializer. ;; Here's how to initialize your instance after Common Lisp gets done ;; constructing it: @@ -488,7 +558,7 @@ nil ; for false `condition` is tested prior to each execution of `body`" (let ((block-name (gensym))) `(tagbody - (when (not ,condition) + (unless ,condition (go ,block-name)) (progn ,@body) @@ -503,13 +573,14 @@ nil ; for false `condition` is tested prior to each execution of `body`" `(loop while ,condition do - ,@body)) + (progn + ,@body))) ;; However, with a modern compiler, this is not required; the LOOP ;; form compiles equally well and is easier to read. -;; Note that ` is used, as well as , and @. ` is a quote-type operator -;; known as quasiquote; it allows the use of ,. , allows "unquoting" +;; Note that ``` is used, as well as `,` and `@`. ``` is a quote-type operator +;; known as quasiquote; it allows the use of `,` . `,` allows "unquoting" ;; variables. @ interpolates lists. ;; Gensym creates a unique symbol guaranteed to not exist elsewhere in @@ -529,3 +600,5 @@ nil ; for false Lots of thanks to the Scheme people for rolling up a great starting point which could be easily moved to Common Lisp. + +- [Paul Khoung](https://github.com/pkhuong) for some great reviewing. -- cgit v1.2.3 From fa910a4bc9044b4ac60c57e568fcd265657f2bcc Mon Sep 17 00:00:00 2001 From: pdn Date: Sun, 11 Aug 2013 10:40:51 -0700 Subject: Foolishness borne of haste --- common-lisp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index 9f2c9957..24b6947f 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -410,7 +410,7 @@ nil ; for false - and the empty list ;; Of course recursion is supported: (defun walker (n) - (if (zerop 0) + (if (zerop n) :walked (walker (1- n)))) -- cgit v1.2.3 From 7f4b100fa813f71879fbefaec6c4cabe35ce719a Mon Sep 17 00:00:00 2001 From: Severin Schoepke Date: Mon, 12 Aug 2013 16:17:37 +0200 Subject: Fixed broken links --- ruby-ecosystem.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index a31f552d..cae55cd3 100644 --- a/ruby-ecosystem.html.markdown +++ b/ruby-ecosystem.html.markdown @@ -93,13 +93,13 @@ which MRI version to target. ## RubySpec -Most ruby implementations rely heavily on (RubySpec)[http://rubyspec.org/]. Ruby +Most ruby implementations rely heavily on [RubySpec](http://rubyspec.org/). Ruby has no official specification, so the community has written executable specs in ruby to test their implementations' compatability with MRI. ## RubyGems -(RubyGems)[http://rubygems.org/] is a community-run package manager for ruby. +[RubyGems](http://rubygems.org/) is a community-run package manager for ruby. RubyGems ships with ruby, so there is no need to download it separately. Ruby packages are called "gems," and they can be hosted by the community at @@ -108,7 +108,7 @@ things like version, dependencies, author(s), and license(s). ## Bundler -(Bundler)[http://bundler.io/] is a gem dependency resolver. It uses a project's +[Bundler](http://bundler.io/) is a gem dependency resolver. It uses a project's Gemfile to find dependencies, and then fetches those dependencies' dependencies recursively. It does this until all dependencies are resolved and downloaded, or it will stop if a conflict has been found. -- cgit v1.2.3 From 46ce57a23cfc9518c79c264ba867c6025ca54bdd Mon Sep 17 00:00:00 2001 From: lyuehh Date: Mon, 12 Aug 2013 22:56:50 +0800 Subject: Add Chinese Translation for racket --- zh-cn/racket-cn.html.markdown | 607 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 607 insertions(+) create mode 100644 zh-cn/racket-cn.html.markdown diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown new file mode 100644 index 00000000..9370d300 --- /dev/null +++ b/zh-cn/racket-cn.html.markdown @@ -0,0 +1,607 @@ +--- + +language: racket +filename: learnracket.rkt +contributors: + - ["th3rac25", "https://github.com/voila"] + - ["Eli Barzilay", "https://github.com/elibarzilay"] + - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"] +translators: + - ["lyuehh", "https://github.com/lyuehh"] +--- + +Racket是Lisp/Scheme家族中的一个通用的,多范式的编程语言。 +非常期待您的反馈!你可以通过[@th3rac25](http://twitter.com/th3rac25)或以用户名为th3rac25的Google游戏爱你过服务 + +```racket +#lang racket ; 声明我们使用的语言 + +;;; 注释 + +;; 单行注释以分号开始 + +#| 块注释 + 可以横跨很多行而且... + #| + 可以嵌套 + |# +|# + +;; S表达式注释忽略剩下的表达式 +;; 在调试的时候会非常有用 +#; (被忽略的表达式) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 1. 原始数据类型和操作符 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; 数字 +9999999999999999999999 ; 整数 +#b111 ; 二进制数字 => 7 +#o111 ; 八进制数字 => 73 +#x111 ; 十六进制数字 => 273 +3.14 ; 实数 +6.02e+23 +1/2 ; 有理数 +1+2i ; 复数 + +;; 函数调用写作(f x y z ...) +;; 在这里 f 是一个函数, x, y, z, ... 是参数 +;; 如果你想创建一个列表数据的字面量, 使用 ' 来阻止它们 +;; 被求值 +'(+ 1 2) ; => (+ 1 2) +;; 接下来,是一些数学运算 +(+ 1 1) ; => 2 +(- 8 1) ; => 7 +(* 10 2) ; => 20 +(expt 2 3) ; => 8 +(quotient 5 2) ; => 2 +(remainder 5 2) ; => 1 +(/ 35 5) ; => 7 +(/ 1 3) ; => 1/3 +(exact->inexact 1/3) ; => 0.3333333333333333 +(+ 1+2i 2-3i) ; => 3-1i + +;;; 布尔类型 +#t ; 为真 +#f ; 为假,#f 之外的任何值都是真 +(not #t) ; => #f +(and 0 #f (error "doesn't get here")) ; => #f +(or #f 0 (error "doesn't get here")) ; => 0 + +;;; 字符 +#\A ; => #\A +#\λ ; => #\λ +#\u03BB ; => #\λ + +;;; 字符串是字符组成的定长数组 +"Hello, world!" +"Benjamin \"Bugsy\" Siegel" ; \是转义字符 +"Foo\tbar\41\x21\u0021\a\r\n" ; 包含C语言的转义字符,和Unicode +"λx:(μα.α→α).xx" ; 字符串可以包含Unicode字符 + +;; 字符串可以相加 +(string-append "Hello " "world!") ; => "Hello world!" + +;; 一个字符串可以看做是一个包含字符的列表 +(string-ref "Apple" 0) ; => #\A + +;; format 可以用来格式化字符串 +(format "~a can be ~a" "strings" "formatted") + +;; 打印字符串非常简单 +(printf "I'm Racket. Nice to meet you!\n") + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 2. 变量 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 你可以使用 define 定义一个变量 +;; 变量的名字可以使用任何字符除了: ()[]{}",'`;#|\ +(define some-var 5) +some-var ; => 5 + +;; 你也可以使用Unicode字符 +(define ⊆ subset?) +(⊆ (set 3 2) (set 1 2 3)) ; => #t + +;; 访问未赋值的变量会引发一个异常 +; x ; => x: undefined ... + +;; 本地绑定: `me' 被绑定到 "Bob",并且只在 let 中生效 +(let ([me "Bob"]) + "Alice" + me) ; => "Bob" + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. 结构和集合 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 结构体 +(struct dog (name breed age)) +(define my-pet + (dog "lassie" "collie" 5)) +my-pet ; => # +(dog? my-pet) ; => #t +(dog-name my-pet) ; => "lassie" + +;;; 对 (不可变的) +;; `cons' 函数返回对, `car' 和 `cdr' 从对中提取第1个 +;; 和第2个元素 +(cons 1 2) ; => '(1 . 2) +(car (cons 1 2)) ; => 1 +(cdr (cons 1 2)) ; => 2 + +;;; 列表 + +;; 列表由链表构成, 由 `cons' 函数的结果 +;; 和一个 `null' (或者 '()) 构成,后者标记了这个列表的结束 +(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3) +;; `list' 给列表提供了一个非常方便的可变参数的生成器 +(list 1 2 3) ; => '(1 2 3) +;; 一个单引号也可以用来表示一个列表字面量 +'(1 2 3) ; => '(1 2 3) + +;; 仍然可以使用 `cons' 函数在列表的开始处添加一项 +(cons 4 '(1 2 3)) ; => '(4 1 2 3) + +;; `append' 函数可以将两个列表合并 +(append '(1 2) '(3 4)) ; => '(1 2 3 4) + +;; 列表是非常基础的类型,所以有*很多*操作列表的方法 +;; 下面是一些例子: +(map add1 '(1 2 3)) ; => '(2 3 4) +(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33) +(filter even? '(1 2 3 4)) ; => '(2 4) +(count even? '(1 2 3 4)) ; => 2 +(take '(1 2 3 4) 2) ; => '(1 2) +(drop '(1 2 3 4) 2) ; => '(3 4) + +;;; 向量 + +;; 向量是定长的数组 +#(1 2 3) ; => '#(1 2 3) + +;; 使用 `vector-append' 方法将2个向量合并 +(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) + +;;; Set(翻译成集合也不太合适,所以不翻译了..) + +;; 从一个列表创建一个Set +(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3) + +;; 使用 `set-add' 函数增加一个成员 +;; (函数式特性: 这里会返回一个扩展后的Set,而不是修改输入的值) +(set-add (set 1 2 3) 4) ; => (set 1 2 3 4) + +;; 使用 `set-remove' 函数移除一个成员 +(set-remove (set 1 2 3) 1) ; => (set 2 3) + +;; 使用 `set-member?' 函数测试成员是否存在 +(set-member? (set 1 2 3) 1) ; => #t +(set-member? (set 1 2 3) 4) ; => #f + +;;; 散列表 + +;; 创建一个不变的散列表 (可变散列表的例子在下面) +(define m (hash 'a 1 'b 2 'c 3)) + +;; 根据键取得值 +(hash-ref m 'a) ; => 1 + +;; 获取一个不存在的键是一个异常 +; (hash-ref m 'd) => 没有找到元素 + +;; 你可以给不存在的键提供一个默认值 +(hash-ref m 'd 0) ; => 0 + +;; 使用 `hash-set' 函数来扩展一个不可变的散列表 +;; (返回的是扩展后的散列表而不是修改它) +(define m2 (hash-set m 'd 4)) +m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3)) + +;; 记住,使用 `hash` 函数创建的散列表是不可变的 +m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' + +;; 使用 `hash-remove' 函数移除一个键值对 (函数式特性,m并不变) +(hash-remove m 'a) ; => '#hash((b . 2) (c . 3)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. 函数 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 使用 `lambda' 创建函数 +;; 函数总是返回它最后一个表达式的值 +(lambda () "Hello World") ; => # +;; 也可以使用 Unicode 字符 `λ' +(λ () "Hello World") ; => 同样的函数 + +;; 使用括号调用一个函数,也可以直接调用一个 lambda 表达式 +((lambda () "Hello World")) ; => "Hello World" +((λ () "Hello World")) ; => "Hello World" + +;; 将函数赋值为一个变量 +(define hello-world (lambda () "Hello World")) +(hello-world) ; => "Hello World" + +;; 你可以使用函数定义的语法糖来简化代码 +(define (hello-world2) "Hello World") + +;; `()`是函数的参数列表 +(define hello + (lambda (name) + (string-append "Hello " name))) +(hello "Steve") ; => "Hello Steve" +;; 同样的,可以使用语法糖来定义: +(define (hello2 name) + (string-append "Hello " name)) + +;; 你也可以使用可变参数, `case-lambda' +(define hello3 + (case-lambda + [() "Hello World"] + [(name) (string-append "Hello " name)])) +(hello3 "Jake") ; => "Hello Jake" +(hello3) ; => "Hello World" +;; ... 或者给参数指定一个可选的默认值 +(define (hello4 [name "World"]) + (string-append "Hello " name)) + +;; 函数可以将多余的参数放到一个列表里 +(define (count-args . args) + (format "You passed ~a args: ~a" (length args) args)) +(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)" +;; ... 也可以使用不带语法糖的 `lambda' 形式: +(define count-args2 + (lambda args + (format "You passed ~a args: ~a" (length args) args))) + +;; 你可以混用两种用法 +(define (hello-count name . args) + (format "Hello ~a, you passed ~a extra args" name (length args))) +(hello-count "Finn" 1 2 3) +; => "Hello Finn, you passed 3 extra args" +;; ... 不带语法糖的形式: +(define hello-count2 + (lambda (name . args) + (format "Hello ~a, you passed ~a extra args" name (length args)))) + +;; 使用关键字 +(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args) + (format "~a ~a, ~a extra args" g name (length args))) +(hello-k) ; => "Hello World, 0 extra args" +(hello-k 1 2 3) ; => "Hello World, 3 extra args" +(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args" +(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args" +(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6) + ; => "Hi Finn, 6 extra args" + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 4. 判断是否相等 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 判断数字使用 `=' +(= 3 3.0) ; => #t +(= 2 1) ; => #f + +;; 判断对象使用 `eq?' +(eq? 3 3) ; => #t +(eq? 3 3.0) ; => #f +(eq? (list 3) (list 3)) ; => #f + +;; 判断集合使用 `equal?' +(equal? (list 'a 'b) (list 'a 'b)) ; => #t +(equal? (list 'a 'b) (list 'b 'a)) ; => #f + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 5. 控制结构 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; 条件判断 + +(if #t ; 测试表达式 + "this is true" ; 为真的表达式 + "this is false") ; 为假的表达式 +; => "this is true" + +;; 注意, 除 `#f` 之外的所有值都认为是真 +(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo) +(if (member 'Groucho '(Harpo Groucho Zeppo)) + 'yep + 'nope) +; => 'yep + +;; `cond' 会进行一系列的判断来选择一个结果 +(cond [(> 2 2) (error "wrong!")] + [(< 2 2) (error "wrong again!")] + [else 'ok]) ; => 'ok + +;;; 模式匹配 + +(define (fizzbuzz? n) + (match (list (remainder n 3) (remainder n 5)) + [(list 0 0) 'fizzbuzz] + [(list 0 _) 'fizz] + [(list _ 0) 'buzz] + [_ #f])) + +(fizzbuzz? 15) ; => 'fizzbuzz +(fizzbuzz? 37) ; => #f + +;;; 循环 + +;; 循环可以使用递归(尾递归) +(define (loop i) + (when (< i 10) + (printf "i=~a\n" i) + (loop (add1 i)))) +(loop 5) ; => i=5, i=6, ... + +;; 类似的,可以使用 `let` 定义 +(let loop ((i 0)) + (when (< i 10) + (printf "i=~a\n" i) + (loop (add1 i)))) ; => i=0, i=1, ... + +;; 看上面的例子怎么增加一个新的 `loop' 形式, 但是 Racket 已经有了一个非常 +;; 灵活的 `for' 了: +(for ([i 10]) + (printf "i=~a\n" i)) ; => i=0, i=1, ... +(for ([i (in-range 5 10)]) + (printf "i=~a\n" i)) ; => i=5, i=6, ... + +;;; 其他形式的迭代 +;; `for' 允许在很多数据结构中迭代: +;; 列表, 向量, 字符串, Set, 散列表, 等... + +(for ([i (in-list '(l i s t))]) + (displayln i)) + +(for ([i (in-vector #(v e c t o r))]) + (displayln i)) + +(for ([i (in-string "string")]) + (displayln i)) + +(for ([i (in-set (set 'x 'y 'z))]) + (displayln i)) + +(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))]) + (printf "key:~a value:~a\n" k v)) + +;;; 更多复杂的迭代 + +;; 并行扫描多个序列 (遇到长度小的就停止) +(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j)) +; => 0:x 1:y 2:z + +;; 嵌套循环 +(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j)) +; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z + +;; 带有条件判断的 `for` +(for ([i 1000] + #:when (> i 5) + #:unless (odd? i) + #:break (> i 10)) + (printf "i=~a\n" i)) +; => i=6, i=8, i=10 + +;;; 更多的例子帮助你加深理解.. +;; 和 `for' 循环非常像 -- 收集结果 + +(for/list ([i '(1 2 3)]) + (add1 i)) ; => '(2 3 4) + +(for/list ([i '(1 2 3)] #:when (even? i)) + i) ; => '(2) + +(for/list ([i 10] [j '(x y z)]) + (list i j)) ; => '((0 x) (1 y) (2 z)) + +(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10)) + i) ; => '(6 8 10) + +(for/hash ([i '(1 2 3)]) + (values i (number->string i))) +; => '#hash((1 . "1") (2 . "2") (3 . "3")) + +;; 也有很多其他的内置方法来收集循环中的值: +(for/sum ([i 10]) (* i i)) ; => 285 +(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000 +(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t +(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t +;; 如果需要合并计算结果, 使用 `for/fold' +(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10 +;; (这个函数可以在大部分情况下替代普通的命令式循环) + +;;; 异常 + +;; 要捕获一个异常,使用 `with-handlers' 形式 +(with-handlers ([exn:fail? (lambda (exn) 999)]) + (+ 1 "2")) ; => 999 +(with-handlers ([exn:break? (lambda (exn) "no time")]) + (sleep 3) + "phew") ; => "phew", 如果你打断了它,那么结果 => "no time" + +;; 使用 `raise' 抛出一个异常后者其他任何值 +(with-handlers ([number? ; 捕获抛出的数字类型的值 + identity]) ; 将它们作为普通值 + (+ 1 (raise 2))) ; => 2 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 6. 可变的值 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 使用 `set!' 给一个已经存在的变量赋一个新值 +(define n 5) +(set! n (add1 n)) +n ; => 6 + +;; 给那些明确的需要变化的值使用 `boxes` (在其他语言里类似指针 +;; 或者引用) +(define n* (box 5)) +(set-box! n* (add1 (unbox n*))) +(unbox n*) ; => 6 + +;; 很多 Racket 诗句类型是不可变的 (对,列表,等),有一些既是可变的 +;; 又是不可变的 (字符串,向量,散列表 +;; 等...) + +;; 使用 `vector' 或者 `make-vector' 创建一个可变的向量 +(define vec (vector 2 2 3 4)) +(define wall (make-vector 100 'bottle-of-beer)) +;; 使用 `vector-set!` 更新一项 +(vector-set! vec 0 1) +(vector-set! wall 99 'down) +vec ; => #(1 2 3 4) + +;; 创建一个空的可变散列表,然后操作它 +(define m3 (make-hash)) +(hash-set! m3 'a 1) +(hash-set! m3 'b 2) +(hash-set! m3 'c 3) +(hash-ref m3 'a) ; => 1 +(hash-ref m3 'd 0) ; => 0 +(hash-remove! m3 'a) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 7. 模块 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 模块让你将你的代码组织为多个文件,成为可重用的模块, +;; 在这里,我们使用嵌套在本文的整个大模块 +;; 里的子模块(从 "#lang" 这一行开始) + +(module cake racket/base ; 基于 racket/base 定义一个 `cake` 模块 + + (provide print-cake) ; 这个模块导出的函数 + + (define (print-cake n) + (show " ~a " n #\.) + (show " .-~a-. " n #\|) + (show " | ~a | " n #\space) + (show "---~a---" n #\-)) + + (define (show fmt n ch) ; 内部函数 + (printf fmt (make-string n ch)) + (newline))) + +;; 使用 `require` 从模块中得到所有 `provide` 的函数 +(require 'cake) ; 这里的 `'`表示是本地的子模块 +(print-cake 3) +; (show "~a" 1 #\A) ; => 报错, `show' 没有被导出,不存在 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 8. 类和对象 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 创建一个 fish% 类(%是给类绑定用的) +(define fish% + (class object% + (init size) ; 初始化的参数 + (super-new) ; 父类的初始化 + ;; 域 + (define current-size size) + ;; 公共方法 + (define/public (get-size) + current-size) + (define/public (grow amt) + (set! current-size (+ amt current-size))) + (define/public (eat other-fish) + (grow (send other-fish get-size))))) + +;; 创建一个 fish% 类的示例 +(define charlie + (new fish% [size 10])) + +;; 使用 `send' 调用一个对象的方法 +(send charlie get-size) ; => 10 +(send charlie grow 6) +(send charlie get-size) ; => 16 + +;; `fish%' 是一个普通的值,我们可以用它来混入 +(define (add-color c%) + (class c% + (init color) + (super-new) + (define my-color color) + (define/public (get-color) my-color))) +(define colored-fish% (add-color fish%)) +(define charlie2 (new colored-fish% [size 10] [color 'red])) +(send charlie2 get-color) +;; 或者,不带名字 +(send (new (add-color fish%) [size 10] [color 'red]) get-color) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 9. 宏 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 宏让你扩展这门语言的语法 + +;; 让我们定义一个while循环 +(define-syntax-rule (while condition body ...) + (let loop () + (when condition + body ... + (loop)))) + +(let ([i 0]) + (while (< i 10) + (displayln i) + (set! i (add1 i)))) + +;; 宏是安全的,你不能修改现有的变量 +(define-syntax-rule (swap! x y) ; !表示会修改 + (let ([tmp x]) + (set! x y) + (set! y tmp))) + +(define tmp 2) +(define other 3) +(swap! tmp other) +(printf "tmp = ~a; other = ~a\n" tmp other) +;; 变量 `tmp` 被重命名为 `tmp_1` +;; 避免名字冲突 +;; (let ([tmp_1 tmp]) +;; (set! tmp other) +;; (set! other tmp_1)) + +;; 但它们仍然会导致错误代码,比如: +(define-syntax-rule (bad-while condition body ...) + (when condition + body ... + (bad-while condition body ...))) +;; 这个宏会挂掉,它产生了一个无限循环,如果你试图去使用它 +;; 编译器会进入死循环 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 10. 契约 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; 契约限制变量从模块中导入 + +(module bank-account racket + (provide (contract-out + [deposit (-> positive? any)] ; 数量一直是正值 + [balance (-> positive?)])) + + (define amount 0) + (define (deposit a) (set! amount (+ amount a))) + (define (balance) amount) + ) + +(require 'bank-account) +(deposit 5) + +(balance) ; => 5 + +;; 客户端尝试存储一个负值时会出错 +;; (deposit -5) ; => deposit: contract violation +;; expected: positive? +;; given: -5 +;; more details.... +``` + +## 进一步阅读 + +想知道更多吗? 尝试 [Getting Started with Racket](http://docs.racket-lang.org/getting-started/) -- cgit v1.2.3 From 73e700e0138d7b4c953d8f5990118d9c55ecfa41 Mon Sep 17 00:00:00 2001 From: lyuehh Date: Mon, 12 Aug 2013 23:02:44 +0800 Subject: fix typo --- zh-cn/racket-cn.html.markdown | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown index 9370d300..e17aedda 100644 --- a/zh-cn/racket-cn.html.markdown +++ b/zh-cn/racket-cn.html.markdown @@ -11,7 +11,7 @@ translators: --- Racket是Lisp/Scheme家族中的一个通用的,多范式的编程语言。 -非常期待您的反馈!你可以通过[@th3rac25](http://twitter.com/th3rac25)或以用户名为th3rac25的Google游戏爱你过服务 +非常期待您的反馈!你可以通过[@th3rac25](http://twitter.com/th3rac25)或以用户名为 th3rac25 的Google邮箱服务和我取得联系 ```racket #lang racket ; 声明我们使用的语言 @@ -125,7 +125,7 @@ my-pet ; => # (dog-name my-pet) ; => "lassie" ;;; 对 (不可变的) -;; `cons' 函数返回对, `car' 和 `cdr' 从对中提取第1个 +;; `cons' 返回对, `car' 和 `cdr' 从对中提取第1个 ;; 和第2个元素 (cons 1 2) ; => '(1 . 2) (car (cons 1 2)) ; => 1 @@ -133,7 +133,7 @@ my-pet ; => # ;;; 列表 -;; 列表由链表构成, 由 `cons' 函数的结果 +;; 列表由链表构成, 由 `cons' 的结果 ;; 和一个 `null' (或者 '()) 构成,后者标记了这个列表的结束 (cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3) ;; `list' 给列表提供了一个非常方便的可变参数的生成器 @@ -141,7 +141,7 @@ my-pet ; => # ;; 一个单引号也可以用来表示一个列表字面量 '(1 2 3) ; => '(1 2 3) -;; 仍然可以使用 `cons' 函数在列表的开始处添加一项 +;; 仍然可以使用 `cons' 在列表的开始处添加一项 (cons 4 '(1 2 3)) ; => '(4 1 2 3) ;; `append' 函数可以将两个列表合并 @@ -169,14 +169,14 @@ my-pet ; => # ;; 从一个列表创建一个Set (list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3) -;; 使用 `set-add' 函数增加一个成员 +;; 使用 `set-add' 增加一个成员 ;; (函数式特性: 这里会返回一个扩展后的Set,而不是修改输入的值) (set-add (set 1 2 3) 4) ; => (set 1 2 3 4) -;; 使用 `set-remove' 函数移除一个成员 +;; 使用 `set-remove' 移除一个成员 (set-remove (set 1 2 3) 1) ; => (set 2 3) -;; 使用 `set-member?' 函数测试成员是否存在 +;; 使用 `set-member?' 测试成员是否存在 (set-member? (set 1 2 3) 1) ; => #t (set-member? (set 1 2 3) 4) ; => #f @@ -194,15 +194,15 @@ my-pet ; => # ;; 你可以给不存在的键提供一个默认值 (hash-ref m 'd 0) ; => 0 -;; 使用 `hash-set' 函数来扩展一个不可变的散列表 +;; 使用 `hash-set' 来扩展一个不可变的散列表 ;; (返回的是扩展后的散列表而不是修改它) (define m2 (hash-set m 'd 4)) m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3)) -;; 记住,使用 `hash` 函数创建的散列表是不可变的 +;; 记住,使用 `hash` 创建的散列表是不可变的 m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' -;; 使用 `hash-remove' 函数移除一个键值对 (函数式特性,m并不变) +;; 使用 `hash-remove' 移除一个键值对 (函数式特性,m并不变) (hash-remove m 'a) ; => '#hash((b . 2) (c . 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -437,7 +437,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (set! n (add1 n)) n ; => 6 -;; 给那些明确的需要变化的值使用 `boxes` (在其他语言里类似指针 +;; 给那些明确地需要变化的值使用 `boxes` (在其他语言里类似指针 ;; 或者引用) (define n* (box 5)) (set-box! n* (add1 (unbox n*))) -- cgit v1.2.3 From 45ddaf5f46ea2e59bb8d8ad9038182d99a2c9550 Mon Sep 17 00:00:00 2001 From: hbc Date: Mon, 12 Aug 2013 23:13:20 +0800 Subject: Improved some translations, added a line break --- zh-cn/ruby-cn.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zh-cn/ruby-cn.html.markdown b/zh-cn/ruby-cn.html.markdown index 16c0ed67..fcc60162 100644 --- a/zh-cn/ruby-cn.html.markdown +++ b/zh-cn/ruby-cn.html.markdown @@ -94,7 +94,7 @@ x = y = 10 #=> 10 x #=> 10 y #=> 10 -# 按照惯例,用snake_case 作为变量名 +# 按照惯例,用 snake_case 作为变量名 snake_case = true # 使用具有描述性的运算符 @@ -102,7 +102,8 @@ path_to_project_root = '/good/name/' path = '/bad/name/' # 符号(Symbols,也是对象) -# 符号是不可变的,内部用整数类型表示的可重用的值。通常用它代替字符串来有效地表达有意义的值 +# 符号是不可变的,内部用整数类型表示的可重用的值。 +# 通常用它代替字符串来有效地表示有意义的值。 :pending.class #=> Symbol -- cgit v1.2.3 From 76526895ff276939dbd4a8f36f631588459584ba Mon Sep 17 00:00:00 2001 From: Xiao Chuan Yu Date: Mon, 12 Aug 2013 11:17:37 -0400 Subject: Update haskell.html.markdown Just a typo. https://github.com/adambard/learnxinyminutes-docs/issues/190 --- haskell.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/haskell.html.markdown b/haskell.html.markdown index 9847ef2a..e3ec3f38 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -84,7 +84,7 @@ not False -- True -- rest of the elements of this "infinite" list don't exist yet! Haskell won't -- actually evaluate them until it needs to. -- joining two lists +-- joining two lists [1..5] ++ [6..10] -- adding to the head of a list -- cgit v1.2.3 From 5424b31848e85e40ea78afb4582e8f33845e1b01 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 14:53:00 -0400 Subject: Explain blocks better --- ruby.html.markdown | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 861a94ad..52321ff6 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -5,6 +5,7 @@ contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] --- ```ruby @@ -158,11 +159,6 @@ hash['number'] #=> 5 # Asking a hash for a key that doesn't exist returns nil: hash['nothing here'] #=> nil -# Iterate over hashes with the #each method: -hash.each do |k, v| - puts "#{k} is #{v}" -end - # Since Ruby 1.9, there's a special syntax when using symbols as keys: new_hash = { defcon: 3, action: true} @@ -193,7 +189,12 @@ end # HOWEVER # No-one uses for loops -# Use `each` instead, like this: +# Under the hood for loops use the each method which takes a "block". +# A block is a bunch of code that you can pass to a method like each. +# It is analogous to lambdas, anonymous functions or closures in other programming languages. + +# The each method runs the block multiple times passing a counter. +# You can iterate over a range like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -204,6 +205,17 @@ end #=> iteration 4 #=> iteration 5 +# You can also surround blocks in curly brackets: +(1..5).each {|counter| puts "iteration #{counter}"} + +# You can also iterate over the contents of data structures using each. +array.each do |element| + puts "#{element} is part of the array" +end +hash.each do |key, value| + puts "#{key} is #{value}" +end + counter = 1 while counter <= 5 do puts "iteration #{counter}" -- cgit v1.2.3 From 5c5c8d3c4a0aea7c1c7a8150f428e354d0a12a6c Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 15:05:00 -0400 Subject: Tidy up wording --- ruby.html.markdown | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 52321ff6..68c5b524 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -187,14 +187,14 @@ end #=> iteration 4 #=> iteration 5 -# HOWEVER -# No-one uses for loops -# Under the hood for loops use the each method which takes a "block". -# A block is a bunch of code that you can pass to a method like each. +# HOWEVER, No-one uses for loops. +# Instead you should use the "each" method and pass it a block. +# A block is a bunch of code that you can pass to a method like "each". # It is analogous to lambdas, anonymous functions or closures in other programming languages. - -# The each method runs the block multiple times passing a counter. -# You can iterate over a range like this: +# +# The "each" method of a range runs the block once for each element of the range. +# The block is passed a counter as a parameter. +# Calling the "each" method with a block looks like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -208,7 +208,7 @@ end # You can also surround blocks in curly brackets: (1..5).each {|counter| puts "iteration #{counter}"} -# You can also iterate over the contents of data structures using each. +# The contents of data structures can also be iterated using each. array.each do |element| puts "#{element} is part of the array" end -- cgit v1.2.3 From bbb8c837457eecc95ac3a66db150ed595ff7bb33 Mon Sep 17 00:00:00 2001 From: Rob Vella Date: Mon, 12 Aug 2013 17:34:17 -0700 Subject: Added visibility to static variable declarations --- php.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/php.html.markdown b/php.html.markdown index ce228870..083574ee 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -440,6 +440,11 @@ class MyClass static $staticVar = 'static'; + // Static variables and their visibility + public static $publicStaticVar = 'publicStatic'; + private static $privateStaticVar = 'privateStatic'; // Accessible within the class only + protected static $protectedStaticVar = 'protectedStatic'; // Accessible from the class and subclasses + // Properties must declare their visibility public $property = 'public'; public $instanceProp; -- cgit v1.2.3 From e17ef8b1802c558ab9d77bd03569816dadabd750 Mon Sep 17 00:00:00 2001 From: Irfan Charania Date: Mon, 12 Aug 2013 17:54:53 -0700 Subject: Add C# Language Based on the Java one --- csharp.html.markdown | 550 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 550 insertions(+) create mode 100644 csharp.html.markdown diff --git a/csharp.html.markdown b/csharp.html.markdown new file mode 100644 index 00000000..d67f6c74 --- /dev/null +++ b/csharp.html.markdown @@ -0,0 +1,550 @@ +--- + +language: c# +contributors: + - ["Irfan Charania", "https://github.com/irfancharania"] +filename: LearnCSharp.cs + +--- + +C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. + +[Read more here.](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx) + +```c# +// Single-line comments start with // +/* +Multi-line comments look like this +*/ +/// +/// This is an XML documentation comment +/// + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +// defines scope to organize code into "packages" +namespace Learning +{ + // Each .cs file should at least contain a class with the same name as the file + // you're allowed to do otherwise, but shouldn't for sanity. + public class LearnCSharp + { + // A console application must have a main method as an entry point + public static void Main(string[] args) + { + // Use Console.WriteLine to print lines + Console.WriteLine("Hello World"); + Console.WriteLine( + "Integer: " + 10 + + " Double: " + 3.14 + + " Boolean: " + true); + + // To print without a new line, use Console.Write + Console.Write("Hello "); + Console.Write("World"); + + + /////////////////////////////////////////////////// + // Types & Variables + // + // Declare a variable using + /////////////////////////////////////////////////// + + // Sbyte - Signed 8-bit integer + // (-128 <= sbyte <= 127) + sbyte fooSbyte = 100; + + // Byte - Unsigned 8-bit integer + // (0 <= byte <= 255) + byte fooByte = 100; + + // Short - Signed 16-bit integer + // (-32,768 <= short <= 32,767) + short fooShort = 10000; + + // Ushort - Unsigned 16-bit integer + // (0 <= ushort <= 65,535) + ushort fooUshort = 10000; + + // Integer - Signed 32-bit integer + // (-2,147,483,648 <= int <= 2,147,483,647) + int fooInt = 1; + + // Uinteger - Unsigned 32-bit integer + // (0 <= uint <= 4,294,967,295) + uint fooUint = 1; + + // Long - Signed 64-bit integer + // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) + long fooLong = 100000L; + // L is used to denote that this variable value is of type long or ulong + // anything without is treated as int or uint depending on size. + + // Ulong - Unsigned 64-bit integer + // (0 <= ulong <= 18,446,744,073,709,551,615) + ulong fooUlong = 100000L; + + // Float - Single-precision 32-bit IEEE 754 Floating Point + // Precision: 7 digits + float fooFloat = 234.5f; + // f is used to denote that this variable value is of type float; + // otherwise it is treated as double. + + // Double - Double-precision 64-bit IEEE 754 Floating Point + // Precision: 15-16 digits + double fooDouble = 123.4; + + // Bool - true & false + bool fooBoolean = true; + bool barBoolean = false; + + // Char - A single 16-bit Unicode character + char fooChar = 'A'; + + // Strings + string fooString = "My string is here!"; + Console.WriteLine(fooString); + + // formatting + string fooFormattedString = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); + Console.WriteLine(fooFormattedString); + + // formatting dates + DateTime fooDate = DateTime.Now; + Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); + + // \n is an escaped character that starts a new line + string barString = "Printing on a new line?\nNo Problem!"; + Console.WriteLine(barString); + + // it can be written prettier by using the @ symbol + string bazString = @"Here's some stuff + on a new line!"; + Console.WriteLine(bazString); + + // quotes need to be escaped + // use \" normally + string quotedString = "some \"quoted\" stuff"; + Console.WriteLine(quotedString); + + // use "" when strings start with @ + string quotedString2 = @"some MORE ""quoted"" stuff"; + Console.WriteLine(quotedString2); + + // Use const or read-only to make a variable immutable + // const values are calculated at compile time + const int HOURS_I_WORK_PER_WEEK = 9001; + + // Nullable types + // any type can be made nullable by suffixing a ? + // ? = + int? nullable = null; + Console.WriteLine("Nullable variable: " + nullable); + + // ?? is syntactic sugar for specifying default value + // in case variable is null + int notNullable = nullable ?? 0; + Console.WriteLine("Not nullable variable: " + notNullable); + + // Var - compiler will choose the most appropriate type based on value + var fooImplicit = true; + + /////////////////////////////////////////////////// + // Data Structures + /////////////////////////////////////////////////// + Console.WriteLine("\n->Data Structures"); + + // Arrays + // The array size must be decided upon declaration + // The format for declaring an array is follows: + // [] = new []; + int[] intArray = new int[10]; + string[] stringArray = new string[1]; + bool[] boolArray = new bool[100]; + + // Another way to declare & initialize an array + int[] y = { 9000, 1000, 1337 }; + + // Indexing an array - Accessing an element + Console.WriteLine("intArray @ 0: " + intArray[0]); + + // Arrays are zero-indexed and mutable. + intArray[1] = 1; + Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1 + + // Lists + // Lists are used more frequently than arrays as they are more flexible + // The format for declaring a list is follows: + // List = new List(); + List intList = new List(); + List stringList = new List(); + + // Another way to declare & initialize a list + List z = new List { 9000, 1000, 1337 }; + + // Indexing a list - Accessing an element + // Lists are zero-indexed and mutable. + Console.WriteLine("z @ 0: " + z[2]); + + // Lists don't default to a value; + // A value must be added before accessing the index + intList.Add(1); + Console.WriteLine("intList @ 0: " + intList[0]); + + + // Others data structures to check out: + // + // Stack/Queue + // Dictionary + // Read-only Collections + // Tuple (.Net 4+) + + + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + Console.WriteLine("\n->Operators"); + + int i1 = 1, i2 = 2; // Shorthand for multiple declarations + + // Arithmetic is straightforward + Console.WriteLine("1+2 = " + (i1 + i2)); // => 3 + Console.WriteLine("2-1 = " + (i2 - i1)); // => 1 + Console.WriteLine("2*1 = " + (i2 * i1)); // => 2 + Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down) + + // Modulo + Console.WriteLine("11%3 = " + (11 % 3)); // => 2 + + // Comparison operators + Console.WriteLine("3 == 2? " + (3 == 2)); // => false + Console.WriteLine("3 != 2? " + (3 != 2)); // => true + Console.WriteLine("3 > 2? " + (3 > 2)); // => true + Console.WriteLine("3 < 2? " + (3 < 2)); // => false + Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true + Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true + + // Bitwise operators! + /* + ~ Unary bitwise complement + << Signed left shift + >> Signed right shift + >>> Unsigned right shift + & Bitwise AND + ^ Bitwise exclusive OR + | Bitwise inclusive OR + */ + + // Incrementations + int i = 0; + Console.WriteLine("\n->Inc/Dec-rementation"); + Console.WriteLine(i++); //i = 1. Post-Incrementation + Console.WriteLine(++i); //i = 2. Pre-Incrementation + Console.WriteLine(i--); //i = 1. Post-Decrementation + Console.WriteLine(--i); //i = 0. Pre-Decrementation + + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + Console.WriteLine("\n->Control Structures"); + + // If statements are c-like + int j = 10; + if (j == 10) + { + Console.WriteLine("I get printed"); + } + else if (j > 10) + { + Console.WriteLine("I don't"); + } + else + { + Console.WriteLine("I also don't"); + } + + // Ternary operators + // A simple if/else can be written as follows + // ? : + string isTrue = (true) ? "True" : "False"; + Console.WriteLine("Ternary demo: " + isTrue); + + + // While loop + int fooWhile = 0; + while (fooWhile < 100) + { + //Console.WriteLine(fooWhile); + //Increment the counter + //Iterated 99 times, fooWhile 0->99 + fooWhile++; + } + Console.WriteLine("fooWhile Value: " + fooWhile); + + // Do While Loop + int fooDoWhile = 0; + do + { + //Console.WriteLine(fooDoWhile); + //Increment the counter + //Iterated 99 times, fooDoWhile 0->99 + fooDoWhile++; + } while (fooDoWhile < 100); + Console.WriteLine("fooDoWhile Value: " + fooDoWhile); + + // For Loop + int fooFor; + //for loop structure => for(; ; ) + for (fooFor = 0; fooFor < 10; fooFor++) + { + //Console.WriteLine(fooFor); + //Iterated 10 times, fooFor 0->9 + } + Console.WriteLine("fooFor Value: " + fooFor); + + // Switch Case + // A switch works with the byte, short, char, and int data types. + // It also works with enumerated types (discussed in Enum Types), + // the String class, and a few special classes that wrap + // primitive types: Character, Byte, Short, and Integer. + int month = 3; + string monthString; + switch (month) + { + case 1: + monthString = "January"; + break; + case 2: + monthString = "February"; + break; + case 3: + monthString = "March"; + break; + default: + monthString = "Some other month"; + break; + } + Console.WriteLine("Switch Case Result: " + monthString); + + + /////////////////////////////////////// + // Converting Data Types And Typcasting + /////////////////////////////////////// + + // Converting data + + // Convert String To Integer + // this will throw an Exception on failure + int.Parse("123");//returns an integer version of "123" + + // try parse will default to type default on failure + // in this case: 0 + int tryInt; + int.TryParse("123", out tryInt); + + // Convert Integer To String + // Convert class has a number of methods to facilitate conversions + Convert.ToString(123); + + /////////////////////////////////////// + // Classes And Functions + /////////////////////////////////////// + + Console.WriteLine("\n->Classes & Functions"); + + // (definition of the Bicycle class follows) + + // Use new to instantiate a class + Bicycle trek = new Bicycle(); + + // Call object methods + trek.speedUp(3); // You should always use setter and getter methods + trek.setCadence(100); + + // ToString is a convention to display the value of this Object. + Console.WriteLine("trek info: " + trek.ToString()); + + // Instantiate another new Bicycle + Bicycle octo = new Bicycle(5, 10); + Console.WriteLine("octo info: " + octo.ToString()); + + // Instantiate a new Penny Farthing + PennyFarthing funbike = new PennyFarthing(1, 10); + Console.WriteLine("funbike info: " + funbike.ToString()); + + Console.Read(); + } // End main method + + + } // End LearnCSharp class + + // You can include other classes in a .cs file + + + // Class Declaration Syntax: + // class { + // //data fields, constructors, functions all inside. + // //functions are called as methods in Java. + // } + + public class Bicycle + { + // Bicycle's Fields/Variables + public int cadence; // Public: Can be accessed from anywhere + private int _speed; // Private: Only accessible from within the class + protected int gear; // Protected: Accessible from the class and subclasses + internal int wheels; // Internal: Accessible from within the assembly + string name; // default: Only accessible from within this class + + // readonly values are set at run time + // they can only be assigned upon declaration or in a constructor + readonly bool hasCardsInSpokes = false; // read-only private + + // Constructors are a way of creating classes + // This is a default constructor + public Bicycle() + { + gear = 1; + cadence = 50; + _speed = 5; + name = "Bontrager"; + } + + // This is a specified constructor (it contains arguments) + public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes) + { + this.gear = startGear; + this.cadence = startCadence; + this._speed = startSpeed; + this.name = name; + this.hasCardsInSpokes = hasCardsInSpokes; + } + + // Constructors can be chained + public Bicycle(int startCadence, int startSpeed) : + this(startCadence, startSpeed, 0, "big wheels", true) + { + } + + // Function Syntax: + // () + + // classes can implement getters and setters for their fields + // or they can implement properties + + // Method declaration syntax: + // () + public int getCadence() + { + return cadence; + } + + // void methods require no return statement + public void setCadence(int newValue) + { + cadence = newValue; + } + + // virtual keyword indicates this method can be overridden + public virtual void setGear(int newValue) + { + gear = newValue; + } + + public void speedUp(int increment) + { + _speed += increment; + } + + public void slowDown(int decrement) + { + _speed -= decrement; + } + + // properties get/set values + // when only data needs to be accessed, consider using properties. + // properties may have either get or set, or both + private bool _hasTassles; // private variable + public bool hasTassles // public accessor + { + get { return _hasTassles; } + set { _hasTassles = value; } + } + + private int _frameSize; + public int FrameSize + { + get { return _frameSize; } + // you are able to specify access modifiers for either get or set + // this means only Bicycle class can call set on Framesize + private set { _frameSize = value; } + } + + //Method to display the attribute values of this Object. + public override string ToString() + { + return "gear: " + gear + + " cadence: " + cadence + + " speed: " + _speed + + " name: " + name + + " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") + + "\n------------------------------\n" + ; + } + } // end class Bicycle + + // PennyFarthing is a subclass of Bicycle + class PennyFarthing : Bicycle + { + // (Penny Farthings are those bicycles with the big front wheel. + // They have no gears.) + + // calling parent constructor + public PennyFarthing(int startCadence, int startSpeed) : + base(startCadence, startSpeed, 0, "PennyFarthing", true) + { + } + + public override void setGear(int gear) + { + gear = 0; + } + } +} // End Namespace + +``` + +## Topics Not Covered + + * Enums, Flags + * Attributes + * Generics (T), Delegates, Func, Actions, lambda expressions + * Exceptions, Interfaces, Abstraction + * LINQ + * ASP.NET (Web Forms/MVC/WebMatrix) + * Winforms + * Windows Presentation Foundation (WPF) + + + +## Further Reading + + * [DotNetPerls](http://www.dotnetperls.com) + * [C# in Depth](http://manning.com/skeet2) + * [Programming C#](http://shop.oreilly.com/product/0636920024064.do) + * [LINQ](http://shop.oreilly.com/product/9780596519254.do) + * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx) + * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials) + * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials) + * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials) + * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208) + + + +[C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx) -- cgit v1.2.3 From bf1f9d811cfc41c33cfeaabca68034ad531a097d Mon Sep 17 00:00:00 2001 From: Irfan Charania Date: Mon, 12 Aug 2013 18:04:11 -0700 Subject: Clean up namespace in C# --- csharp.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index d67f6c74..e079571e 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -20,12 +20,10 @@ Multi-line comments look like this /// This is an XML documentation comment /// - +// Specify namespaces application will be using using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; + // defines scope to organize code into "packages" namespace Learning -- cgit v1.2.3 From 6dab86ce9d6df4211328e64afadb94a64fd955bb Mon Sep 17 00:00:00 2001 From: Bruno Henrique Date: Mon, 12 Aug 2013 23:31:46 -0300 Subject: for review --- pt-br/ruby-pt.html.markdown | 598 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 598 insertions(+) create mode 100644 pt-br/ruby-pt.html.markdown diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown new file mode 100644 index 00000000..cedd2db1 --- /dev/null +++ b/pt-br/ruby-pt.html.markdown @@ -0,0 +1,598 @@ +--- +language: ruby +filename: learnruby.rb +contributors: + - ["Bruno Henrique - Garu", "http://garulab.com"] +--- + +```ruby +# Isso é um comentario + +=begin +This is a multiline comment +No-one uses them +You shouldn't either + +Isso é um comentario multilinha +Ninguém os usa + +=end + +# First and foremost: Everything is an object. +# Primeiro e principal: Tudo é um objeto. + +# Numbers are objects +# Números são objetos + +3.class #=> Fixnum + +3.to_s #=> "3" + + +# Some basic arithmetic +# Aritmética básica + +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Arithmetic is just syntactic sugar +# for calling a method on an object +# Arithmetic é apenas açúcar semântico +# para chamar um métoddo de um objeto +1.+(3) #=> 4 +10.* 5 #=> 50 + +# Special values are objects +# Valores especiais são obejetos +nil # Nothing to see here +nil # Nada para ver aqui +true # truth +true # verdadeiro +false # falsehood +false # falso + +nil.class #=> NilClass +true.class #=> TrueClass +false.class #=> FalseClass + +# Equality +# Igualdade +1 == 1 #=> true +2 == 1 #=> false + +# Inequality +# Desigualdade +1 != 1 #=> false +2 != 1 #=> true +!true #=> false +!false #=> true + +# apart from false itself, nil is the only other 'falsey' value +# além de 'false', 'nil' é o único outro valor falso + +!nil #=> true +!false #=> true +!0 #=> false + +# More comparisons +# Mais comparações +1 < 10 #=> true +1 > 10 #=> false +2 <= 2 #=> true +2 >= 2 #=> true + +# Strings are objects +# Strings são obejetos + +'I am a string'.class #=> String +'Eu sou uma string'.class #=> String +"I am a string too".class #=> String +"Eu também sou uma string".class #=> String + +placeholder = "use string interpolation" +placeholder = "usar interpolação de string" +"I can #{placeholder} when using double quoted strings" +"Eu posso #{placeholder} quando estiver usando aspas duplas" +#=> "I can use string interpolation when using double quoted strings" +#=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas" + + +# print to the output +# imprime para output (saida) +puts "I'm printing!" +puts "Estou imprimindo" + +# Variables +# Variáveis +x = 25 #=> 25 +x #=> 25 + +# Note that assignment returns the value assigned +# Note que uma atribuição retorna o valor atribuido +# This means you can do multiple assignment: +# Isso significa que você pode fazer multiplas atribuições: + +x = y = 10 #=> 10 +x #=> 10 +y #=> 10 + +# By convention, use snake_case for variable names +# Por convenção, use snake_case para nomes de variáveis +snake_case = true + +# Use descriptive variable names +# Use nomes de variáveis descrivos +path_to_project_root = '/good/name/' +caminho_para_a_raiz_do_projeto = '/bom/nome/' +path = '/bad/name/' +caminho = '/nome/ruim/' + +# Symbols (are objects) +# Simbolos (são objetos) +# Symbols are immutable, reusable constants represented internally by an +# Simbolos são imultáveis, são constantes reutilizáveis representadadas internamente por um +# integer value. They're often used instead of strings to efficiently convey +# valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores +# specific, meaningful values +# específicos e significativos + +:pending.class #=> Symbol +:pendente.class #=> Symbol + +status = :pending +status = :pendente + +status == :pending #=> true +status == :pendente #=> true + +status == 'pending' #=> false +status == 'pendente' #=> false + +status == :approved #=> false +status == :aprovado #=> false + +# Arrays + +# This is an array +# Isso é um array +[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] + +# Arrays can contain different types of items +# Arrays podem conter diferentes tipos de itens + +array = [1, "hello", false] #=> => [1, "hello", false] +array = [1, "Oi", false] #=> => [1, "Oi", false] + +# Arrays can be indexed +# Arrays podem ser indexados +# From the front +# a partir do começo +array[0] #=> 1 +array[12] #=> nil + +# Like arithmetic, [var] access +# Como aritimetica, o acesso via [var] +# is just syntactic sugar +# é apenas açúcar sintático +# for calling a method [] on an object +# para chamar o método [] de um objeto +array.[] 0 #=> 1 +array.[] 12 #=> nil + +# From the end +# a partir do final +array[-1] #=> 5 + +# With a start and end index +# Com um índice de começo e fim +array[2, 4] #=> [3, 4, 5] + +# Or with a range +# Ou com um intervalo de valores +array[1..3] #=> [2, 3, 4] + +# Add to an array like this +# Adicionar a um array como este +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# Hashes are Ruby's primary dictionary with keys/value pairs. +# Hashes são dicionário com um par de chave(key)/valor(value) +# Hashes are denoted with curly braces: +# Hashes são simbolizados com chaves "{}" +hash = {'color' => 'green', 'number' => 5} +hash = {'cor' => 'verde', 'numero' => 5} + +hash.keys #=> ['cor', 'numero'] + +# Hashes can be quickly looked up by key: +# Hashes podem ser rapidamente pesquisado pela chave (key) +hash['cor'] #=> 'verde' +hash['numero'] #=> 5 + +# Asking a hash for a key that doesn't exist returns nil: +# Procurar em um hash por uma chave que não existe retorna nil: +hash['nothing here'] #=> nil +hash['nada aqui'] #=> nil + +# Iterate over hashes with the #each method: +# Interar sobre hashes com o método #each: +hash.each do |k, v| + puts "#{k} is #{v}" +end + +hash.each do |k, v| + puts "#{k} é #{v}" +end + +# Since Ruby 1.9, there's a special syntax when using symbols as keys: +# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos simbolos como chaves (keys) + +new_hash = { defcon: 3, action: true} +novo_hash = { defcon: 3, acao: true} + +new_hash.keys #=> [:defcon, :action] +novo_hash.keys #=> [:defcon, :acao] + +# Tip: Both Arrays and Hashes are Enumerable +# Dica: Tanto Arrays quanto Hashes são Enumerable +# They share a lot of useful methods such as each, map, count, and more +# Eles compartilham um monte de métodos úteis como each, map, count e mais + +# Control structures +# Estruturas de controle + +if true + "if statement" +elsif false + "else if, optional" +else + "else, also optional" +end + +if true + "Se verdadeiro" +elsif false + "else if, opicional" +else + "else, também é opicional" +end + +for counter in 1..5 + puts "iteration #{counter}" +end + +for contador in 1..5 + puts "interação #{contador}" +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +# HOWEVER +# PORÉM +# No-one uses for loops +# Ninguém usa para loops +# Use `each` instead, like this: +# Use "each" em vez, dessa forma: + +(1..5).each do |counter| + puts "iteration #{counter}" +end + +(1..5).each do |contador| + puts "interação #{contador}" +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +counter = 1 +while counter <= 5 do + puts "iteration #{counter}" + counter += 1 +end + +contador = 1 +while contador <= 5 do + puts "interação #{contador}" + contador += 1 +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +grade = 'B' + +case grade +when 'A' + puts "Way to go kiddo" +when 'B' + puts "Better luck next time" +when 'C' + puts "You can do better" +when 'D' + puts "Scraping through" +when 'F' + puts "You failed!" +else + puts "Alternative grading system, eh?" +end + +grau = 'B' + +case grau +when 'A' + puts "Um longo caminho a percorrer pequeno gafanhoto" +when 'B' + puts "Melhor sorte da próxima vez" +when 'C' + puts "Você pode fazer melhor" +when 'D' + puts "Scraping through" +when 'F' + puts "Você falhou" +else + puts "Alternative grading system, eh?" +end + +# Functions +# Funções + +def dobrar(x) + x * 2 +end + +# Functions (and all blocks) implcitly return the value of the last statement +# Funções (e todos os blocos) retornam implicitamente o valor da última linha +double(2) #=> 4 +dobrar(2) #=> 4 + +# Parentheses are optional where the result is unambiguous +# Parênteses são opicionais onde o resultado é claro +double 3 #=> 6 +dobrar 3 #=> 6 + +double double 3 #=> 12 +dobrar dobrar 3 #=> 12 + +def sum(x,y) + x + y +end + +def somar(x,y) + x + y +end + +# Method arguments are separated by a comma +# Argumentos de métodos são separados por uma virgula +sum 3, 4 #=> 7 +somar 3, 4 #=> 7 + +somar somar(3,4), 5 #=> 12 + +# yield +# All methods have an implicit, optional block parameter +# Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco +# it can be called with the 'yield' keyword +# ele pode ser chamado com a palavra chave 'yield' + +def surround + puts "{" + yield + puts "}" +end + +surround { puts 'hello world' } + + +def ao_redor + puts "{" + yield + puts "}" +end + +ao_redor { puts 'Olá mundo' } + +# { +# Olá mundo +# } + + +# Define a class with the class keyword +# Define uma classe com a palavra chave 'class' +class Human + + # A class variable. It is shared by all instances of this class. + @@species = "H. sapiens" + + # Basic initializer + def initialize(name, age=0) + # Assign the argument to the "name" instance variable for the instance + @name = name + # If no age given, we will fall back to the default in the arguments list. + @age = age + end + + # Basic setter method + def name=(name) + @name = name + end + + # Basic getter method + def name + @name + end + + # A class method uses self to distinguish from instance methods. + # It can only be called on the class, not an instance. + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end + +end + + +class Humano + + # Uma variavel de classe. Ela é compartilhada por todas as instancias dessa classe + @@especies = "H. sapiens" + + # Inicialização básica (contructor) + def initialize(nome, idade=0) + # Atribui o argumento para a variavel de instacia "nome" do objeto + @nome = nome + # Se a idade não for passada, nós definimos um valor padrão na lista de argumentos + @idade = idade + end + + # Método básico para atribuir valor + def nome=(nome) + @nome = nome + end + + # Método básico de resgatar valor + def nome + @nome + end + + # Um método de classe usa a palavra chave self para se defenciar dos métodos de instancia. + # Ele só pode ser chamado na classe, não na instancia + def self.diz(msg) + puts "#{msg}" + end + + def especies + @@especies + end + +end + + +# Instantiate a class +# Instaciando uma classe +jim = Human.new("Jim Halpert") +jim = Humano.new("Jim Halpert") + +dwight = Human.new("Dwight K. Schrute") +dwight = Humano.new("Dwight K. Schrute") + +# Let's call a couple of methods +# Vamos chamar um par de métodos +jim.species #=> "H. sapiens" +jim.especies #=> "H. sapiens" + +jim.name #=> "Jim Halpert" +jim.nome #=> "Jim Halpert" + +jim.name = "Jim Halpert II" #=> "Jim Halpert II" +jim.nome = "Jim Halpert II" #=> "Jim Halpert II" + +jim.name #=> "Jim Halpert II" +jim.nome #=> "Jim Halpert II" + +dwight.species #=> "H. sapiens" +dwight.especies #=> "H. sapiens" + +dwight.name #=> "Dwight K. Schrute" +dwight.nome #=> "Dwight K. Schrute" + +# Call the class method +# Chamar o método de classe +Human.say("Hi") #=> "Hi" +Humano.diz("Oi") #=> "Oi" + +# Class also is object in ruby. So class can have instance variables. +# Uma classe também é objeto em Ruby. Então uma classe pode possuir um variavel de instancia +# Class variable is shared among the class and all of its descendants. +# Variavies de classe são compartilhadas entre a classe e todos os seus descendentes. + +# base class +class Human + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + + +# Classe base +class Humano + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + +# classe filha +class Trabalhador < Humano +end + +Human.foo # 0 +Humano.foo # 0 +Worker.foo # 0 +Trabalhador.foo # 0 + +Human.foo = 2 # 2 +Humano.foo = 2 # 2 +Worker.foo # 2 +Trabalhador.foo # 2 + +# Class instance variable is not shared by the class's descendants. +# Uma variavel de instancia não é compartilhada por suas classes decendentes. + +class Human + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Humano + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Doctor < Human +end + +class Doutor < Humano +end + +Humano.bar # 0 +Doutor.bar # nil + +``` -- cgit v1.2.3 From 4283f0c964f55295f024e535a2373ef8d04e4069 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:32:20 +0200 Subject: [FIX] filename --- objective-c.html.markdown | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 objective-c.html.markdown diff --git a/objective-c.html.markdown b/objective-c.html.markdown new file mode 100644 index 00000000..787a9219 --- /dev/null +++ b/objective-c.html.markdown @@ -0,0 +1,99 @@ +--- +language: Objectiv-C +author: Eugene Yagrushkin +author_url: www.about.me/yagrushkin +filename: learnc.Objectiv-C +--- + +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. +It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. + +```Objective-C +// Single-line comments start with // + +/* +Multi-line comments look like this. +*/ + +##Basic types +// all the primitive variable types are the same as in C +// char, int, long, double, float + + +// Simple, common classes +// number +NSNumber *firstNumber = @1; +NSNumber *secondNumber = @23.0; +NSNumber *boolNumber = @YES; + +// string +NSString *aString = @"some string"; + +// array +NSArray *array = @[ @1, @2]; + +// dictionary +NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; + +// Import headers with #import +#import +#import "SomeAppDelegate.h" + +##Coding classes + +// Declare your class in a header(.h) file: + +@interface UserObject : NSObject{ +// instance variables +} + +// Class method + + (NSString*) ClassMethod; + +// Instance method + - (NSString*) instanceMethodWithParmeter:(NSString*)string; + +@end + +// Add class methods in an implementation (.m) file: + +@implementation UserObject + ++ (NSString*) ClassMethod{ + return @"SomeString"; +} + +- (NSString*) instanceMethodWithParmeter:(NSString*)string; +{ + return @"New string"; +} + +- (NSString*) otherMethodWithString:(NSString*)string; +{ + return [NSString stringWithString:string]; +} +@end + +// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. +UserObject *someObject = [[UserObject alloc] init]; + +##Calling Methods + +// The Objective-C model of object-oriented programming is based on message passing to object instances. +// In Objective-C one does not simply call a method; one sends a message. + +[someObject instanceMethodWithParmeter:@"Steve Jobs"]; + +##Nested Messages +// nested messages look like this: + +[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; + +``` +## Further Reading + +[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) + +[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) + +[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 6d48842ce96919774e468093e7460ef9779ab341 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:32:57 +0200 Subject: [FIX] filename --- objvectiv-C.html.markdown | 99 ----------------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 objvectiv-C.html.markdown diff --git a/objvectiv-C.html.markdown b/objvectiv-C.html.markdown deleted file mode 100644 index 787a9219..00000000 --- a/objvectiv-C.html.markdown +++ /dev/null @@ -1,99 +0,0 @@ ---- -language: Objectiv-C -author: Eugene Yagrushkin -author_url: www.about.me/yagrushkin -filename: learnc.Objectiv-C ---- - -Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. -It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. - -```Objective-C -// Single-line comments start with // - -/* -Multi-line comments look like this. -*/ - -##Basic types -// all the primitive variable types are the same as in C -// char, int, long, double, float - - -// Simple, common classes -// number -NSNumber *firstNumber = @1; -NSNumber *secondNumber = @23.0; -NSNumber *boolNumber = @YES; - -// string -NSString *aString = @"some string"; - -// array -NSArray *array = @[ @1, @2]; - -// dictionary -NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; - -// Import headers with #import -#import -#import "SomeAppDelegate.h" - -##Coding classes - -// Declare your class in a header(.h) file: - -@interface UserObject : NSObject{ -// instance variables -} - -// Class method - + (NSString*) ClassMethod; - -// Instance method - - (NSString*) instanceMethodWithParmeter:(NSString*)string; - -@end - -// Add class methods in an implementation (.m) file: - -@implementation UserObject - -+ (NSString*) ClassMethod{ - return @"SomeString"; -} - -- (NSString*) instanceMethodWithParmeter:(NSString*)string; -{ - return @"New string"; -} - -- (NSString*) otherMethodWithString:(NSString*)string; -{ - return [NSString stringWithString:string]; -} -@end - -// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. -UserObject *someObject = [[UserObject alloc] init]; - -##Calling Methods - -// The Objective-C model of object-oriented programming is based on message passing to object instances. -// In Objective-C one does not simply call a method; one sends a message. - -[someObject instanceMethodWithParmeter:@"Steve Jobs"]; - -##Nested Messages -// nested messages look like this: - -[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; - -``` -## Further Reading - -[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) - -[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) - -[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 5b182eb5e99be7d04071cdb04466ccdae7b2fde9 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:38:39 +0200 Subject: [UPDATE] Authors --- objective-c.html.markdown | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 787a9219..df789677 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -1,8 +1,11 @@ --- -language: Objectiv-C -author: Eugene Yagrushkin -author_url: www.about.me/yagrushkin -filename: learnc.Objectiv-C + +language: Objective-C +contributors: + - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] + - ["Yannick Loriot", "https://github.com/YannickL"] +filename: LearnObjectiveC.m + --- Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. -- cgit v1.2.3 From d842eb4f819568c1503c26e494e271f1cd179542 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:45:49 +0200 Subject: minor fixes --- objective-c.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index df789677..21460632 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -8,8 +8,8 @@ filename: LearnObjectiveC.m --- -Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. -It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. +It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. ```Objective-C // Single-line comments start with // @@ -97,6 +97,6 @@ UserObject *someObject = [[UserObject alloc] init]; [Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) -[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) +[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/) [iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 48fcef441fce2235e5dcd0d7c052b44f315504a5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:56:09 +0200 Subject: [ADD] Hello World! --- objective-c.html.markdown | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 21460632..63aa64f1 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -18,7 +18,26 @@ It is a general-purpose, object-oriented programming language that adds Smalltal Multi-line comments look like this. */ -##Basic types +// Imports the Foundation headers with #import +#import + +// Your program's entry point is a function called +// main with an integer return type. +int main (int argc, const char * argv[]) +{ + // Create an autorelease pool to manage the memory into your program + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // Print "Hello World!" to the console + NSLog(@"Hello World!"); + + // Clean up the memory you used into your program + [pool drain]; + + // End your program + return 0; +} + // all the primitive variable types are the same as in C // char, int, long, double, float -- cgit v1.2.3 From a29c4ee753894d1b58fa398d9f49567d098d6221 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:25:47 +0200 Subject: [Refactoring] Literals --- objective-c.html.markdown | 80 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 63aa64f1..2b8e9874 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -28,72 +28,80 @@ int main (int argc, const char * argv[]) // Create an autorelease pool to manage the memory into your program NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - // Print "Hello World!" to the console - NSLog(@"Hello World!"); + // Use NSLog to print lines to the console + NSLog(@"Hello World!"); // Print "Hello World!" - // Clean up the memory you used into your program - [pool drain]; - - // End your program - return 0; -} + // character literals + NSNumber *theLetterZ = @'Z'; -// all the primitive variable types are the same as in C -// char, int, long, double, float + // integral literals + NSNumber *fortyTwo = @42; + NSNumber *fortyTwoUnsigned = @42U; + NSNumber *fortyTwoLong = @42L; + NSNumber *fortyTwoLongLong = @42LL; + // floating point literals + NSNumber *piFloat = @3.141592654F; + NSNumber *piDouble = @3.1415926535; -// Simple, common classes -// number -NSNumber *firstNumber = @1; -NSNumber *secondNumber = @23.0; -NSNumber *boolNumber = @YES; + // BOOL literals + NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES] + NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO] -// string -NSString *aString = @"some string"; + // strings + NSString *helloString = @"hello"; -// array -NSArray *array = @[ @1, @2]; + // array + NSArray *anArray = @[@1, @2]; -// dictionary -NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; + // dictionary + NSDictionay *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; -// Import headers with #import -#import -#import "SomeAppDelegate.h" + // Clean up the memory you used into your program + [pool drain]; + + // End your program + return 0; +} -##Coding classes +/////////////////////////////////////// +// Classes And Functions +/////////////////////////////////////// // Declare your class in a header(.h) file: -@interface UserObject : NSObject{ -// instance variables +@interface UserObject : NSObject +{ + // instance variables } // Class method - + (NSString*) ClassMethod; ++ (NSString *)classMethod; // Instance method - - (NSString*) instanceMethodWithParmeter:(NSString*)string; +- (NSString *)instanceMethodWithParmeter:(NSString *)string; @end -// Add class methods in an implementation (.m) file: +// Implement the methods in an implementation (.m) file: @implementation UserObject -+ (NSString*) ClassMethod{ - return @"SomeString"; ++ (NSString *)classMethod +{ + return @"SomeString"; } -- (NSString*) instanceMethodWithParmeter:(NSString*)string; +- (NSString *)instanceMethodWithParmeter:(NSString *)string { - return @"New string"; + return @"New string"; } -- (NSString*) otherMethodWithString:(NSString*)string; +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number { - return [NSString stringWithString:string]; + return @42; } + @end // Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. -- cgit v1.2.3 From 0bd403fdb935331c3c391ffd79f0245032dee3b5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:50:09 +0200 Subject: [UPDATE] Literals Examples --- objective-c.html.markdown | 63 ++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 2b8e9874..284eca92 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -29,33 +29,52 @@ int main (int argc, const char * argv[]) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // Use NSLog to print lines to the console - NSLog(@"Hello World!"); // Print "Hello World!" + NSLog(@"Hello World!"); // Print the string "Hello World!" - // character literals + // String object + NSString *worldString = @"World"; + // %@ is an object + NSLog(@"Hello %@!", worldString); // Print "Hello World!" + + // Character literals NSNumber *theLetterZ = @'Z'; + NSLog(@"%c", [theLetterZ charValue]); - // integral literals - NSNumber *fortyTwo = @42; - NSNumber *fortyTwoUnsigned = @42U; - NSNumber *fortyTwoLong = @42L; - NSNumber *fortyTwoLongLong = @42LL; - - // floating point literals - NSNumber *piFloat = @3.141592654F; - NSNumber *piDouble = @3.1415926535; + // Integral literals + NSNumber *fortyTwoNumber = @42; + int fortyTwo = [fortyTwo intValue]; + NSLog(@"%i", fortyTwo); + + NSNumber *fortyTwoUnsignedNumber = @42U; + unsigned int fortyTwoUnsigned = [fortyTwoUnsigned unsignedIntValue]; + NSLog(@"%u", fortyTwoUnsigned); + + NSNumber *fortyTwoLongNumber = @42L; + long fortyTwoLong = [aLong longValue]; + NSLog(@"%li", fortyTwoLong); + + // Floating point literals + NSNumber *piFloatNumber = @3.141592654F; + float piFloat = [piFloat floatValue]; + NSLog(@"%f", piFloat); + + NSNumber *piDoubleNumber = @3.1415926535; + piDouble = [piDouble doubleValue]; + NSLog(@"%f", piDouble); // BOOL literals - NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES] - NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO] - - // strings - NSString *helloString = @"hello"; - - // array - NSArray *anArray = @[@1, @2]; - - // dictionary - NSDictionay *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; + NSNumber *yesNumber = @YES; + NSNumber *noNumber = @NO; + + // Array object + NSArray *anArray = @[@1, @2, @3, @4]; + NSNumber *thirdNumber = anArray[2]; + NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3" + + // Dictionary object + NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; + NSObject *valueObject = aDictionary[@"A Key"]; + NSLog(@"Object = %@", valueObject); // Print "Object = (null)" // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 947c137680c2699bdb118b63460c560bac1fdd3c Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:52:11 +0200 Subject: minor fixes --- objective-c.html.markdown | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 284eca92..fc4b2900 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -37,29 +37,30 @@ int main (int argc, const char * argv[]) NSLog(@"Hello %@!", worldString); // Print "Hello World!" // Character literals - NSNumber *theLetterZ = @'Z'; - NSLog(@"%c", [theLetterZ charValue]); + NSNumber *theLetterZNumber = @'Z'; + char theLetterZ = [theLetterZNumber charValue]; + NSLog(@"%c", theLetterZ); // Integral literals NSNumber *fortyTwoNumber = @42; - int fortyTwo = [fortyTwo intValue]; + int fortyTwo = [fortyTwoNumber intValue]; NSLog(@"%i", fortyTwo); NSNumber *fortyTwoUnsignedNumber = @42U; - unsigned int fortyTwoUnsigned = [fortyTwoUnsigned unsignedIntValue]; + unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; NSLog(@"%u", fortyTwoUnsigned); NSNumber *fortyTwoLongNumber = @42L; - long fortyTwoLong = [aLong longValue]; + long fortyTwoLong = [fortyTwoLongNumber longValue]; NSLog(@"%li", fortyTwoLong); // Floating point literals NSNumber *piFloatNumber = @3.141592654F; - float piFloat = [piFloat floatValue]; + float piFloat = [piFloatNumber floatValue]; NSLog(@"%f", piFloat); NSNumber *piDoubleNumber = @3.1415926535; - piDouble = [piDouble doubleValue]; + piDouble = [piDoubleNumber doubleValue]; NSLog(@"%f", piDouble); // BOOL literals -- cgit v1.2.3 From 0040ce616b4e27a182834cd2fd03aacc2561a198 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:04:20 +0200 Subject: [UPDATE] Object Declaration --- objective-c.html.markdown | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index fc4b2900..c3df514b 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -25,15 +25,28 @@ Multi-line comments look like this. // main with an integer return type. int main (int argc, const char * argv[]) { - // Create an autorelease pool to manage the memory into your program + // Create an autorelease pool to manage the memory into the program NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // Use NSLog to print lines to the console NSLog(@"Hello World!"); // Print the string "Hello World!" - // String object - NSString *worldString = @"World"; + /////////////////////////////////////// + // Types & Variables + /////////////////////////////////////// + + // Primitive declarations + int myPrimitive; + + // Object declarations + // Put the * in front of the variable names for strongly-typed object declarations + MyClass *myObject1; // Strong typing + id myObject2; // Weak typing // %@ is an object + NSLog(@"%@ and %@", myObject1, myObject2); // Print "(null) and (null)" + + // String + NSString *worldString = @"World"; NSLog(@"Hello %@!", worldString); // Print "Hello World!" // Character literals @@ -50,6 +63,10 @@ int main (int argc, const char * argv[]) unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; NSLog(@"%u", fortyTwoUnsigned); + NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42]; + short fortyTwoShort = [fortyTwoShortNumber shortValue]; + NSLog(@"%hi", fortyTwoShort); + NSNumber *fortyTwoLongNumber = @42L; long fortyTwoLong = [fortyTwoLongNumber longValue]; NSLog(@"%li", fortyTwoLong); @@ -77,10 +94,14 @@ int main (int argc, const char * argv[]) NSObject *valueObject = aDictionary[@"A Key"]; NSLog(@"Object = %@", valueObject); // Print "Object = (null)" + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + // Clean up the memory you used into your program [pool drain]; - // End your program + // End the program return 0; } -- cgit v1.2.3 From 25f4c7e80a4682db35daed75d42ca91eb1504736 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:07:58 +0200 Subject: minor fixes --- objective-c.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index c3df514b..ad2bedf9 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -36,14 +36,16 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Primitive declarations - int myPrimitive; + int myPrimitive1; + long myPrimitive2; // Object declarations // Put the * in front of the variable names for strongly-typed object declarations MyClass *myObject1; // Strong typing id myObject2; // Weak typing // %@ is an object - NSLog(@"%@ and %@", myObject1, myObject2); // Print "(null) and (null)" + // 'description' is a convention to display the value of the Objects + NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)" // String NSString *worldString = @"World"; -- cgit v1.2.3 From 0970cb8010e590332b86de26a4746c7202c22363 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:09:38 +0200 Subject: minor update --- objective-c.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index ad2bedf9..7f87da6f 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -36,13 +36,13 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Primitive declarations - int myPrimitive1; - long myPrimitive2; + int myPrimitive1 = 1; + long myPrimitive2 = 234554664565; // Object declarations // Put the * in front of the variable names for strongly-typed object declarations - MyClass *myObject1; // Strong typing - id myObject2; // Weak typing + MyClass *myObject1 = nil; // Strong typing + id myObject2 = nil; // Weak typing // %@ is an object // 'description' is a convention to display the value of the Objects NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)" -- cgit v1.2.3 From 24d9cde4883202d4b7c5db7dad8981b4a4000125 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:22:40 +0200 Subject: [NEW] Statements --- objective-c.html.markdown | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 7f87da6f..ebae2fc7 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -99,6 +99,78 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Operators /////////////////////////////////////// + + // The operators works like in the C language + // For example: + 3 == 2; // => 0 (NO) + 3 != 2; // => 1 (YES) + 1 && 1; // => 1 (Logical and) + 0 || 1; // => 1 (Logical or) + ~0x0F; // => 0xF0 (bitwise negation) + 0x0F & 0xF0; // => 0x00 (bitwise AND) + 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + + // If-Else statement + if (NO) + { + NSLog(@"I am never run"); + } else if (0) + { + NSLog(@"I am also never run"); + } else + { + NSLog(@"I print"); + } + + // Switch statement + switch (2) { + case 0: + { + NSLog(@"I am never run"); + } break; + case 1: + { + NSLog(@"I am also never run"); + } break; + default: + { + NSLog(@"I print"); + } break; + } + + // While loops exist + int ii = 0; + while (ii < 4) + { + NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. + } // => prints "0, + 1, + 2, + 3," + + // For loops too + int jj; + for (jj=0; jj < 4; jj++) + { + NSLog(@"%d,", ii++); + } // => prints "0, + 1, + 2, + 3," + + // Foreach + NSArray *values = @[@0, @1, @2, @3]; + for (NSNumber *value in values) + { + NSLog(@"%@,", value); + } // => prints "0, + 1, + 2, + 3," // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 5d800b25e427417c589f3cf2c3b19c18c178a11f Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:32:16 +0200 Subject: [NEW] Try-Catch-Finally --- objective-c.html.markdown | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index ebae2fc7..479d9ad5 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -102,6 +102,8 @@ int main (int argc, const char * argv[]) // The operators works like in the C language // For example: + 2 + 5; // => 7 + 4.2f + 5.1f; // => 9.3f 3 == 2; // => 0 (NO) 3 != 2; // => 1 (YES) 1 && 1; // => 1 (Logical and) @@ -127,7 +129,8 @@ int main (int argc, const char * argv[]) } // Switch statement - switch (2) { + switch (2) + { case 0: { NSLog(@"I am never run"); @@ -142,36 +145,50 @@ int main (int argc, const char * argv[]) } break; } - // While loops exist + // While loops statements int ii = 0; while (ii < 4) { NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," - // For loops too + // For loops statements int jj; for (jj=0; jj < 4; jj++) { NSLog(@"%d,", ii++); - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," - // Foreach + // Foreach statements NSArray *values = @[@0, @1, @2, @3]; for (NSNumber *value in values) { NSLog(@"%@,", value); - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," + // Try-Catch-Finally statements + @try + { + // Your statements here + @throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil]; + } @catch (NSException * e) + { + NSLog(@"Exception: %@", e); + } @finally + { + NSLog(@"Finally"); + } // => prints "Exception: File Not Found on System" + "Finally" + // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 50c3526247810078b6c63e2cbaf79bc226e873dc Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:34:12 +0200 Subject: minor updates --- objective-c.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 479d9ad5..6eac69a8 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -151,9 +151,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // For loops statements int jj; @@ -161,9 +161,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%d,", ii++); } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // Foreach statements NSArray *values = @[@0, @1, @2, @3]; @@ -171,9 +171,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%@,", value); } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // Try-Catch-Finally statements @try @@ -187,7 +187,7 @@ int main (int argc, const char * argv[]) { NSLog(@"Finally"); } // => prints "Exception: File Not Found on System" - "Finally" + // "Finally" // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 0d41a6405627b50f2839df3a3b019836f361ecc0 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:46:16 +0200 Subject: [UPDATE] Object Creation --- objective-c.html.markdown | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 6eac69a8..f43081cf 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -201,27 +201,56 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Declare your class in a header(.h) file: - -@interface UserObject : NSObject +// Class Declaration Syntax: +// @interface : +// { +// Member variable declarations; +// } +// -/+ (type) Method declarations; +// @end +@interface MyClass : NSObject { - // instance variables + int count; + id data; + NSString *name; } +// Create the public getter/setter for the variable count +@property(assign) int count; + +// Methods ++/- (return type)methodSignature:(Parameter Type *)parameterName; -// Class method +// + for class method + (NSString *)classMethod; -// Instance method +// - for instance method - (NSString *)instanceMethodWithParmeter:(NSString *)string; - +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; +- @end // Implement the methods in an implementation (.m) file: @implementation UserObject +// Constructors are a way of creating classes +// This is a default constructor +- (id)init +{ + if ((self = [super init])) + { + self.count = 1; + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + UserObject *someObject = [[UserObject alloc] init]; + } + return self; +} + + (NSString *)classMethod { - return @"SomeString"; + return [[self alloc] init]; } - (NSString *)instanceMethodWithParmeter:(NSString *)string @@ -236,9 +265,6 @@ int main (int argc, const char * argv[]) @end -// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. -UserObject *someObject = [[UserObject alloc] init]; - ##Calling Methods // The Objective-C model of object-oriented programming is based on message passing to object instances. -- cgit v1.2.3 From 3fe1c3c8a562427533439f385df952a00b36a998 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:49:23 +0200 Subject: minor update --- objective-c.html.markdown | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index f43081cf..22791659 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -189,6 +189,18 @@ int main (int argc, const char * argv[]) } // => prints "Exception: File Not Found on System" // "Finally" + /////////////////////////////////////// + // Objects + /////////////////////////////////////// + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + MyClass *myObject = [[MyClass alloc] init]; + + // The Objective-C model of object-oriented programming is based on message passing to object instances. + // In Objective-C one does not simply call a method; one sends a message. + [myObject instanceMethodWithParmeter:@"Steve Jobs"]; + // Clean up the memory you used into your program [pool drain]; @@ -240,10 +252,6 @@ int main (int argc, const char * argv[]) if ((self = [super init])) { self.count = 1; - - // Create an object instance by allocating memory and initializing it. - // An object is not fully functional until both steps have been completed. - UserObject *someObject = [[UserObject alloc] init]; } return self; } @@ -265,18 +273,6 @@ int main (int argc, const char * argv[]) @end -##Calling Methods - -// The Objective-C model of object-oriented programming is based on message passing to object instances. -// In Objective-C one does not simply call a method; one sends a message. - -[someObject instanceMethodWithParmeter:@"Steve Jobs"]; - -##Nested Messages -// nested messages look like this: - -[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; - ``` ## Further Reading -- cgit v1.2.3 From 664d592bc79c7dcc6c429bcee79965ef3df464f5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:50:22 +0200 Subject: minor change --- objective-c.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 22791659..9d1178e1 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -20,6 +20,7 @@ Multi-line comments look like this. // Imports the Foundation headers with #import #import +#import "MyClass.h" // Your program's entry point is a function called // main with an integer return type. @@ -212,7 +213,7 @@ int main (int argc, const char * argv[]) // Classes And Functions /////////////////////////////////////// -// Declare your class in a header(.h) file: +// Declare your class in a header(MyClass.h) file: // Class Declaration Syntax: // @interface : // { @@ -241,7 +242,7 @@ int main (int argc, const char * argv[]) - @end -// Implement the methods in an implementation (.m) file: +// Implement the methods in an implementation (MyClass.m) file: @implementation UserObject -- cgit v1.2.3 From 0932765947b14407aa41fbe7ded00ca37a25f5c6 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:58:13 +0200 Subject: [NEW] Protocol Implementation --- objective-c.html.markdown | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 9d1178e1..187ea30a 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -215,20 +215,22 @@ int main (int argc, const char * argv[]) // Declare your class in a header(MyClass.h) file: // Class Declaration Syntax: -// @interface : +// @interface ClassName : ParentClassName // { // Member variable declarations; // } // -/+ (type) Method declarations; // @end -@interface MyClass : NSObject +@interface MyClass : NSObject { int count; id data; NSString *name; } -// Create the public getter/setter for the variable count -@property(assign) int count; +// Convenience notation to auto generate public getter and setter +@property int count; +@property (copy) NSString *name; // Copy the object during assignment. +@property (readonly) id data; // Declare only a getter method. // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; @@ -246,8 +248,13 @@ int main (int argc, const char * argv[]) @implementation UserObject +// Call when the object is releasing +- (void)dealloc +{ +} + // Constructors are a way of creating classes -// This is a default constructor +// This is a default constructor which is call when the object is creating - (id)init { if ((self = [super init])) @@ -272,8 +279,25 @@ int main (int argc, const char * argv[]) return @42; } +// Methods declared into MyProtocol +- (void)myProtocolMethod +{ + // statements +} + @end +/* + * A protocol declares methods that can be implemented by any class. + * Protocols are not classes themselves. They simply define an interface + * that other objects are responsible for implementing. + * / +@protocol MyProtocol + - (void)myProtocolMethod; +@end + + + ``` ## Further Reading -- cgit v1.2.3 From dbf07b80ac03648ebcec89d07eac279faa1d0cb6 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 08:19:56 -0700 Subject: Scheme encoding for cl --- common-lisp.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index 24b6947f..e174e0df 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -17,7 +17,7 @@ Another popular and recent book is -```commonlisp +```scheme ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 0. Syntax @@ -589,6 +589,7 @@ nil ; for false - and the empty list ;; regular code. ;; See Practical Common Lisp for more information on macros. +``` ## Further Reading -- cgit v1.2.3 From 93fc8f5e8044c404d1feb600cf50eb932b596325 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 10:10:49 -0700 Subject: Line length edits to C# --- csharp.html.markdown | 973 ++++++++++++++++++++++++++------------------------- 1 file changed, 487 insertions(+), 486 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index e079571e..c254b5a9 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -28,492 +28,493 @@ using System.Collections.Generic; // defines scope to organize code into "packages" namespace Learning { - // Each .cs file should at least contain a class with the same name as the file - // you're allowed to do otherwise, but shouldn't for sanity. - public class LearnCSharp - { - // A console application must have a main method as an entry point - public static void Main(string[] args) - { - // Use Console.WriteLine to print lines - Console.WriteLine("Hello World"); - Console.WriteLine( - "Integer: " + 10 + - " Double: " + 3.14 + - " Boolean: " + true); - - // To print without a new line, use Console.Write - Console.Write("Hello "); - Console.Write("World"); - - - /////////////////////////////////////////////////// - // Types & Variables - // - // Declare a variable using - /////////////////////////////////////////////////// - - // Sbyte - Signed 8-bit integer - // (-128 <= sbyte <= 127) - sbyte fooSbyte = 100; - - // Byte - Unsigned 8-bit integer - // (0 <= byte <= 255) - byte fooByte = 100; - - // Short - Signed 16-bit integer - // (-32,768 <= short <= 32,767) - short fooShort = 10000; - - // Ushort - Unsigned 16-bit integer - // (0 <= ushort <= 65,535) - ushort fooUshort = 10000; - - // Integer - Signed 32-bit integer - // (-2,147,483,648 <= int <= 2,147,483,647) - int fooInt = 1; - - // Uinteger - Unsigned 32-bit integer - // (0 <= uint <= 4,294,967,295) - uint fooUint = 1; - - // Long - Signed 64-bit integer - // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) - long fooLong = 100000L; - // L is used to denote that this variable value is of type long or ulong - // anything without is treated as int or uint depending on size. - - // Ulong - Unsigned 64-bit integer - // (0 <= ulong <= 18,446,744,073,709,551,615) - ulong fooUlong = 100000L; - - // Float - Single-precision 32-bit IEEE 754 Floating Point - // Precision: 7 digits - float fooFloat = 234.5f; - // f is used to denote that this variable value is of type float; - // otherwise it is treated as double. - - // Double - Double-precision 64-bit IEEE 754 Floating Point - // Precision: 15-16 digits - double fooDouble = 123.4; - - // Bool - true & false - bool fooBoolean = true; - bool barBoolean = false; - - // Char - A single 16-bit Unicode character - char fooChar = 'A'; - - // Strings - string fooString = "My string is here!"; - Console.WriteLine(fooString); - - // formatting - string fooFormattedString = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); - Console.WriteLine(fooFormattedString); - - // formatting dates - DateTime fooDate = DateTime.Now; - Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); - - // \n is an escaped character that starts a new line - string barString = "Printing on a new line?\nNo Problem!"; - Console.WriteLine(barString); - - // it can be written prettier by using the @ symbol - string bazString = @"Here's some stuff - on a new line!"; - Console.WriteLine(bazString); - - // quotes need to be escaped - // use \" normally - string quotedString = "some \"quoted\" stuff"; - Console.WriteLine(quotedString); - - // use "" when strings start with @ - string quotedString2 = @"some MORE ""quoted"" stuff"; - Console.WriteLine(quotedString2); - - // Use const or read-only to make a variable immutable - // const values are calculated at compile time - const int HOURS_I_WORK_PER_WEEK = 9001; - - // Nullable types - // any type can be made nullable by suffixing a ? - // ? = - int? nullable = null; - Console.WriteLine("Nullable variable: " + nullable); - - // ?? is syntactic sugar for specifying default value - // in case variable is null - int notNullable = nullable ?? 0; - Console.WriteLine("Not nullable variable: " + notNullable); - - // Var - compiler will choose the most appropriate type based on value - var fooImplicit = true; - - /////////////////////////////////////////////////// - // Data Structures - /////////////////////////////////////////////////// - Console.WriteLine("\n->Data Structures"); - - // Arrays - // The array size must be decided upon declaration - // The format for declaring an array is follows: - // [] = new []; - int[] intArray = new int[10]; - string[] stringArray = new string[1]; - bool[] boolArray = new bool[100]; - - // Another way to declare & initialize an array - int[] y = { 9000, 1000, 1337 }; - - // Indexing an array - Accessing an element - Console.WriteLine("intArray @ 0: " + intArray[0]); - - // Arrays are zero-indexed and mutable. - intArray[1] = 1; - Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1 - - // Lists - // Lists are used more frequently than arrays as they are more flexible - // The format for declaring a list is follows: - // List = new List(); - List intList = new List(); - List stringList = new List(); - - // Another way to declare & initialize a list - List z = new List { 9000, 1000, 1337 }; - - // Indexing a list - Accessing an element - // Lists are zero-indexed and mutable. - Console.WriteLine("z @ 0: " + z[2]); - - // Lists don't default to a value; - // A value must be added before accessing the index - intList.Add(1); - Console.WriteLine("intList @ 0: " + intList[0]); - - - // Others data structures to check out: - // - // Stack/Queue - // Dictionary - // Read-only Collections - // Tuple (.Net 4+) - - - /////////////////////////////////////// - // Operators - /////////////////////////////////////// - Console.WriteLine("\n->Operators"); - - int i1 = 1, i2 = 2; // Shorthand for multiple declarations - - // Arithmetic is straightforward - Console.WriteLine("1+2 = " + (i1 + i2)); // => 3 - Console.WriteLine("2-1 = " + (i2 - i1)); // => 1 - Console.WriteLine("2*1 = " + (i2 * i1)); // => 2 - Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down) - - // Modulo - Console.WriteLine("11%3 = " + (11 % 3)); // => 2 - - // Comparison operators - Console.WriteLine("3 == 2? " + (3 == 2)); // => false - Console.WriteLine("3 != 2? " + (3 != 2)); // => true - Console.WriteLine("3 > 2? " + (3 > 2)); // => true - Console.WriteLine("3 < 2? " + (3 < 2)); // => false - Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true - Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true - - // Bitwise operators! - /* - ~ Unary bitwise complement - << Signed left shift - >> Signed right shift - >>> Unsigned right shift - & Bitwise AND - ^ Bitwise exclusive OR - | Bitwise inclusive OR - */ - - // Incrementations - int i = 0; - Console.WriteLine("\n->Inc/Dec-rementation"); - Console.WriteLine(i++); //i = 1. Post-Incrementation - Console.WriteLine(++i); //i = 2. Pre-Incrementation - Console.WriteLine(i--); //i = 1. Post-Decrementation - Console.WriteLine(--i); //i = 0. Pre-Decrementation - - - /////////////////////////////////////// - // Control Structures - /////////////////////////////////////// - Console.WriteLine("\n->Control Structures"); - - // If statements are c-like - int j = 10; - if (j == 10) - { - Console.WriteLine("I get printed"); - } - else if (j > 10) - { - Console.WriteLine("I don't"); - } - else - { - Console.WriteLine("I also don't"); - } - - // Ternary operators - // A simple if/else can be written as follows - // ? : - string isTrue = (true) ? "True" : "False"; - Console.WriteLine("Ternary demo: " + isTrue); - - - // While loop - int fooWhile = 0; - while (fooWhile < 100) - { - //Console.WriteLine(fooWhile); - //Increment the counter - //Iterated 99 times, fooWhile 0->99 - fooWhile++; - } - Console.WriteLine("fooWhile Value: " + fooWhile); - - // Do While Loop - int fooDoWhile = 0; - do - { - //Console.WriteLine(fooDoWhile); - //Increment the counter - //Iterated 99 times, fooDoWhile 0->99 - fooDoWhile++; - } while (fooDoWhile < 100); - Console.WriteLine("fooDoWhile Value: " + fooDoWhile); - - // For Loop - int fooFor; - //for loop structure => for(; ; ) - for (fooFor = 0; fooFor < 10; fooFor++) - { - //Console.WriteLine(fooFor); - //Iterated 10 times, fooFor 0->9 - } - Console.WriteLine("fooFor Value: " + fooFor); - - // Switch Case - // A switch works with the byte, short, char, and int data types. - // It also works with enumerated types (discussed in Enum Types), - // the String class, and a few special classes that wrap - // primitive types: Character, Byte, Short, and Integer. - int month = 3; - string monthString; - switch (month) - { - case 1: - monthString = "January"; - break; - case 2: - monthString = "February"; - break; - case 3: - monthString = "March"; - break; - default: - monthString = "Some other month"; - break; - } - Console.WriteLine("Switch Case Result: " + monthString); - - - /////////////////////////////////////// - // Converting Data Types And Typcasting - /////////////////////////////////////// - - // Converting data - - // Convert String To Integer - // this will throw an Exception on failure - int.Parse("123");//returns an integer version of "123" - - // try parse will default to type default on failure - // in this case: 0 - int tryInt; - int.TryParse("123", out tryInt); - - // Convert Integer To String - // Convert class has a number of methods to facilitate conversions - Convert.ToString(123); - - /////////////////////////////////////// - // Classes And Functions - /////////////////////////////////////// - - Console.WriteLine("\n->Classes & Functions"); - - // (definition of the Bicycle class follows) - - // Use new to instantiate a class - Bicycle trek = new Bicycle(); - - // Call object methods - trek.speedUp(3); // You should always use setter and getter methods - trek.setCadence(100); - - // ToString is a convention to display the value of this Object. - Console.WriteLine("trek info: " + trek.ToString()); - - // Instantiate another new Bicycle - Bicycle octo = new Bicycle(5, 10); - Console.WriteLine("octo info: " + octo.ToString()); - - // Instantiate a new Penny Farthing - PennyFarthing funbike = new PennyFarthing(1, 10); - Console.WriteLine("funbike info: " + funbike.ToString()); - - Console.Read(); - } // End main method - - - } // End LearnCSharp class - - // You can include other classes in a .cs file - - - // Class Declaration Syntax: - // class { - // //data fields, constructors, functions all inside. - // //functions are called as methods in Java. - // } - - public class Bicycle - { - // Bicycle's Fields/Variables - public int cadence; // Public: Can be accessed from anywhere - private int _speed; // Private: Only accessible from within the class - protected int gear; // Protected: Accessible from the class and subclasses - internal int wheels; // Internal: Accessible from within the assembly - string name; // default: Only accessible from within this class - - // readonly values are set at run time - // they can only be assigned upon declaration or in a constructor - readonly bool hasCardsInSpokes = false; // read-only private - - // Constructors are a way of creating classes - // This is a default constructor - public Bicycle() - { - gear = 1; - cadence = 50; - _speed = 5; - name = "Bontrager"; - } - - // This is a specified constructor (it contains arguments) - public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes) - { - this.gear = startGear; - this.cadence = startCadence; - this._speed = startSpeed; - this.name = name; - this.hasCardsInSpokes = hasCardsInSpokes; - } - - // Constructors can be chained - public Bicycle(int startCadence, int startSpeed) : - this(startCadence, startSpeed, 0, "big wheels", true) - { - } - - // Function Syntax: - // () - - // classes can implement getters and setters for their fields - // or they can implement properties - - // Method declaration syntax: - // () - public int getCadence() - { - return cadence; - } - - // void methods require no return statement - public void setCadence(int newValue) - { - cadence = newValue; - } - - // virtual keyword indicates this method can be overridden - public virtual void setGear(int newValue) - { - gear = newValue; - } - - public void speedUp(int increment) - { - _speed += increment; - } - - public void slowDown(int decrement) - { - _speed -= decrement; - } - - // properties get/set values - // when only data needs to be accessed, consider using properties. - // properties may have either get or set, or both - private bool _hasTassles; // private variable - public bool hasTassles // public accessor - { - get { return _hasTassles; } - set { _hasTassles = value; } - } - - private int _frameSize; - public int FrameSize - { - get { return _frameSize; } - // you are able to specify access modifiers for either get or set - // this means only Bicycle class can call set on Framesize - private set { _frameSize = value; } - } - - //Method to display the attribute values of this Object. - public override string ToString() - { - return "gear: " + gear + - " cadence: " + cadence + - " speed: " + _speed + - " name: " + name + - " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") + - "\n------------------------------\n" - ; - } - } // end class Bicycle - - // PennyFarthing is a subclass of Bicycle - class PennyFarthing : Bicycle - { - // (Penny Farthings are those bicycles with the big front wheel. - // They have no gears.) - - // calling parent constructor - public PennyFarthing(int startCadence, int startSpeed) : - base(startCadence, startSpeed, 0, "PennyFarthing", true) - { - } - - public override void setGear(int gear) - { - gear = 0; - } - } + // Each .cs file should at least contain a class with the same name as the file + // you're allowed to do otherwise, but shouldn't for sanity. + public class LearnCSharp + { + // A console application must have a main method as an entry point + public static void Main(string[] args) + { + // Use Console.WriteLine to print lines + Console.WriteLine("Hello World"); + Console.WriteLine( + "Integer: " + 10 + + " Double: " + 3.14 + + " Boolean: " + true); + + // To print without a new line, use Console.Write + Console.Write("Hello "); + Console.Write("World"); + + + /////////////////////////////////////////////////// + // Types & Variables + // + // Declare a variable using + /////////////////////////////////////////////////// + + // Sbyte - Signed 8-bit integer + // (-128 <= sbyte <= 127) + sbyte fooSbyte = 100; + + // Byte - Unsigned 8-bit integer + // (0 <= byte <= 255) + byte fooByte = 100; + + // Short - Signed 16-bit integer + // (-32,768 <= short <= 32,767) + short fooShort = 10000; + + // Ushort - Unsigned 16-bit integer + // (0 <= ushort <= 65,535) + ushort fooUshort = 10000; + + // Integer - Signed 32-bit integer + // (-2,147,483,648 <= int <= 2,147,483,647) + int fooInt = 1; + + // Uinteger - Unsigned 32-bit integer + // (0 <= uint <= 4,294,967,295) + uint fooUint = 1; + + // Long - Signed 64-bit integer + // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) + long fooLong = 100000L; + // L is used to denote that this variable value is of type long or ulong + // anything without is treated as int or uint depending on size. + + // Ulong - Unsigned 64-bit integer + // (0 <= ulong <= 18,446,744,073,709,551,615) + ulong fooUlong = 100000L; + + // Float - Single-precision 32-bit IEEE 754 Floating Point + // Precision: 7 digits + float fooFloat = 234.5f; + // f is used to denote that this variable value is of type float; + // otherwise it is treated as double. + + // Double - Double-precision 64-bit IEEE 754 Floating Point + // Precision: 15-16 digits + double fooDouble = 123.4; + + // Bool - true & false + bool fooBoolean = true; + bool barBoolean = false; + + // Char - A single 16-bit Unicode character + char fooChar = 'A'; + + // Strings + string fooString = "My string is here!"; + Console.WriteLine(fooString); + + // formatting + string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); + Console.WriteLine(fooFormattedString); + + // formatting dates + DateTime fooDate = DateTime.Now; + Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); + + // \n is an escaped character that starts a new line + string barString = "Printing on a new line?\nNo Problem!"; + Console.WriteLine(barString); + + // it can be written prettier by using the @ symbol + string bazString = @"Here's some stuff + on a new line!"; + Console.WriteLine(bazString); + + // quotes need to be escaped + // use \" normally + string quotedString = "some \"quoted\" stuff"; + Console.WriteLine(quotedString); + + // use "" when strings start with @ + string quotedString2 = @"some MORE ""quoted"" stuff"; + Console.WriteLine(quotedString2); + + // Use const or read-only to make a variable immutable + // const values are calculated at compile time + const int HOURS_I_WORK_PER_WEEK = 9001; + + // Nullable types + // any type can be made nullable by suffixing a ? + // ? = + int? nullable = null; + Console.WriteLine("Nullable variable: " + nullable); + + // ?? is syntactic sugar for specifying default value + // in case variable is null + int notNullable = nullable ?? 0; + Console.WriteLine("Not nullable variable: " + notNullable); + + // Var - compiler will choose the most appropriate type based on value + var fooImplicit = true; + + /////////////////////////////////////////////////// + // Data Structures + /////////////////////////////////////////////////// + Console.WriteLine("\n->Data Structures"); + + // Arrays + // The array size must be decided upon declaration + // The format for declaring an array is follows: + // [] = new []; + int[] intArray = new int[10]; + string[] stringArray = new string[1]; + bool[] boolArray = new bool[100]; + + // Another way to declare & initialize an array + int[] y = { 9000, 1000, 1337 }; + + // Indexing an array - Accessing an element + Console.WriteLine("intArray @ 0: " + intArray[0]); + + // Arrays are zero-indexed and mutable. + intArray[1] = 1; + Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1 + + // Lists + // Lists are used more frequently than arrays as they are more flexible + // The format for declaring a list is follows: + // List = new List(); + List intList = new List(); + List stringList = new List(); + + // Another way to declare & initialize a list + List z = new List { 9000, 1000, 1337 }; + + // Indexing a list - Accessing an element + // Lists are zero-indexed and mutable. + Console.WriteLine("z @ 0: " + z[2]); + + // Lists don't default to a value; + // A value must be added before accessing the index + intList.Add(1); + Console.WriteLine("intList @ 0: " + intList[0]); + + + // Others data structures to check out: + // + // Stack/Queue + // Dictionary + // Read-only Collections + // Tuple (.Net 4+) + + + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + Console.WriteLine("\n->Operators"); + + int i1 = 1, i2 = 2; // Shorthand for multiple declarations + + // Arithmetic is straightforward + Console.WriteLine("1+2 = " + (i1 + i2)); // => 3 + Console.WriteLine("2-1 = " + (i2 - i1)); // => 1 + Console.WriteLine("2*1 = " + (i2 * i1)); // => 2 + Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down) + + // Modulo + Console.WriteLine("11%3 = " + (11 % 3)); // => 2 + + // Comparison operators + Console.WriteLine("3 == 2? " + (3 == 2)); // => false + Console.WriteLine("3 != 2? " + (3 != 2)); // => true + Console.WriteLine("3 > 2? " + (3 > 2)); // => true + Console.WriteLine("3 < 2? " + (3 < 2)); // => false + Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true + Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true + + // Bitwise operators! + /* + ~ Unary bitwise complement + << Signed left shift + >> Signed right shift + >>> Unsigned right shift + & Bitwise AND + ^ Bitwise exclusive OR + | Bitwise inclusive OR + */ + + // Incrementations + int i = 0; + Console.WriteLine("\n->Inc/Dec-rementation"); + Console.WriteLine(i++); //i = 1. Post-Incrementation + Console.WriteLine(++i); //i = 2. Pre-Incrementation + Console.WriteLine(i--); //i = 1. Post-Decrementation + Console.WriteLine(--i); //i = 0. Pre-Decrementation + + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + Console.WriteLine("\n->Control Structures"); + + // If statements are c-like + int j = 10; + if (j == 10) + { + Console.WriteLine("I get printed"); + } + else if (j > 10) + { + Console.WriteLine("I don't"); + } + else + { + Console.WriteLine("I also don't"); + } + + // Ternary operators + // A simple if/else can be written as follows + // ? : + string isTrue = (true) ? "True" : "False"; + Console.WriteLine("Ternary demo: " + isTrue); + + + // While loop + int fooWhile = 0; + while (fooWhile < 100) + { + //Console.WriteLine(fooWhile); + //Increment the counter + //Iterated 99 times, fooWhile 0->99 + fooWhile++; + } + Console.WriteLine("fooWhile Value: " + fooWhile); + + // Do While Loop + int fooDoWhile = 0; + do + { + //Console.WriteLine(fooDoWhile); + //Increment the counter + //Iterated 99 times, fooDoWhile 0->99 + fooDoWhile++; + } while (fooDoWhile < 100); + Console.WriteLine("fooDoWhile Value: " + fooDoWhile); + + // For Loop + int fooFor; + //for loop structure => for(; ; ) + for (fooFor = 0; fooFor < 10; fooFor++) + { + //Console.WriteLine(fooFor); + //Iterated 10 times, fooFor 0->9 + } + Console.WriteLine("fooFor Value: " + fooFor); + + // Switch Case + // A switch works with the byte, short, char, and int data types. + // It also works with enumerated types (discussed in Enum Types), + // the String class, and a few special classes that wrap + // primitive types: Character, Byte, Short, and Integer. + int month = 3; + string monthString; + switch (month) + { + case 1: + monthString = "January"; + break; + case 2: + monthString = "February"; + break; + case 3: + monthString = "March"; + break; + default: + monthString = "Some other month"; + break; + } + Console.WriteLine("Switch Case Result: " + monthString); + + + /////////////////////////////////////// + // Converting Data Types And Typcasting + /////////////////////////////////////// + + // Converting data + + // Convert String To Integer + // this will throw an Exception on failure + int.Parse("123");//returns an integer version of "123" + + // try parse will default to type default on failure + // in this case: 0 + int tryInt; + int.TryParse("123", out tryInt); + + // Convert Integer To String + // Convert class has a number of methods to facilitate conversions + Convert.ToString(123); + + /////////////////////////////////////// + // Classes And Functions + /////////////////////////////////////// + + Console.WriteLine("\n->Classes & Functions"); + + // (definition of the Bicycle class follows) + + // Use new to instantiate a class + Bicycle trek = new Bicycle(); + + // Call object methods + trek.speedUp(3); // You should always use setter and getter methods + trek.setCadence(100); + + // ToString is a convention to display the value of this Object. + Console.WriteLine("trek info: " + trek.ToString()); + + // Instantiate another new Bicycle + Bicycle octo = new Bicycle(5, 10); + Console.WriteLine("octo info: " + octo.ToString()); + + // Instantiate a new Penny Farthing + PennyFarthing funbike = new PennyFarthing(1, 10); + Console.WriteLine("funbike info: " + funbike.ToString()); + + Console.Read(); + } // End main method + + + } // End LearnCSharp class + + // You can include other classes in a .cs file + + + // Class Declaration Syntax: + // class { + // //data fields, constructors, functions all inside. + // //functions are called as methods in Java. + // } + + public class Bicycle + { + // Bicycle's Fields/Variables + public int cadence; // Public: Can be accessed from anywhere + private int _speed; // Private: Only accessible from within the class + protected int gear; // Protected: Accessible from the class and subclasses + internal int wheels; // Internal: Accessible from within the assembly + string name; // default: Only accessible from within this class + + // readonly values are set at run time + // they can only be assigned upon declaration or in a constructor + readonly bool hasCardsInSpokes = false; // read-only private + + // Constructors are a way of creating classes + // This is a default constructor + public Bicycle() + { + gear = 1; + cadence = 50; + _speed = 5; + name = "Bontrager"; + } + + // This is a specified constructor (it contains arguments) + public Bicycle(int startCadence, int startSpeed, int startGear, + string name, bool hasCardsInSpokes) + { + this.gear = startGear; + this.cadence = startCadence; + this._speed = startSpeed; + this.name = name; + this.hasCardsInSpokes = hasCardsInSpokes; + } + + // Constructors can be chained + public Bicycle(int startCadence, int startSpeed) : + this(startCadence, startSpeed, 0, "big wheels", true) + { + } + + // Function Syntax: + // () + + // classes can implement getters and setters for their fields + // or they can implement properties + + // Method declaration syntax: + // () + public int getCadence() + { + return cadence; + } + + // void methods require no return statement + public void setCadence(int newValue) + { + cadence = newValue; + } + + // virtual keyword indicates this method can be overridden + public virtual void setGear(int newValue) + { + gear = newValue; + } + + public void speedUp(int increment) + { + _speed += increment; + } + + public void slowDown(int decrement) + { + _speed -= decrement; + } + + // properties get/set values + // when only data needs to be accessed, consider using properties. + // properties may have either get or set, or both + private bool _hasTassles; // private variable + public bool hasTassles // public accessor + { + get { return _hasTassles; } + set { _hasTassles = value; } + } + + private int _frameSize; + public int FrameSize + { + get { return _frameSize; } + // you are able to specify access modifiers for either get or set + // this means only Bicycle class can call set on Framesize + private set { _frameSize = value; } + } + + //Method to display the attribute values of this Object. + public override string ToString() + { + return "gear: " + gear + + " cadence: " + cadence + + " speed: " + _speed + + " name: " + name + + " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") + + "\n------------------------------\n" + ; + } + } // end class Bicycle + + // PennyFarthing is a subclass of Bicycle + class PennyFarthing : Bicycle + { + // (Penny Farthings are those bicycles with the big front wheel. + // They have no gears.) + + // calling parent constructor + public PennyFarthing(int startCadence, int startSpeed) : + base(startCadence, startSpeed, 0, "PennyFarthing", true) + { + } + + public override void setGear(int gear) + { + gear = 0; + } + } } // End Namespace ``` -- cgit v1.2.3 From 10637b0e77cca3daf5bd79e2d1eed2cb250a4192 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 10:14:57 -0700 Subject: Header changes --- common-lisp.html.markdown | 2 +- ko-kr/lua-kr.html.markdown | 5 +++-- zh-cn/racket-cn.html.markdown | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index e174e0df..a917304c 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -1,6 +1,6 @@ --- -language: commonlisp +language: "Common Lisp" filename: commonlisp.lisp contributors: - ["Paul Nathan", "https://github.com/pnathan"] diff --git a/ko-kr/lua-kr.html.markdown b/ko-kr/lua-kr.html.markdown index 3f6223d6..2badf734 100644 --- a/ko-kr/lua-kr.html.markdown +++ b/ko-kr/lua-kr.html.markdown @@ -1,4 +1,4 @@ ---- +--- language: lua category: language contributors: @@ -395,6 +395,7 @@ g() -- 343이 출력됩니다. 그전까지는 아무것도 출력되지 않습 --]] ``` + ## 참고자료 루아를 배우는 일이 흥미진진했던 이유는 Love 2D 게임 엔진을 이용해 @@ -420,4 +421,4 @@ learn.lua로 저장한 후 "lua learn.lua"를 실행해 보세요! 이 글은 tylerneylon.com에 처음으로 써본 글이며, Github의 Gist에서도 확인할 수 있습니다. -루아로 즐거운 시간을 보내세요! \ No newline at end of file +루아로 즐거운 시간을 보내세요! diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown index e17aedda..d43511ea 100644 --- a/zh-cn/racket-cn.html.markdown +++ b/zh-cn/racket-cn.html.markdown @@ -1,6 +1,7 @@ --- language: racket +lang: zh-cn filename: learnracket.rkt contributors: - ["th3rac25", "https://github.com/voila"] -- cgit v1.2.3 From 50e49eced17ee758d47ca06141ee2b0e27ea2ee8 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 10:30:44 -0700 Subject: Line length edits to objective c --- objective-c.html.markdown | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 187ea30a..2b1b3c67 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -11,7 +11,7 @@ filename: LearnObjectiveC.m Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. -```Objective-C +```cpp // Single-line comments start with // /* @@ -180,7 +180,8 @@ int main (int argc, const char * argv[]) @try { // Your statements here - @throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil]; + @throw [NSException exceptionWithName:@"FileNotFoundException" + reason:@"File Not Found on System" userInfo:nil]; } @catch (NSException * e) { NSLog(@"Exception: %@", e); @@ -198,9 +199,10 @@ int main (int argc, const char * argv[]) // An object is not fully functional until both steps have been completed. MyClass *myObject = [[MyClass alloc] init]; - // The Objective-C model of object-oriented programming is based on message passing to object instances. + // The Objective-C model of object-oriented programming is based on message + // passing to object instances. // In Objective-C one does not simply call a method; one sends a message. - [myObject instanceMethodWithParmeter:@"Steve Jobs"]; + [myObject instanceMethodWithParameter:@"Steve Jobs"]; // Clean up the memory you used into your program [pool drain]; @@ -241,7 +243,7 @@ int main (int argc, const char * argv[]) // - for instance method - (NSString *)instanceMethodWithParmeter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; -- + @end // Implement the methods in an implementation (MyClass.m) file: @@ -291,7 +293,7 @@ int main (int argc, const char * argv[]) * A protocol declares methods that can be implemented by any class. * Protocols are not classes themselves. They simply define an interface * that other objects are responsible for implementing. - * / + */ @protocol MyProtocol - (void)myProtocolMethod; @end -- cgit v1.2.3 From de6069d3d60eb2da7ee945c38fbe8d7249c66a6a Mon Sep 17 00:00:00 2001 From: Sonia Keys Date: Tue, 13 Aug 2013 13:52:13 -0400 Subject: Go first draft --- go.html.markdown | 423 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100644 go.html.markdown diff --git a/go.html.markdown b/go.html.markdown new file mode 100644 index 00000000..9888b37d --- /dev/null +++ b/go.html.markdown @@ -0,0 +1,423 @@ +--- +name: Go +category: language +language: Go +filename: learngo.go +contributors: + - ["Sonia Keys", "https://github.com/soniakeys"] +--- + +Go was created out of the need to get work done. It's not the latest trend +in computer science, but it is the newest fastest way to solve real-world +problems. + +It has familiar concepts of imperative languages with static typing. +It's fast to compile and fast to execute, it adds easy-to-understand +concurrency to leverage today's multi-core CPUs, and has features to +help with large-scale programming. + +Go comes with a great standard library and an enthusiastic community. + +```Go +// Single line comment +/* Multi- + line comment */ + +// A package clause starts every source file. +// Main is a special name declaring an executable rather than a library. +package main + +// An import declaration comes next. It declares library packages referenced +// in this file. The list must be exactly correct! Missing or unused packages +// are errors, not warnings. +import ( + "fmt" // A package in the Go standard library + "net/http" // Yes, a web server! + "strconv" // String conversions +) + +// A function definition. Main is special. It is the entry point for the +// executable program. Love it or hate it, Go uses brace brackets. +func main() { + // Println is a function that outputs a line to stdout. It can be + // called here because fmt has been imported and the function name + // "Println" is upper case. Symbols starting with an upper case letter + // are publicly visible. No other special syntax is needed to export + // something from a package. + // To call Println, qualify it with the package name, fmt. + fmt.Println("Hello world!") + + // Call another function within this package. + beyondHello() +} + +// Idiomatic Go uses camel case. Functions have parameters in parentheses. +// If there are no parameters, empty parens are still required. +func beyondHello() { + var x int // Variable declaration. Variables must be declared before use. + x = 3 // Variable assignment. + // "Short" declarations use := syntax to declare and assign, infering the + // type from the right hand side as much as possible and using some + // defaults where the rhs could be interpreted different ways. + // Idiomatic Go uses short declarations in preference to var keyword. + y := 4 + sum, prod := learnMultiple(x, y) // function returns two values + fmt.Println("sum:", sum, "prod:", prod) // simple output + learnTypes() // < y minutes, learn more! +} + +// Functions can have parameters and (multiple!) return values. +// In declarations, the symbol precedes the type, and the type does not have +// to be repeated if it is the same for multiple symbols in a row. +func learnMultiple(x, y int) (sum, prod int) { + return x + y, x * y // return two values +} + +// Some built-in types and literals. +func learnTypes() { + // Short declaration usually gives you what you want. + s := "Learn Go!" // string type + + s2 := `A "raw" string literal +can include line breaks.` // same string type + + // non-ASCII literal. Go source is UTF-8. + g := 'Σ' // rune type, an alias for uint32, holds a UTF-8 code point + + f := 3.14195 // float64, an IEEE-754 64-bit floating point number + c := 3 + 4i // complex128, represented internally with two float64s + + // You can use var syntax with an initializer if you want + // something other than the default that a short declaration gives you. + var u uint = 7 // unsigned, but implementation dependent size as with int + var pi float32 = 22. / 7 + + // Or more idiomatically, use conversion syntax with a short declaration. + n := byte('\n') // byte is an alias for uint8 + + // Arrays have size fixed at compile time. + var a4 [4]int // an array of 4 ints, initialized to all 0 + a3 := [...]int{3, 1, 5} // an array of 3 ints, initialized as shown + + // Slices have dynamic size. Arrays and slices each have advantages + // but use cases for slices are much more common. + s3 := []int{4, 5, 9} // compare to a3. no ellipsis here + s4 := make([]int, 4) // allocates slice of 4 ints, initialized to all 0 + var d2 [][]float64 // declaration only, nothing allocated here + bs := []byte("a slice") // type conversion syntax + + p, q := learnMemory() // A little side bar. + // Did you read it? This short declaration declares p and q to be of + // type pointer to int. P is now pointing into a block of of 20 ints, but + // the only one accessible is the one that p is pointing at. There is + // no p++ to get at the next one. + fmt.Println(*p, *q) // * follows a pointer. This prints two ints. + + // Maps are a dynamically growable associative array type, like the + // hash or dictionary types of some other languages. + m := map[string]int{"three": 3, "four": 4} + m["one"] = 1 + + // Unused variables are an error in Go. + // The underbar lets you "use" a variable but discard its value. + _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs + // Output of course counts as using a variable. + fmt.Println(s, c, a4, s3, d2, m) + + learnFlowControl() // back in the flow +} + +// Go is fully garbage collected. It has pointers but no pointer arithmetic. +// You can make a mistake with a nil pointer, but not by incrementing a pointer. +func learnMemory() (p, q *int) { + // Named return values p and q have type pointer to int. They are + // initialized to nil at this point. Evaluating *p or *q here would cause + // a panic--a run time error. + p = new(int) // built-in function new allocates memory. + // The allocated int is initialized to 0, p is no longer nil. + s := make([]int, 20) // allocate 20 ints as a single block of memory + s[3] = 7 // assign one of them + r := -2 // declare another local variable + return &s[3], &r // Oh my. + // The line above returns two values, yes, and both of the expressions + // are valid. & takes the address of an object. Elements of a slice are + // addressable, and so are local variables. Built-in functions new and + // make explicitly allocate memory, but local objects can be allocated + // as needed. Here memory for r will be still be referenced after the + // function returns so it will be allocated as well. The int allocated + // with new on the other hand will no longer be referenced and can be + // garbage collected as needed by the Go runtime. The memory allocated + // with make will still be referenced at that one element, and so it + // cannot be garbage collected. All 20 ints remain in memory because + // one of them is still referenced. +} + +func expensiveComputation() int { + return 1e6 +} + +func learnFlowControl() { + // If statements require brace brackets, and do not require parens. + if true { + fmt.Println("told ya") + } + // This is how we format the brace brackets. Formatting is standardized + // by the command line command "go fmt." Everybody does it. You will + // suffer endless disparaging remarks until you conform as well. + if false { + // pout + } else { + // gloat + } + // If statements can be chained of course, but it's idiomatic to use + // the handy switch statement instead. + x := 1 + switch x { + case 0: + case 1: + // cases don't "fall through" + case 2: + // unreached + } + // Like if, for doesn't use parens either. The scope of a variable + // declared in the first clause of the for statement is the statement + // and block. This x shadows the x declared above, but goes out of + // scope after the for block. + for x := 0; x < 3; x++ { // ++ is a statement + fmt.Println("iteration", x) + } + // x == 1 here. + + // For is the only loop statement in Go, but it has alternate forms. + for { // infinite loop + break // just kidding + continue // unreached + } + // The initial assignment of the for statement is handy enough that Go + // if statements can have one as well. Just like in the for statement, + // the := here means to declare and assign y first, then test y > x. + // The scope of y is limited to the if statement and block. + if y := expensiveComputation(); y > x { + x = y + } + // Functions are first class objects and function literals are handy. + // Function literals are closures. + xBig := func() bool { + return x > 100 // references x declared above switch statement. + } + fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x) + x /= 1e5 // this makes it == 10 + fmt.Println("xBig:", xBig()) // false now + + // When you need it, you'll love it. Actually Go's goto has been reformed + // a bit to avoid indeterminate states. You can't jump around variable + // declarations and you can't jump into blocks. + goto love +love: + + learnInterfaces() // Good stuff coming up! +} + +// An interface is a list of functionality that a type supports. Notably +// missing from an interface definition is any declaration of which types +// implement the interface. Types simply implement an interface or they don't. +// +// An interface can have any number of methods, but it's actually common +// for an interface to have only single method. It is idiomatic in this +// case for the single method to be named with some action, and for the +// interface name to end in "er." +// +// An interface definition is one kind of a type definition. Interface is +// a built in type. Stringer is defined here as an interface type with one +// method, String. +type Stringer interface { + String() string +} + +// Struct is another built in type. A struct aggregates "fields." +// Pair here has two fields, ints named x and y. +type pair struct { + x, y int +} + +// User defined types can have "methods." These are functions that operate +// in the context of an instance of the user defined type. The instance +// is called the "receiver" and is identified with a declaration just in front +// of the method name. The receiver here is "p." In most ways the receiver +// works just like a function parameter. +// +// This String method has the same name and return value as the String method +// of the Stringer interface. Further, String is the only method of Stringer. +// The pair type thus implements all methods of the Stringer interface and +// we say simply that pair implements Stringer. No other syntax is needed. +func (p pair) String() string { + // Sprintf is another public function in package fmt. + // Dot syntax references fields of p. + return fmt.Sprintf("(%d, %d)", p.x, p.y) +} + +func learnInterfaces() { + // Brace syntax is a "struct literal." It evaluates to an initialized + // struct. The := syntax declares and initializes p to this struct. + p := pair{3, 4} + fmt.Println(p.String()) // call String method of p, of type pair. + var i Stringer // declare i of type Stringer. + i = p // valid because pair implements Stringer + // Call String method of i, of type Stringer. Output same as above. + fmt.Println(i.String()) + // It gets more interesting now. We defined Stringer in this file, + // but the same interface happens to be defined in package fmt. + // Pair thus implements fmt.Stringer as well, and does so with no + // declaration of the fact. The definition of pair doesn't mention + // any interfaces at all, and of course the authors of fmt.Stringer + // had no idea that we were going to define pair. + // + // Functions in the fmt package know how to print some standard built in + // types, and beyond that, they see if a type implements fmt.Stringer. + // If so, they simply call the String method to ask an object for a + // printable representation of itself. + fmt.Println(p) // output same as above. Println calls String method. + fmt.Println(i) // output same as above + + learnErrorHandling() +} + +func learnErrorHandling() { + // Sometimes you just need to know if something worked or not. Go has + // a ", ok" idiom for that. Something, a map expression here, but commonly + // a function, can return a boolean value of ok or not ok as a second + // return value. + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // , ok is optional but see how useful it is. + fmt.Println("no one there") + } else { + fmt.Print(x) + } + // An error value communicates not just "ok" but more about the problem. + if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value + // prints "strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } + // error is a built in type. It is an interface with a single method, + // defined internally as, + // + // type error interface { + // Error() string + // } + // + // The string returned by the Error method is conventionally a printable + // error message. You can define your own error types by simply adding + // an Error method. Your type then automatically implements the error + // interface. We've seen two interfaces now, fmt.Stringer and error. + + // We'll revisit interfaces a little later. Meanwhile, + learnConcurrency() +} + +// Go has concurrency support in the language definition. The element of +// concurrent execution is called a "goroutine" and is similar to a thread +// but "lighter." Goroutines are multiplexed to operating system threads +// and a running Go program can have far more goroutines than available OS +// threads. If a machine has multiple CPU cores, goroutines can run in +// parallel. +// +// Go "Channels" allow communication between goroutines in a way that is +// both powerful and easy to understand. Channel is a type in Go and objects +// of type channel are first class objects--they can be assigned to variables, +// passed around to functions, and so on. A channel works conceptually much +// like a Unix pipe. You put data in at one end and it comes out the other. +// Channel "send" and "receive" operations are goroutine-safe. No locks +// or additional synchronization is needed. + +// Inc increments a number, and sends the result on a channel. The channel +// operation makes this function useful to run concurrently with other +// goroutines. There is no special declaration though that says this function +// is concurrent. It is an ordinary function that happens to have a +// parameter of channel type. +func inc(i int, c chan int) { + c <- i + 1 // <- is the "send" operator when a channel appears on the left. +} + +// We'll use inc to increment some numbers concurrently. +func learnConcurrency() { + // Same make function used earlier to make a slice. Make allocates and + // initializes slices, maps, and channels. + c := make(chan int) + // Start three concurrent goroutines. Numbers will be incremented + // concurrently, perhaps in parallel if the machine is capable and + // properly configured. All three send to the same channel. + go inc(0, c) // go is a statement that starts a new goroutine. + go inc(10, c) + go inc(-805, c) + // Read three results from the channel and print them out. + // There is no telling in what order the results will arrive! + fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator. + + cs := make(chan string) // another channel, this one handles strings. + cc := make(chan chan string) // a channel of channels. + go func() { c <- 84 }() // start a new goroutine just to send a value + go func() { cs <- "wordy" }() // again, for cs this time + // Select has syntax like a switch statement but is doing something + // pretty different. Each case involves a channel operation. In rough + // terms, a case is selected at random out of the cases that are ready to + // communicate. If none are ready, select waits for one to become ready. + select { + case i := <-c: // the value received can be assigned to a variable + fmt.Println("it's a", i) + case <-cs: // or the value received can be discarded + fmt.Println("it's a string") + case <-cc: // empty channel, not ready for communication. + fmt.Println("didn't happen.") + } + // At this point a value was taken from either c or cs. One of the two + // goroutines started above has completed, the other will remain blocked. + + learnWebProgramming() // Go does it. You want to do it too. +} + +// A simple web server can be created with a single function from the standard +// library. ListenAndServe, in package net/http, listens at the specified +// TCP address and uses an object that knows how to serve data. "Knows how" +// means "satisfies an interface." The second parameter is of type interface, +// specifically http.Handler. http.Handler has a single method, ServeHTTP. +func learnWebProgramming() { + err := http.ListenAndServe(":8080", pair{}) + // Error returns are ubiquitous in Go. Always check error returns and + // do something with them. Often it's enough to print it out as an + // indication of what failed. Of course there are better things to do + // in production code: log it, try something else, shut everything down, + // and so on. + fmt.Println(err) +} + +// You can make any type into an http.Hander by implementing ServeHTTP. +// Lets use the pair type we defined earlier, just because we have it +// sitting around. ServeHTTP has two parameters. The request parameter +// is a struct that we'll ignore here. http.ResponseWriter is yet another +// interface! Here it is an object supplied to us with the guarantee that +// it implements its interface, which includes a method Write. +// We call this Write method to serve data. +func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("You learned Go in Y minutes!")) +} + +// And that's it for a proof-of-concept web server! If you run this program +// it will print out all the lines from the earlier parts of the lesson, then +// start this web server. To hit the web server, just point a browser at +// localhost:8080 and you'll see the message. (Then you can probably press +// ctrl-C to kill it.) +``` + +## Further Reading + +The root of all things Go is the [official Go web site](http://golang.org/). +There you can follow the tutorial, play interactively, and read lots. + +The language definition itself is highly recommended. It's easy to read +and amazingly short (as language definitions go these days.) + +On the reading list for students of Go is the source code to the standard +library. Comprehensively documented, it demonstrates the best of readable +and understandable Go, Go style, and Go idioms. Click on a function name +in the documentation and the source code comes up! + -- cgit v1.2.3 From 49cca911886480d10b071ced3a91c325ec3516f9 Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Tue, 13 Aug 2013 16:28:48 -0300 Subject: Add brazilian portuguese translation for elisp tutorial. --- pt-br/elisp-pt.html.markdown | 359 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 pt-br/elisp-pt.html.markdown diff --git a/pt-br/elisp-pt.html.markdown b/pt-br/elisp-pt.html.markdown new file mode 100644 index 00000000..9031cad9 --- /dev/null +++ b/pt-br/elisp-pt.html.markdown @@ -0,0 +1,359 @@ +--- +language: elisp +contributors: + - ["Bastien Guerry", "http://bzg.fr"] +translators: + - ["Lucas Tadeu Teixeira", "http://ltt.me"] +lang: pt-br +filename: learn-emacs-lisp-pt.el +--- + +```scheme +;; Introdução ao Emacs Lisp em 15 minutos (v0.2d) +;; +;; Autor: Bastien / @bzg2 / http://bzg.fr +;; +;; Antes de começar, leia este texto escrito Peter Norvig: +;; http://norvig.com/21-days.html +;; +;; Agora instale GNU Emacs 24.3: +;; +;; Debian: apt-get install emacs (ou veja as instruções da sua distribuição) +;; OSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg +;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip +;; +;; Informações mais gerais podem ser encontradas em: +;; http://www.gnu.org/software/emacs/#Obtaining + +;; Aviso importante: +;; +;; Realizar este tutorial não danificará seu computador, a menos +;; que você fique tão irritado a ponto de jogá-lo no chão. Neste caso, +;; me abstenho de qualquer responsabilidade. Divirta-se! + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Abra o Emacs. +;; +;; Aperte a tecla `q' para ocultar a mensagem de boas vindas. +;; +;; Agora olhe para a linha cinza na parte inferior da janela: +;; +;; "*scratch*" é o nome do espaço de edição em que você se encontra. +;; Este espaço de edição é chamado "buffer". +;; +;; O buffer de rascunho (i.e., "scratch") é o buffer padrão quando +;; o Emacs é aberto. Você nunca está editando arquivos: você está +;; editando buffers que você pode salvar em um arquivo. +;; +;; "Lisp interaction" refere-se a um conjunto de comandos disponíveis aqui. +;; +;; O Emacs possui um conjunto de comandos embutidos (disponíveis em +;; qualquer buffer) e vários subconjuntos de comandos disponíveis +;; quando você ativa um modo específico. Aqui nós utilizamos +;; `lisp-interaction-mode', que possui comandos para interpretar e navegar +;; em código Elisp. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Pontos e vírgulas iniciam comentários em qualquer parte de uma linha. +;; +;; Programas codificados em Elisp são compostos por expressões simbólicas +;; (conhecidas também por "sexps"): +(+ 2 2) + +;; Esta expressão simbólica significa "Some 2 e 2". + +;; "Sexps" são envoltas em parêntese, possivelmente aninhados: +(+ 2 (+ 1 1)) + +;; Uma expressão simbólica contém átomos ou outras expressões +;; simbólicas. Nos exemplos acima, 1 e 2 são átomos; +;; (+ 2 (+ 1 1)) e (+ 1 1) são expressões simbólicas. + +;; No modo `lisp-interaction-mode' você pode interpretar "sexps". +;; Posicione o cursor logo após o parêntese de fechamento e, +;; então, segure apertado Ctrl e aperte a tecla j ("C-j", em resumo). + +(+ 3 (+ 1 2)) +;; ^ posicione o cursor aqui +;; `C-j' => 6 + +;; `C-j' insere o resultado da interpretação da expressão no buffer. + +;; `C-xC-e' exibe o mesmo resultado na linha inferior do Emacs, +;; chamada de "mini-buffer". Nós geralmente utilizaremos `C-xC-e', +;; já que não queremos poluir o buffer com texto desnecessário. + +;; `setq' armazena um valor em uma variável: +(setq my-name "Bastien") +;; `C-xC-e' => "Bastien" (texto exibido no mini-buffer) + +;; `insert' insere "Hello!" na posição em que se encontra seu cursor: +(insert "Hello!") +;; `C-xC-e' => "Hello!" + +;; Nós executamos `insert' com apenas um argumento ("Hello!"), mas +;; mais argumentos podem ser passados -- aqui utilizamos dois: + +(insert "Hello" " world!") +;; `C-xC-e' => "Hello world!" + +;; Você pode utilizar variávies no lugar de strings: +(insert "Hello, I am " my-name) +;; `C-xC-e' => "Hello, I am Bastien" + +;; Você pode combinar "sexps" em funções: +(defun hello () (insert "Hello, I am " my-name)) +;; `C-xC-e' => hello + +;; Você pode interpretar chamadas de funções: +(hello) +;; `C-xC-e' => Hello, I am Bastien + +;; Os parêntesis vazios na definição da função significam que ela +;; não aceita argumentos. Mas sempre utilizar `my-name' é um tédio! +;; Vamos dizer à função para aceitar um argumento (o argumento é +;; chamado "name"): + +(defun hello (name) (insert "Hello " name)) +;; `C-xC-e' => hello + +;; Agora vamos executar a função com a string "you" como o valor +;; para seu único parâmetro: +(hello "you") +;; `C-xC-e' => "Hello you" + +;; Aí sim! + +;; Respire um pouco. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Agora mude para um novo buffer chamado "*test*": + +(switch-to-buffer-other-window "*test*") +;; `C-xC-e' +;; => [a tela exibirá duas janelas e o cursor estará no buffer *test*] + +;; Posicione o mouse sobre a janela superior e clique com o botão +;; esquerdo para voltar. Ou você pode utilizar `C-xo' (i.e. segure +;; ctrl-x e aperte o) para voltar para a outra janela, de forma interativa. + +;; Você pode combinar várias "sexps" com `progn': +(progn + (switch-to-buffer-other-window "*test*") + (hello "you")) +;; `C-xC-e' +;; => [A tela exibirá duas janelas e o cursor estará no buffer *test*] + +;; Agora, se você não se importar, pararei de pedir que você aperte +;; `C-xC-e': faça isso para cada "sexp" que escrevermos. + +;; Sempre volte para o buffer *scratch* com o mouse ou `C-xo'. + +;; Frequentemente, é útil apagar o conteúdo do buffer: +(progn + (switch-to-buffer-other-window "*test*") + (erase-buffer) + (hello "there")) + +;; Ou voltar para a outra janela: +(progn + (switch-to-buffer-other-window "*test*") + (erase-buffer) + (hello "you") + (other-window 1)) + +;; Você pode armazenar um valor em uma variável local utilizando `let': +(let ((local-name "you")) + (switch-to-buffer-other-window "*test*") + (erase-buffer) + (hello local-name) + (other-window 1)) + +;; Neste caso, não é necessário utilizar `progn' já que `let' combina +;; várias "sexps". + +;; Vamos formatar uma string: +(format "Hello %s!\n" "visitor") + +;; %s é um espaço reservado para uma string, substituído por "visitor". +;; \n é um caractere de nova linha. + +;; Vamos refinar nossa função utilizando `format': +(defun hello (name) + (insert (format "Hello %s!\n" name))) + +(hello "you") + +;; Vamos criar outra função que utilize `let': +(defun greeting (name) + (let ((your-name "Bastien")) + (insert (format "Hello %s!\n\nI am %s." + name ; the argument of the function + your-name ; the let-bound variable "Bastien" + )))) + +;; E executá-la: +(greeting "you") + +;; Algumas funções são interativas: +(read-from-minibuffer "Enter your name: ") + +;; Ao ser interpretada, esta função retorna o que você digitou no prompt. + +;; Vamos fazer nossa função `greeting' pedir pelo seu nome: +(defun greeting (from-name) + (let ((your-name (read-from-minibuffer "Enter your name: "))) + (insert (format "Hello!\n\nI am %s and you are %s." + from-name ; the argument of the function + your-name ; the let-bound var, entered at prompt + )))) + +(greeting "Bastien") + +;; Vamos finalizá-la fazendo-a exibir os resultados em outra janela: +(defun greeting (from-name) + (let ((your-name (read-from-minibuffer "Enter your name: "))) + (switch-to-buffer-other-window "*test*") + (erase-buffer) + (insert (format "Hello %s!\n\nI am %s." your-name from-name)) + (other-window 1))) + +;; Agora teste-a: +(greeting "Bastien") + +;; Respire um pouco. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Vamos armazenar uma lista de nomes: +(setq list-of-names '("Sarah" "Chloe" "Mathilde")) + +;; Pegue o primeiro elemento desta lista utilizando `car': +(car list-of-names) + +;; Pegue uma lista de todos os elementos, exceto o primeiro, utilizando +;; `cdr': +(cdr list-of-names) + +;; Adicione um elemento ao início da lista com `push': +(push "Stephanie" list-of-names) + +;; NOTA: `car' e `cdr' não modificam a lista, `push' sim. +;; Esta é uma diferença importante: algumas funções não têm qualquer +;; efeito colateral (como `car'), enquanto outras sim (como `push'). + +;; Vamos executar `hello' para cada elemento em `list-of-names': +(mapcar 'hello list-of-names) + +;; Refine `greeting' para saudar todos os nomes em `list-of-names': +(defun greeting () + (switch-to-buffer-other-window "*test*") + (erase-buffer) + (mapcar 'hello list-of-names) + (other-window 1)) + +(greeting) + +;; Você se lembra da função `hello' que nós definimos lá em cima? Ela +;; recebe um argumento, um nome. `mapcar' executa `hello', sucessivamente, +;; utilizando cada elemento de `list-of-names' como argumento para `hello'. + +;; Agora vamos arrumar, um pouco, o que nós temos escrito no buffer: + +(defun replace-hello-by-bonjour () + (switch-to-buffer-other-window "*test*") + (goto-char (point-min)) + (while (search-forward "Hello") + (replace-match "Bonjour")) + (other-window 1)) + +;; (goto-char (point-min)) vai para o início do buffer. +;; (search-forward "Hello") busca pela string "Hello". +;; (while x y) interpreta a(s) sexp(s) y enquanto x retornar algo. +;; Se x retornar `nil' (nada), nós saímos do laço. + +(replace-hello-by-bonjour) + +;; Você deveria ver todas as ocorrências de "Hello" no buffer *test* +;; substituídas por "Bonjour". + +;; Você deveria, também, receber um erro: "Search failed: Hello". +;; +;; Para evitar este erro, você precisa dizer ao `search-forward' se ele +;; deveria parar de buscar em algum ponto no buffer, e se ele deveria +;; falhar de forma silenciosa quando nada fosse encontrado: + +;; (search-forward "Hello" nil t) dá conta do recado: + +;; O argumento `nil' diz: a busca não está limitada a uma posição. +;; O argumento `t' diz: falhe silenciosamente quando nada for encontrado. + +;; Nós utilizamos esta "sexp" na função abaixo, que não gera um erro: + +(defun hello-to-bonjour () + (switch-to-buffer-other-window "*test*") + (erase-buffer) + ;; Say hello to names in `list-of-names' + (mapcar 'hello list-of-names) + (goto-char (point-min)) + ;; Replace "Hello" by "Bonjour" + (while (search-forward "Hello" nil t) + (replace-match "Bonjour")) + (other-window 1)) + +(hello-to-bonjour) + +;; Vamos colorir os nomes: + +(defun boldify-names () + (switch-to-buffer-other-window "*test*") + (goto-char (point-min)) + (while (re-search-forward "Bonjour \\(.+\\)!" nil t) + (add-text-properties (match-beginning 1) + (match-end 1) + (list 'face 'bold))) + (other-window 1)) + +;; Esta função introduz `re-search-forward': ao invés de buscar +;; pela string "Bonjour", você busca por um padrão utilizando uma +;; "expressão regular" (abreviada pelo prefixo "re-"). + +;; A expressão regular é "Bonjour \\(.+\\)!" e lê-se: +;; a string "Bonjour ", e +;; um grupo de | que é o \\( ... \\) +;; quaisquer caracteres | que é o . +;; possivelmente repetidos | que é o + +;; e a string "!". + +;; Preparado? Teste! + +(boldify-names) + +;; `add-text-properties' adiciona... propriedades de texto, como uma fonte. + +;; OK, terminamos por aqui. Feliz Hacking! + +;; Se você quiser saber mais sobre uma variável ou função: +;; +;; C-h v uma-variável RET +;; C-h f uma-função RET +;; +;; Para ler o manual de Emacs Lisp que vem com o Emacs: +;; +;; C-h i m elisp RET +;; +;; Para ler uma introdução online ao Emacs Lisp: +;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html + +;; Agradecimentos a estas pessoas por seu feedback e sugestões: +;; - Wes Hardaker +;; - notbob +;; - Kevin Montuori +;; - Arne Babenhauserheide +;; - Alan Schmitt +;; - LinXitoW +;; - Aaron Meurer +``` -- cgit v1.2.3 From cf3149c4bde261aa35cde0cba34432a2f0bf649a Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Tue, 13 Aug 2013 16:29:33 -0300 Subject: Fix typo on 'lang' tag. --- pt-br/python-pt.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pt-br/python-pt.html.markdown b/pt-br/python-pt.html.markdown index e08bb5a8..5afd46d0 100644 --- a/pt-br/python-pt.html.markdown +++ b/pt-br/python-pt.html.markdown @@ -4,7 +4,7 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] translators: - ["Vilson Vieira", "http://automata.cc"] -lang: pt-bf +lang: pt-br filename: learnpython-pt.py --- -- cgit v1.2.3 From a73d5c83c162569d0def8e481d85ece8d517b06a Mon Sep 17 00:00:00 2001 From: Sonia Keys Date: Tue, 13 Aug 2013 17:12:54 -0400 Subject: slashed comments --- go.html.markdown | 198 +++++++++++-------------------------------------------- 1 file changed, 38 insertions(+), 160 deletions(-) diff --git a/go.html.markdown b/go.html.markdown index 9888b37d..e7b35926 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -27,9 +27,7 @@ Go comes with a great standard library and an enthusiastic community. // Main is a special name declaring an executable rather than a library. package main -// An import declaration comes next. It declares library packages referenced -// in this file. The list must be exactly correct! Missing or unused packages -// are errors, not warnings. +// Import declaration declares library packages referenced in this file. import ( "fmt" // A package in the Go standard library "net/http" // Yes, a web server! @@ -39,27 +37,20 @@ import ( // A function definition. Main is special. It is the entry point for the // executable program. Love it or hate it, Go uses brace brackets. func main() { - // Println is a function that outputs a line to stdout. It can be - // called here because fmt has been imported and the function name - // "Println" is upper case. Symbols starting with an upper case letter - // are publicly visible. No other special syntax is needed to export - // something from a package. - // To call Println, qualify it with the package name, fmt. + // Println outputs a line to stdout. + // Qualify it with the package name, fmt. fmt.Println("Hello world!") // Call another function within this package. beyondHello() } -// Idiomatic Go uses camel case. Functions have parameters in parentheses. +// Functions have parameters in parentheses. // If there are no parameters, empty parens are still required. func beyondHello() { var x int // Variable declaration. Variables must be declared before use. x = 3 // Variable assignment. - // "Short" declarations use := syntax to declare and assign, infering the - // type from the right hand side as much as possible and using some - // defaults where the rhs could be interpreted different ways. - // Idiomatic Go uses short declarations in preference to var keyword. + // "Short" declarations use := to infer the type, declare, and assign. y := 4 sum, prod := learnMultiple(x, y) // function returns two values fmt.Println("sum:", sum, "prod:", prod) // simple output @@ -67,8 +58,6 @@ func beyondHello() { } // Functions can have parameters and (multiple!) return values. -// In declarations, the symbol precedes the type, and the type does not have -// to be repeated if it is the same for multiple symbols in a row. func learnMultiple(x, y int) (sum, prod int) { return x + y, x * y // return two values } @@ -87,12 +76,11 @@ can include line breaks.` // same string type f := 3.14195 // float64, an IEEE-754 64-bit floating point number c := 3 + 4i // complex128, represented internally with two float64s - // You can use var syntax with an initializer if you want - // something other than the default that a short declaration gives you. + // Var syntax with an initializers. var u uint = 7 // unsigned, but implementation dependent size as with int var pi float32 = 22. / 7 - // Or more idiomatically, use conversion syntax with a short declaration. + // Conversion syntax with a short declaration. n := byte('\n') // byte is an alias for uint8 // Arrays have size fixed at compile time. @@ -106,12 +94,8 @@ can include line breaks.` // same string type var d2 [][]float64 // declaration only, nothing allocated here bs := []byte("a slice") // type conversion syntax - p, q := learnMemory() // A little side bar. - // Did you read it? This short declaration declares p and q to be of - // type pointer to int. P is now pointing into a block of of 20 ints, but - // the only one accessible is the one that p is pointing at. There is - // no p++ to get at the next one. - fmt.Println(*p, *q) // * follows a pointer. This prints two ints. + p, q := learnMemory() // declares p, q to be type pointer to int. + fmt.Println(*p, *q) // * follows a pointer. This prints two ints. // Maps are a dynamically growable associative array type, like the // hash or dictionary types of some other languages. @@ -130,26 +114,13 @@ can include line breaks.` // same string type // Go is fully garbage collected. It has pointers but no pointer arithmetic. // You can make a mistake with a nil pointer, but not by incrementing a pointer. func learnMemory() (p, q *int) { - // Named return values p and q have type pointer to int. They are - // initialized to nil at this point. Evaluating *p or *q here would cause - // a panic--a run time error. + // Named return values p and q have type pointer to int. p = new(int) // built-in function new allocates memory. // The allocated int is initialized to 0, p is no longer nil. s := make([]int, 20) // allocate 20 ints as a single block of memory s[3] = 7 // assign one of them r := -2 // declare another local variable - return &s[3], &r // Oh my. - // The line above returns two values, yes, and both of the expressions - // are valid. & takes the address of an object. Elements of a slice are - // addressable, and so are local variables. Built-in functions new and - // make explicitly allocate memory, but local objects can be allocated - // as needed. Here memory for r will be still be referenced after the - // function returns so it will be allocated as well. The int allocated - // with new on the other hand will no longer be referenced and can be - // garbage collected as needed by the Go runtime. The memory allocated - // with make will still be referenced at that one element, and so it - // cannot be garbage collected. All 20 ints remain in memory because - // one of them is still referenced. + return &s[3], &r // & takes the address of an object. } func expensiveComputation() int { @@ -161,16 +132,13 @@ func learnFlowControl() { if true { fmt.Println("told ya") } - // This is how we format the brace brackets. Formatting is standardized - // by the command line command "go fmt." Everybody does it. You will - // suffer endless disparaging remarks until you conform as well. + // Formatting is standardized by the command line command "go fmt." if false { // pout } else { // gloat } - // If statements can be chained of course, but it's idiomatic to use - // the handy switch statement instead. + // Use switch in preference to chained if statements. x := 1 switch x { case 0: @@ -179,10 +147,7 @@ func learnFlowControl() { case 2: // unreached } - // Like if, for doesn't use parens either. The scope of a variable - // declared in the first clause of the for statement is the statement - // and block. This x shadows the x declared above, but goes out of - // scope after the for block. + // Like if, for doesn't use parens either. for x := 0; x < 3; x++ { // ++ is a statement fmt.Println("iteration", x) } @@ -193,14 +158,11 @@ func learnFlowControl() { break // just kidding continue // unreached } - // The initial assignment of the for statement is handy enough that Go - // if statements can have one as well. Just like in the for statement, - // the := here means to declare and assign y first, then test y > x. - // The scope of y is limited to the if statement and block. + // As with for, := in an if statement means to declare and assign y first, + // then test y > x. if y := expensiveComputation(); y > x { x = y } - // Functions are first class objects and function literals are handy. // Function literals are closures. xBig := func() bool { return x > 100 // references x declared above switch statement. @@ -209,48 +171,25 @@ func learnFlowControl() { x /= 1e5 // this makes it == 10 fmt.Println("xBig:", xBig()) // false now - // When you need it, you'll love it. Actually Go's goto has been reformed - // a bit to avoid indeterminate states. You can't jump around variable - // declarations and you can't jump into blocks. + // When you need it, you'll love it. goto love love: learnInterfaces() // Good stuff coming up! } -// An interface is a list of functionality that a type supports. Notably -// missing from an interface definition is any declaration of which types -// implement the interface. Types simply implement an interface or they don't. -// -// An interface can have any number of methods, but it's actually common -// for an interface to have only single method. It is idiomatic in this -// case for the single method to be named with some action, and for the -// interface name to end in "er." -// -// An interface definition is one kind of a type definition. Interface is -// a built in type. Stringer is defined here as an interface type with one -// method, String. +// Define Stringer as an interface type with one method, String. type Stringer interface { String() string } -// Struct is another built in type. A struct aggregates "fields." -// Pair here has two fields, ints named x and y. +// Define pair as a struct with two fields, ints named x and y. type pair struct { x, y int } -// User defined types can have "methods." These are functions that operate -// in the context of an instance of the user defined type. The instance -// is called the "receiver" and is identified with a declaration just in front -// of the method name. The receiver here is "p." In most ways the receiver -// works just like a function parameter. -// -// This String method has the same name and return value as the String method -// of the Stringer interface. Further, String is the only method of Stringer. -// The pair type thus implements all methods of the Stringer interface and -// we say simply that pair implements Stringer. No other syntax is needed. -func (p pair) String() string { +// Define a method on type pair. Pair now implements Stringer. +func (p pair) String() string { // p is called the "receiver" // Sprintf is another public function in package fmt. // Dot syntax references fields of p. return fmt.Sprintf("(%d, %d)", p.x, p.y) @@ -261,21 +200,13 @@ func learnInterfaces() { // struct. The := syntax declares and initializes p to this struct. p := pair{3, 4} fmt.Println(p.String()) // call String method of p, of type pair. - var i Stringer // declare i of type Stringer. + var i Stringer // declare i of interface type Stringer. i = p // valid because pair implements Stringer // Call String method of i, of type Stringer. Output same as above. fmt.Println(i.String()) - // It gets more interesting now. We defined Stringer in this file, - // but the same interface happens to be defined in package fmt. - // Pair thus implements fmt.Stringer as well, and does so with no - // declaration of the fact. The definition of pair doesn't mention - // any interfaces at all, and of course the authors of fmt.Stringer - // had no idea that we were going to define pair. - // - // Functions in the fmt package know how to print some standard built in - // types, and beyond that, they see if a type implements fmt.Stringer. - // If so, they simply call the String method to ask an object for a - // printable representation of itself. + + // Functions in the fmt package call the String method to ask an object + // for a printable representation of itself. fmt.Println(p) // output same as above. Println calls String method. fmt.Println(i) // output same as above @@ -283,57 +214,23 @@ func learnInterfaces() { } func learnErrorHandling() { - // Sometimes you just need to know if something worked or not. Go has - // a ", ok" idiom for that. Something, a map expression here, but commonly - // a function, can return a boolean value of ok or not ok as a second - // return value. + // ", ok" idiom used to tell if something worked or not. m := map[int]string{3: "three", 4: "four"} - if x, ok := m[1]; !ok { // , ok is optional but see how useful it is. + if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map. fmt.Println("no one there") } else { - fmt.Print(x) + fmt.Print(x) // x would be the value, if it were in the map. } // An error value communicates not just "ok" but more about the problem. if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value // prints "strconv.ParseInt: parsing "non-int": invalid syntax" fmt.Println(err) } - // error is a built in type. It is an interface with a single method, - // defined internally as, - // - // type error interface { - // Error() string - // } - // - // The string returned by the Error method is conventionally a printable - // error message. You can define your own error types by simply adding - // an Error method. Your type then automatically implements the error - // interface. We've seen two interfaces now, fmt.Stringer and error. - // We'll revisit interfaces a little later. Meanwhile, learnConcurrency() } -// Go has concurrency support in the language definition. The element of -// concurrent execution is called a "goroutine" and is similar to a thread -// but "lighter." Goroutines are multiplexed to operating system threads -// and a running Go program can have far more goroutines than available OS -// threads. If a machine has multiple CPU cores, goroutines can run in -// parallel. -// -// Go "Channels" allow communication between goroutines in a way that is -// both powerful and easy to understand. Channel is a type in Go and objects -// of type channel are first class objects--they can be assigned to variables, -// passed around to functions, and so on. A channel works conceptually much -// like a Unix pipe. You put data in at one end and it comes out the other. -// Channel "send" and "receive" operations are goroutine-safe. No locks -// or additional synchronization is needed. - -// Inc increments a number, and sends the result on a channel. The channel -// operation makes this function useful to run concurrently with other -// goroutines. There is no special declaration though that says this function -// is concurrent. It is an ordinary function that happens to have a -// parameter of channel type. +// c is a channel, a concurrency-safe communication object. func inc(i int, c chan int) { c <- i + 1 // <- is the "send" operator when a channel appears on the left. } @@ -357,10 +254,9 @@ func learnConcurrency() { cc := make(chan chan string) // a channel of channels. go func() { c <- 84 }() // start a new goroutine just to send a value go func() { cs <- "wordy" }() // again, for cs this time - // Select has syntax like a switch statement but is doing something - // pretty different. Each case involves a channel operation. In rough - // terms, a case is selected at random out of the cases that are ready to - // communicate. If none are ready, select waits for one to become ready. + // Select has syntax like a switch statement but each case involves + // a channel operation. It selects a case at random out of the cases + // that are ready to communicate. select { case i := <-c: // the value received can be assigned to a variable fmt.Println("it's a", i) @@ -375,37 +271,19 @@ func learnConcurrency() { learnWebProgramming() // Go does it. You want to do it too. } -// A simple web server can be created with a single function from the standard -// library. ListenAndServe, in package net/http, listens at the specified -// TCP address and uses an object that knows how to serve data. "Knows how" -// means "satisfies an interface." The second parameter is of type interface, -// specifically http.Handler. http.Handler has a single method, ServeHTTP. +// A single function from package http starts a web server. func learnWebProgramming() { + // ListenAndServe first parameter is TCP address to listen at. + // Second parameter is an interface, specifically http.Handler. err := http.ListenAndServe(":8080", pair{}) - // Error returns are ubiquitous in Go. Always check error returns and - // do something with them. Often it's enough to print it out as an - // indication of what failed. Of course there are better things to do - // in production code: log it, try something else, shut everything down, - // and so on. - fmt.Println(err) + fmt.Println(err) // don't ignore errors } -// You can make any type into an http.Hander by implementing ServeHTTP. -// Lets use the pair type we defined earlier, just because we have it -// sitting around. ServeHTTP has two parameters. The request parameter -// is a struct that we'll ignore here. http.ResponseWriter is yet another -// interface! Here it is an object supplied to us with the guarantee that -// it implements its interface, which includes a method Write. -// We call this Write method to serve data. +// Make pair an http.Handler by implementing its only method, ServeHTTP. func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // Serve data with a method of http.ResponseWriter w.Write([]byte("You learned Go in Y minutes!")) } - -// And that's it for a proof-of-concept web server! If you run this program -// it will print out all the lines from the earlier parts of the lesson, then -// start this web server. To hit the web server, just point a browser at -// localhost:8080 and you'll see the message. (Then you can probably press -// ctrl-C to kill it.) ``` ## Further Reading -- cgit v1.2.3 From ea853dfa674a1be2c6d5e54b1307bf80693cf01d Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:42:03 -0500 Subject: Fixes array typos --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 68c5b524..46bfbb7c 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -117,11 +117,11 @@ status == :approved #=> false # Arrays # This is an array -[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items -array = [1, "hello", false] #=> => [1, "hello", false] +[1, "hello", false] #=> [1, "hello", false] # Arrays can be indexed # From the front -- cgit v1.2.3 From 7af884a9407c0b79aaede1edd3539e13a55552c8 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:46:33 -0500 Subject: Fixes spacing issues --- ruby.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 46bfbb7c..963d1fc1 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -173,9 +173,9 @@ new_hash.keys #=> [:defcon, :action] if true "if statement" elsif false - "else if, optional" + "else if, optional" else - "else, also optional" + "else, also optional" end for counter in 1..5 @@ -190,7 +190,8 @@ end # HOWEVER, No-one uses for loops. # Instead you should use the "each" method and pass it a block. # A block is a bunch of code that you can pass to a method like "each". -# It is analogous to lambdas, anonymous functions or closures in other programming languages. +# It is analogous to lambdas, anonymous functions or closures in other +# programming languages. # # The "each" method of a range runs the block once for each element of the range. # The block is passed a counter as a parameter. -- cgit v1.2.3 From 9fd70ffbb12e92a4bd129cfbe28d91e07138c398 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:47:52 -0500 Subject: Giving myself some credit... I guess --- ruby.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/ruby.html.markdown b/ruby.html.markdown index 963d1fc1..19f2ec86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] --- ```ruby -- cgit v1.2.3 From c5a3e191405e6722239c951635a2205c93d55bde Mon Sep 17 00:00:00 2001 From: hbc Date: Wed, 14 Aug 2013 08:19:05 +0800 Subject: Improved some translations, made the code more PEP8 --- zh-cn/python-cn.html.markdown | 277 +++++++++++++++++++++--------------------- 1 file changed, 139 insertions(+), 138 deletions(-) diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown index 764eed54..51efaac3 100755 --- a/zh-cn/python-cn.html.markdown +++ b/zh-cn/python-cn.html.markdown @@ -17,6 +17,7 @@ Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语 如果是Python 3,请在网络上寻找其他教程 ```python + # 单行注释 """ 多行字符串可以用 三个引号包裹,不过这也可以被当做 @@ -28,84 +29,84 @@ Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语 #################################################### # 数字类型 -3 #=> 3 +3 # => 3 # 简单的算数 -1 + 1 #=> 2 -8 - 1 #=> 7 -10 * 2 #=> 20 -35 / 5 #=> 7 +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7 # 整数的除法会自动取整 -5 / 2 #=> 2 +5 / 2 # => 2 # 要做精确的除法,我们需要引入浮点数 2.0 # 浮点数 -11.0 / 4.0 #=> 2.75 好多了 +11.0 / 4.0 # => 2.75 精确多了 # 括号具有最高优先级 -(1 + 3) * 2 #=> 8 +(1 + 3) * 2 # => 8 -# 布尔值也是原始数据类型 +# 布尔值也是基本的数据类型 True False -# 用not来取非 -not True #=> False -not False #=> True +# 用 not 来取非 +not True # => False +not False # => True # 相等 -1 == 1 #=> True -2 == 1 #=> False +1 == 1 # => True +2 == 1 # => False # 不等 -1 != 1 #=> False -2 != 1 #=> True +1 != 1 # => False +2 != 1 # => True # 更多的比较操作符 -1 < 10 #=> True -1 > 10 #=> False -2 <= 2 #=> True -2 >= 2 #=> True +1 < 10 # => True +1 > 10 # => False +2 <= 2 # => True +2 >= 2 # => True # 比较运算可以连起来写! -1 < 2 < 3 #=> True -2 < 3 < 2 #=> False +1 < 2 < 3 # => True +2 < 3 < 2 # => False -# 字符串通过"或'括起来 +# 字符串通过 " 或 ' 括起来 "This is a string." 'This is also a string.' # 字符串通过加号拼接 -"Hello " + "world!" #=> "Hello world!" +"Hello " + "world!" # => "Hello world!" # 字符串可以被视为字符的列表 -"This is a string"[0] #=> 'T' +"This is a string"[0] # => 'T' # % 可以用来格式化字符串 "%s can be %s" % ("strings", "interpolated") -# 也可以用format方法来格式化字符串 +# 也可以用 format 方法来格式化字符串 # 推荐使用这个方法 "{0} can be {1}".format("strings", "formatted") # 也可以用变量名代替数字 "{name} wants to eat {food}".format(name="Bob", food="lasagna") # None 是对象 -None #=> None +None # => None # 不要用相等 `==` 符号来和None进行比较 -# 要用 `is` -"etc" is None #=> False -None is None #=> True +# 要用 `is` +"etc" is None # => False +None is None # => True # 'is' 可以用来比较对象的相等性 # 这个操作符在比较原始数据时没多少用,但是比较对象时必不可少 -# None, 0, 和空字符串都被算作False -# 其他的均为True -0 == False #=> True -"" == False #=> True +# None, 0, 和空字符串都被算作 False +# 其他的均为 True +0 == False # => True +"" == False # => True #################################################### @@ -116,16 +117,16 @@ None is None #=> True print "I'm Python. Nice to meet you!" -# 给变量赋值前不需要事先生命 -some_var = 5 # 规范用小写字母和下划线来做为变量名 -some_var #=> 5 +# 给变量赋值前不需要事先声明 +some_var = 5 # 一般建议使用小写字母和下划线组合来做为变量名 +some_var # => 5 -# 访问之前为赋值的变量会抛出异常 -# 查看控制流程一节来了解异常处理 -some_other_var # 抛出命名异常 +# 访问未赋值的变量会抛出异常 +# 可以查看控制流程一节来了解如何异常处理 +some_other_var # 抛出 NameError -# if语句可以作为表达式来使用 -"yahoo!" if 3 > 2 else 2 #=> "yahoo!" +# if 语句可以作为表达式来使用 +"yahoo!" if 3 > 2 else 2 # => "yahoo!" # 列表用来保存序列 li = [] @@ -133,64 +134,64 @@ li = [] other_li = [4, 5, 6] # 在列表末尾添加元素 -li.append(1) #li 现在是 [1] -li.append(2) #li 现在是 [1, 2] -li.append(4) #li 现在是 [1, 2, 4] -li.append(3) #li 现在是 [1, 2, 4, 3] +li.append(1) # li 现在是 [1] +li.append(2) # li 现在是 [1, 2] +li.append(4) # li 现在是 [1, 2, 4] +li.append(3) # li 现在是 [1, 2, 4, 3] # 移除列表末尾元素 -li.pop() #=> 3 and li is now [1, 2, 4] -# 放回来 +li.pop() # => 3 li 现在是 [1, 2, 4] +# 重新加进去 li.append(3) # li is now [1, 2, 4, 3] again. # 像其他语言访问数组一样访问列表 -li[0] #=> 1 +li[0] # => 1 # 访问最后一个元素 -li[-1] #=> 3 +li[-1] # => 3 # 越界会抛出异常 -li[4] # 抛出越界异常 +li[4] # 抛出越界异常 # 切片语法需要用到列表的索引访问 # 可以看做数学之中左闭右开区间 -li[1:3] #=> [2, 4] +li[1:3] # => [2, 4] # 省略开头的元素 -li[2:] #=> [4, 3] +li[2:] # => [4, 3] # 省略末尾的元素 -li[:3] #=> [1, 2, 4] +li[:3] # => [1, 2, 4] # 删除特定元素 -del li[2] # li 现在是 [1, 2, 3] +del li[2] # li 现在是 [1, 2, 3] # 合并列表 -li + other_li #=> [1, 2, 3, 4, 5, 6] - 不改变这两个列表 +li + other_li # => [1, 2, 3, 4, 5, 6] - 并不会不改变这两个列表 -# 通过拼接合并列表 -li.extend(other_li) # li 是 [1, 2, 3, 4, 5, 6] +# 通过拼接来合并列表 +li.extend(other_li) # li 是 [1, 2, 3, 4, 5, 6] -# 用in来返回元素是否在列表中 -1 in li #=> True +# 用 in 来返回元素是否在列表中 +1 in li # => True # 返回列表长度 -len(li) #=> 6 +len(li) # => 6 -# 元组类似于列表,但是他是不可改变的 +# 元组类似于列表,但它是不可改变的 tup = (1, 2, 3) -tup[0] #=> 1 +tup[0] # => 1 tup[0] = 3 # 类型错误 # 对于大多数的列表操作,也适用于元组 -len(tup) #=> 3 -tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) -tup[:2] #=> (1, 2) -2 in tup #=> True +len(tup) # => 3 +tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +tup[:2] # => (1, 2) +2 in tup # => True # 你可以将元组解包赋给多个变量 -a, b, c = (1, 2, 3) # a是1,b是2,c是3 -# 如果不加括号,那么会自动视为元组 +a, b, c = (1, 2, 3) # a 是 1,b 是 2,c 是 3 +# 如果不加括号,将会被自动视为元组 d, e, f = 4, 5, 6 # 现在我们可以看看交换两个数字是多么容易的事 -e, d = d, e # d是5,e是4 +e, d = d, e # d 是 5,e 是 4 # 字典用来储存映射关系 @@ -199,59 +200,59 @@ empty_dict = {} filled_dict = {"one": 1, "two": 2, "three": 3} # 字典也用中括号访问元素 -filled_dict["one"] #=> 1 +filled_dict["one"] # => 1 # 把所有的键保存在列表中 -filled_dict.keys() #=> ["three", "two", "one"] +filled_dict.keys() # => ["three", "two", "one"] # 键的顺序并不是唯一的,得到的不一定是这个顺序 # 把所有的值保存在列表中 -filled_dict.values() #=> [3, 2, 1] +filled_dict.values() # => [3, 2, 1] # 和键的顺序相同 # 判断一个键是否存在 -"one" in filled_dict #=> True -1 in filled_dict #=> False +"one" in filled_dict # => True +1 in filled_dict # => False -# 查询一个不存在的键会抛出键异常 -filled_dict["four"] # 键异常 +# 查询一个不存在的键会抛出 KeyError +filled_dict["four"] # KeyError -# 用get方法来避免键异常 -filled_dict.get("one") #=> 1 -filled_dict.get("four") #=> None -# get方法支持在不存在的时候返回一个默认值 -filled_dict.get("one", 4) #=> 1 -filled_dict.get("four", 4) #=> 4 +# 用 get 方法来避免 KeyError +filled_dict.get("one") # => 1 +filled_dict.get("four") # => None +# get 方法支持在不存在的时候返回一个默认值 +filled_dict.get("one", 4) # => 1 +filled_dict.get("four", 4) # => 4 -# Setdefault是一个更安全的添加字典元素的方法 -filled_dict.setdefault("five", 5) #filled_dict["five"] 的值为 5 -filled_dict.setdefault("five", 6) #filled_dict["five"] 的值仍然是 5 +# setdefault 是一个更安全的添加字典元素的方法 +filled_dict.setdefault("five", 5) # filled_dict["five"] 的值为 5 +filled_dict.setdefault("five", 6) # filled_dict["five"] 的值仍然是 5 # 集合储存无顺序的元素 empty_set = set() -# 出事话一个集合 -some_set = set([1,2,2,3,4]) # filled_set 现在是 set([1, 2, 3, 4]) +# 初始化一个集合 +some_set = set([1, 2, 2, 3, 4]) # filled_set 现在是 set([1, 2, 3, 4]) # Python 2.7 之后,大括号可以用来表示集合 -filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} +filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} -# 为集合添加元素 -filled_set.add(5) # filled_set 现在是 {1, 2, 3, 4, 5} +# 向集合添加元素 +filled_set.add(5) # filled_set 现在是 {1, 2, 3, 4, 5} -# 用&来实现集合的交 +# 用 & 来计算集合的交 other_set = {3, 4, 5, 6} -filled_set & other_set #=> {3, 4, 5} +filled_set & other_set # => {3, 4, 5} -# 用|来实现集合的并 -filled_set | other_set #=> {1, 2, 3, 4, 5, 6} +# 用 | 来计算集合的并 +filled_set | other_set # => {1, 2, 3, 4, 5, 6} -# 用-来实现集合的差 -{1,2,3,4} - {2,3,5} #=> {1, 4} +# 用 - 来计算集合的差 +{1, 2, 3, 4} - {2, 3, 5} # => {1, 4} -# 用in来判断元素是否存在于集合中 -2 in filled_set #=> True -10 in filled_set #=> False +# 用 in 来判断元素是否存在于集合中 +2 in filled_set # => True +10 in filled_set # => False #################################################### @@ -261,13 +262,13 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} # 新建一个变量 some_var = 5 -# 这是个if语句,在python中缩进是很重要的。 -# 会输出 "some var is smaller than 10" +# 这是个 if 语句,在 python 中缩进是很重要的。 +# 下面的代码片段将会输出 "some var is smaller than 10" if some_var > 10: print "some_var is totally bigger than 10." elif some_var < 10: # 这个 elif 语句是不必须的 print "some_var is smaller than 10." -else: # 也不是必须的 +else: # 这个 else 也不是必须的 print "some_var is indeed 10." @@ -281,7 +282,7 @@ else: # 也不是必须的 for animal in ["dog", "cat", "mouse"]: # 你可以用 % 来格式化字符串 print "%s is a mammal" % animal - + """ `range(number)` 返回从0到给定数字的列表 输出: @@ -294,7 +295,7 @@ for i in range(4): print i """ -While循环 +while 循环 输出: 0 1 @@ -304,29 +305,29 @@ While循环 x = 0 while x < 4: print x - x += 1 # Shorthand for x = x + 1 + x += 1 # x = x + 1 的简写 -# 用 try/except块来处理异常 +# 用 try/except 块来处理异常 # Python 2.6 及以上适用: try: - # 用raise来抛出异常 + # 用 raise 来抛出异常 raise IndexError("This is an index error") except IndexError as e: - pass # Pass就是什么都不做,不过通常这里会做一些恢复工作 + pass # pass 就是什么都不做,不过通常这里会做一些恢复工作 #################################################### ## 4. 函数 #################################################### -# 用def来新建函数 +# 用 def 来新建函数 def add(x, y): print "x is %s and y is %s" % (x, y) - return x + y # Return values with a return statement + return x + y # 通过 return 来返回值 # 调用带参数的函数 -add(5, 6) #=> 输出 "x is 5 and y is 6" 返回 11 +add(5, 6) # => 输出 "x is 5 and y is 6" 返回 11 # 通过关键字赋值来调用函数 add(y=6, x=5) # 顺序是无所谓的 @@ -335,7 +336,7 @@ add(y=6, x=5) # 顺序是无所谓的 def varargs(*args): return args -varargs(1, 2, 3) #=> (1,2,3) +varargs(1, 2, 3) # => (1,2,3) # 我们也可以定义接受多个变量的函数,这些变量是按照关键字排列的 @@ -343,7 +344,7 @@ def keyword_args(**kwargs): return kwargs # 实际效果: -keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} +keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} # 你也可以同时将一个函数定义成两种形式 def all_the_args(*args, **kwargs): @@ -355,38 +356,38 @@ all_the_args(1, 2, a=3, b=4) prints: {"a": 3, "b": 4} """ -# 当调用函数的时候,我们也可以和之前所做的相反,把元组和字典展开为参数 +# 当调用函数的时候,我们也可以进行相反的操作,把元组和字典展开为参数 args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -all_the_args(*args) # equivalent to foo(1, 2, 3, 4) -all_the_args(**kwargs) # equivalent to foo(a=3, b=4) -all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # 等价于 foo(1, 2, 3, 4) +all_the_args(**kwargs) # 等价于 foo(a=3, b=4) +all_the_args(*args, **kwargs) # 等价于 foo(1, 2, 3, 4, a=3, b=4) -# Python 有一等函数: +# 函数在 python 中是一等公民 def create_adder(x): def adder(y): return x + y return adder add_10 = create_adder(10) -add_10(3) #=> 13 +add_10(3) # => 13 # 匿名函数 -(lambda x: x > 2)(3) #=> True +(lambda x: x > 2)(3) # => True # 内置高阶函数 -map(add_10, [1,2,3]) #=> [11, 12, 13] -filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] +map(add_10, [1, 2, 3]) # => [11, 12, 13] +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # 可以用列表方法来对高阶函数进行更巧妙的引用 -[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] -[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] +[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] +[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7] #################################################### ## 5. 类 #################################################### -# 我们新建的类是从object类中继承的 +# 我们新建的类是从 object 类中继承的 class Human(object): # 类属性,由所有类的对象共享 @@ -397,9 +398,9 @@ class Human(object): # 将参数赋给对象成员属性 self.name = name - # 成员方法,参数要有self + # 成员方法,参数要有 self def say(self, msg): - return "%s: %s" % (self.name, msg) + return "%s: %s" % (self.name, msg) # 类方法由所有类的对象共享 # 这类方法在调用时,会把类本身传给第一个参数 @@ -421,15 +422,15 @@ j = Human("Joel") print j.say("hello") # 输出 "Joel: hello" # 访问类的方法 -i.get_species() #=> "H. sapiens" +i.get_species() # => "H. sapiens" # 改变共享属性 Human.species = "H. neanderthalensis" -i.get_species() #=> "H. neanderthalensis" -j.get_species() #=> "H. neanderthalensis" +i.get_species() # => "H. neanderthalensis" +j.get_species() # => "H. neanderthalensis" # 访问静态变量 -Human.grunt() #=> "*grunt*" +Human.grunt() # => "*grunt*" #################################################### @@ -438,12 +439,12 @@ Human.grunt() #=> "*grunt*" # 我们可以导入其他模块 import math -print math.sqrt(16) #=> 4 +print math.sqrt(16) # => 4 -# 我们也可以从一个模块中特定的函数 +# 我们也可以从一个模块中导入特定的函数 from math import ceil, floor -print ceil(3.7) #=> 4.0 -print floor(3.7) #=> 3.0 +print ceil(3.7) # => 4.0 +print floor(3.7) # => 3.0 # 从模块中导入所有的函数 # 警告:不推荐使用 @@ -451,13 +452,13 @@ from math import * # 简写模块名 import math as m -math.sqrt(16) == m.sqrt(16) #=> True +math.sqrt(16) == m.sqrt(16) # => True # Python的模块其实只是普通的python文件 # 你也可以创建自己的模块,并且导入它们 # 模块的名字就和文件的名字相同 -# 以可以通过下面的信息找找要成为模块需要什么属性或方法 +# 也可以通过下面的方法查看模块中有什么属性和方法 import math dir(math) -- cgit v1.2.3 From 4d705abd99dbe13fbdb50b5d5e74d6fe8c18f559 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 19:59:19 -0700 Subject: Piddly things --- go.html.markdown | 354 +++++++++++++++++++++---------------------- pt-br/elisp-pt.html.markdown | 16 +- 2 files changed, 185 insertions(+), 185 deletions(-) diff --git a/go.html.markdown b/go.html.markdown index e7b35926..4db76a49 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -18,7 +18,7 @@ help with large-scale programming. Go comes with a great standard library and an enthusiastic community. -```Go +```go // Single line comment /* Multi- line comment */ @@ -29,260 +29,260 @@ package main // Import declaration declares library packages referenced in this file. import ( - "fmt" // A package in the Go standard library - "net/http" // Yes, a web server! - "strconv" // String conversions + "fmt" // A package in the Go standard library + "net/http" // Yes, a web server! + "strconv" // String conversions ) // A function definition. Main is special. It is the entry point for the // executable program. Love it or hate it, Go uses brace brackets. func main() { - // Println outputs a line to stdout. - // Qualify it with the package name, fmt. - fmt.Println("Hello world!") + // Println outputs a line to stdout. + // Qualify it with the package name, fmt. + fmt.Println("Hello world!") - // Call another function within this package. - beyondHello() + // Call another function within this package. + beyondHello() } // Functions have parameters in parentheses. // If there are no parameters, empty parens are still required. func beyondHello() { - var x int // Variable declaration. Variables must be declared before use. - x = 3 // Variable assignment. - // "Short" declarations use := to infer the type, declare, and assign. - y := 4 - sum, prod := learnMultiple(x, y) // function returns two values - fmt.Println("sum:", sum, "prod:", prod) // simple output - learnTypes() // < y minutes, learn more! + var x int // Variable declaration. Variables must be declared before use. + x = 3 // Variable assignment. + // "Short" declarations use := to infer the type, declare, and assign. + y := 4 + sum, prod := learnMultiple(x, y) // function returns two values + fmt.Println("sum:", sum, "prod:", prod) // simple output + learnTypes() // < y minutes, learn more! } // Functions can have parameters and (multiple!) return values. func learnMultiple(x, y int) (sum, prod int) { - return x + y, x * y // return two values + return x + y, x * y // return two values } // Some built-in types and literals. func learnTypes() { - // Short declaration usually gives you what you want. - s := "Learn Go!" // string type + // Short declaration usually gives you what you want. + s := "Learn Go!" // string type - s2 := `A "raw" string literal + s2 := `A "raw" string literal can include line breaks.` // same string type - // non-ASCII literal. Go source is UTF-8. - g := 'Σ' // rune type, an alias for uint32, holds a UTF-8 code point + // non-ASCII literal. Go source is UTF-8. + g := 'Σ' // rune type, an alias for uint32, holds a UTF-8 code point - f := 3.14195 // float64, an IEEE-754 64-bit floating point number - c := 3 + 4i // complex128, represented internally with two float64s + f := 3.14195 // float64, an IEEE-754 64-bit floating point number + c := 3 + 4i // complex128, represented internally with two float64s - // Var syntax with an initializers. - var u uint = 7 // unsigned, but implementation dependent size as with int - var pi float32 = 22. / 7 + // Var syntax with an initializers. + var u uint = 7 // unsigned, but implementation dependent size as with int + var pi float32 = 22. / 7 - // Conversion syntax with a short declaration. - n := byte('\n') // byte is an alias for uint8 + // Conversion syntax with a short declaration. + n := byte('\n') // byte is an alias for uint8 - // Arrays have size fixed at compile time. - var a4 [4]int // an array of 4 ints, initialized to all 0 - a3 := [...]int{3, 1, 5} // an array of 3 ints, initialized as shown + // Arrays have size fixed at compile time. + var a4 [4]int // an array of 4 ints, initialized to all 0 + a3 := [...]int{3, 1, 5} // an array of 3 ints, initialized as shown - // Slices have dynamic size. Arrays and slices each have advantages - // but use cases for slices are much more common. - s3 := []int{4, 5, 9} // compare to a3. no ellipsis here - s4 := make([]int, 4) // allocates slice of 4 ints, initialized to all 0 - var d2 [][]float64 // declaration only, nothing allocated here - bs := []byte("a slice") // type conversion syntax + // Slices have dynamic size. Arrays and slices each have advantages + // but use cases for slices are much more common. + s3 := []int{4, 5, 9} // compare to a3. no ellipsis here + s4 := make([]int, 4) // allocates slice of 4 ints, initialized to all 0 + var d2 [][]float64 // declaration only, nothing allocated here + bs := []byte("a slice") // type conversion syntax - p, q := learnMemory() // declares p, q to be type pointer to int. - fmt.Println(*p, *q) // * follows a pointer. This prints two ints. + p, q := learnMemory() // declares p, q to be type pointer to int. + fmt.Println(*p, *q) // * follows a pointer. This prints two ints. - // Maps are a dynamically growable associative array type, like the - // hash or dictionary types of some other languages. - m := map[string]int{"three": 3, "four": 4} - m["one"] = 1 + // Maps are a dynamically growable associative array type, like the + // hash or dictionary types of some other languages. + m := map[string]int{"three": 3, "four": 4} + m["one"] = 1 - // Unused variables are an error in Go. - // The underbar lets you "use" a variable but discard its value. - _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs - // Output of course counts as using a variable. - fmt.Println(s, c, a4, s3, d2, m) + // Unused variables are an error in Go. + // The underbar lets you "use" a variable but discard its value. + _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs + // Output of course counts as using a variable. + fmt.Println(s, c, a4, s3, d2, m) - learnFlowControl() // back in the flow + learnFlowControl() // back in the flow } // Go is fully garbage collected. It has pointers but no pointer arithmetic. // You can make a mistake with a nil pointer, but not by incrementing a pointer. func learnMemory() (p, q *int) { - // Named return values p and q have type pointer to int. - p = new(int) // built-in function new allocates memory. - // The allocated int is initialized to 0, p is no longer nil. - s := make([]int, 20) // allocate 20 ints as a single block of memory - s[3] = 7 // assign one of them - r := -2 // declare another local variable - return &s[3], &r // & takes the address of an object. + // Named return values p and q have type pointer to int. + p = new(int) // built-in function new allocates memory. + // The allocated int is initialized to 0, p is no longer nil. + s := make([]int, 20) // allocate 20 ints as a single block of memory + s[3] = 7 // assign one of them + r := -2 // declare another local variable + return &s[3], &r // & takes the address of an object. } func expensiveComputation() int { - return 1e6 + return 1e6 } func learnFlowControl() { - // If statements require brace brackets, and do not require parens. - if true { - fmt.Println("told ya") - } - // Formatting is standardized by the command line command "go fmt." - if false { - // pout - } else { - // gloat - } - // Use switch in preference to chained if statements. - x := 1 - switch x { - case 0: - case 1: - // cases don't "fall through" - case 2: - // unreached - } - // Like if, for doesn't use parens either. - for x := 0; x < 3; x++ { // ++ is a statement - fmt.Println("iteration", x) - } - // x == 1 here. - - // For is the only loop statement in Go, but it has alternate forms. - for { // infinite loop - break // just kidding - continue // unreached - } - // As with for, := in an if statement means to declare and assign y first, - // then test y > x. - if y := expensiveComputation(); y > x { - x = y - } - // Function literals are closures. - xBig := func() bool { - return x > 100 // references x declared above switch statement. - } - fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x) - x /= 1e5 // this makes it == 10 - fmt.Println("xBig:", xBig()) // false now - - // When you need it, you'll love it. - goto love + // If statements require brace brackets, and do not require parens. + if true { + fmt.Println("told ya") + } + // Formatting is standardized by the command line command "go fmt." + if false { + // pout + } else { + // gloat + } + // Use switch in preference to chained if statements. + x := 1 + switch x { + case 0: + case 1: + // cases don't "fall through" + case 2: + // unreached + } + // Like if, for doesn't use parens either. + for x := 0; x < 3; x++ { // ++ is a statement + fmt.Println("iteration", x) + } + // x == 1 here. + + // For is the only loop statement in Go, but it has alternate forms. + for { // infinite loop + break // just kidding + continue // unreached + } + // As with for, := in an if statement means to declare and assign y first, + // then test y > x. + if y := expensiveComputation(); y > x { + x = y + } + // Function literals are closures. + xBig := func() bool { + return x > 100 // references x declared above switch statement. + } + fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x) + x /= 1e5 // this makes it == 10 + fmt.Println("xBig:", xBig()) // false now + + // When you need it, you'll love it. + goto love love: - learnInterfaces() // Good stuff coming up! + learnInterfaces() // Good stuff coming up! } // Define Stringer as an interface type with one method, String. type Stringer interface { - String() string + String() string } // Define pair as a struct with two fields, ints named x and y. type pair struct { - x, y int + x, y int } // Define a method on type pair. Pair now implements Stringer. func (p pair) String() string { // p is called the "receiver" - // Sprintf is another public function in package fmt. - // Dot syntax references fields of p. - return fmt.Sprintf("(%d, %d)", p.x, p.y) + // Sprintf is another public function in package fmt. + // Dot syntax references fields of p. + return fmt.Sprintf("(%d, %d)", p.x, p.y) } func learnInterfaces() { - // Brace syntax is a "struct literal." It evaluates to an initialized - // struct. The := syntax declares and initializes p to this struct. - p := pair{3, 4} - fmt.Println(p.String()) // call String method of p, of type pair. - var i Stringer // declare i of interface type Stringer. - i = p // valid because pair implements Stringer - // Call String method of i, of type Stringer. Output same as above. - fmt.Println(i.String()) - - // Functions in the fmt package call the String method to ask an object - // for a printable representation of itself. - fmt.Println(p) // output same as above. Println calls String method. - fmt.Println(i) // output same as above - - learnErrorHandling() + // Brace syntax is a "struct literal." It evaluates to an initialized + // struct. The := syntax declares and initializes p to this struct. + p := pair{3, 4} + fmt.Println(p.String()) // call String method of p, of type pair. + var i Stringer // declare i of interface type Stringer. + i = p // valid because pair implements Stringer + // Call String method of i, of type Stringer. Output same as above. + fmt.Println(i.String()) + + // Functions in the fmt package call the String method to ask an object + // for a printable representation of itself. + fmt.Println(p) // output same as above. Println calls String method. + fmt.Println(i) // output same as above + + learnErrorHandling() } func learnErrorHandling() { - // ", ok" idiom used to tell if something worked or not. - m := map[int]string{3: "three", 4: "four"} - if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map. - fmt.Println("no one there") - } else { - fmt.Print(x) // x would be the value, if it were in the map. - } - // An error value communicates not just "ok" but more about the problem. - if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value - // prints "strconv.ParseInt: parsing "non-int": invalid syntax" - fmt.Println(err) - } - // We'll revisit interfaces a little later. Meanwhile, - learnConcurrency() + // ", ok" idiom used to tell if something worked or not. + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map. + fmt.Println("no one there") + } else { + fmt.Print(x) // x would be the value, if it were in the map. + } + // An error value communicates not just "ok" but more about the problem. + if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value + // prints "strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } + // We'll revisit interfaces a little later. Meanwhile, + learnConcurrency() } // c is a channel, a concurrency-safe communication object. func inc(i int, c chan int) { - c <- i + 1 // <- is the "send" operator when a channel appears on the left. + c <- i + 1 // <- is the "send" operator when a channel appears on the left. } // We'll use inc to increment some numbers concurrently. func learnConcurrency() { - // Same make function used earlier to make a slice. Make allocates and - // initializes slices, maps, and channels. - c := make(chan int) - // Start three concurrent goroutines. Numbers will be incremented - // concurrently, perhaps in parallel if the machine is capable and - // properly configured. All three send to the same channel. - go inc(0, c) // go is a statement that starts a new goroutine. - go inc(10, c) - go inc(-805, c) - // Read three results from the channel and print them out. - // There is no telling in what order the results will arrive! - fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator. - - cs := make(chan string) // another channel, this one handles strings. - cc := make(chan chan string) // a channel of channels. - go func() { c <- 84 }() // start a new goroutine just to send a value - go func() { cs <- "wordy" }() // again, for cs this time - // Select has syntax like a switch statement but each case involves - // a channel operation. It selects a case at random out of the cases - // that are ready to communicate. - select { - case i := <-c: // the value received can be assigned to a variable - fmt.Println("it's a", i) - case <-cs: // or the value received can be discarded - fmt.Println("it's a string") - case <-cc: // empty channel, not ready for communication. - fmt.Println("didn't happen.") - } - // At this point a value was taken from either c or cs. One of the two - // goroutines started above has completed, the other will remain blocked. - - learnWebProgramming() // Go does it. You want to do it too. + // Same make function used earlier to make a slice. Make allocates and + // initializes slices, maps, and channels. + c := make(chan int) + // Start three concurrent goroutines. Numbers will be incremented + // concurrently, perhaps in parallel if the machine is capable and + // properly configured. All three send to the same channel. + go inc(0, c) // go is a statement that starts a new goroutine. + go inc(10, c) + go inc(-805, c) + // Read three results from the channel and print them out. + // There is no telling in what order the results will arrive! + fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator. + + cs := make(chan string) // another channel, this one handles strings. + cc := make(chan chan string) // a channel of channels. + go func() { c <- 84 }() // start a new goroutine just to send a value + go func() { cs <- "wordy" }() // again, for cs this time + // Select has syntax like a switch statement but each case involves + // a channel operation. It selects a case at random out of the cases + // that are ready to communicate. + select { + case i := <-c: // the value received can be assigned to a variable + fmt.Println("it's a", i) + case <-cs: // or the value received can be discarded + fmt.Println("it's a string") + case <-cc: // empty channel, not ready for communication. + fmt.Println("didn't happen.") + } + // At this point a value was taken from either c or cs. One of the two + // goroutines started above has completed, the other will remain blocked. + + learnWebProgramming() // Go does it. You want to do it too. } // A single function from package http starts a web server. func learnWebProgramming() { - // ListenAndServe first parameter is TCP address to listen at. - // Second parameter is an interface, specifically http.Handler. - err := http.ListenAndServe(":8080", pair{}) - fmt.Println(err) // don't ignore errors + // ListenAndServe first parameter is TCP address to listen at. + // Second parameter is an interface, specifically http.Handler. + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // don't ignore errors } // Make pair an http.Handler by implementing its only method, ServeHTTP. func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // Serve data with a method of http.ResponseWriter - w.Write([]byte("You learned Go in Y minutes!")) + // Serve data with a method of http.ResponseWriter + w.Write([]byte("You learned Go in Y minutes!")) } ``` diff --git a/pt-br/elisp-pt.html.markdown b/pt-br/elisp-pt.html.markdown index 9031cad9..fc2d1e40 100644 --- a/pt-br/elisp-pt.html.markdown +++ b/pt-br/elisp-pt.html.markdown @@ -4,7 +4,7 @@ contributors: - ["Bastien Guerry", "http://bzg.fr"] translators: - ["Lucas Tadeu Teixeira", "http://ltt.me"] -lang: pt-br +lang: pt-br filename: learn-emacs-lisp-pt.el --- @@ -30,9 +30,9 @@ filename: learn-emacs-lisp-pt.el ;; Realizar este tutorial não danificará seu computador, a menos ;; que você fique tão irritado a ponto de jogá-lo no chão. Neste caso, ;; me abstenho de qualquer responsabilidade. Divirta-se! - + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; +;; ;; Abra o Emacs. ;; ;; Aperte a tecla `q' para ocultar a mensagem de boas vindas. @@ -45,11 +45,11 @@ filename: learn-emacs-lisp-pt.el ;; O buffer de rascunho (i.e., "scratch") é o buffer padrão quando ;; o Emacs é aberto. Você nunca está editando arquivos: você está ;; editando buffers que você pode salvar em um arquivo. -;; +;; ;; "Lisp interaction" refere-se a um conjunto de comandos disponíveis aqui. -;; -;; O Emacs possui um conjunto de comandos embutidos (disponíveis em -;; qualquer buffer) e vários subconjuntos de comandos disponíveis +;; +;; O Emacs possui um conjunto de comandos embutidos (disponíveis em +;; qualquer buffer) e vários subconjuntos de comandos disponíveis ;; quando você ativa um modo específico. Aqui nós utilizamos ;; `lisp-interaction-mode', que possui comandos para interpretar e navegar ;; em código Elisp. @@ -137,7 +137,7 @@ filename: learn-emacs-lisp-pt.el ;; => [a tela exibirá duas janelas e o cursor estará no buffer *test*] ;; Posicione o mouse sobre a janela superior e clique com o botão -;; esquerdo para voltar. Ou você pode utilizar `C-xo' (i.e. segure +;; esquerdo para voltar. Ou você pode utilizar `C-xo' (i.e. segure ;; ctrl-x e aperte o) para voltar para a outra janela, de forma interativa. ;; Você pode combinar várias "sexps" com `progn': -- cgit v1.2.3 From 69c4e4e509019740cab8eb2074826b5c5c9f6ec3 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Wed, 14 Aug 2013 00:12:15 -0500 Subject: Made some corrections and clarifications --- javascript.html.markdown | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index cc279b9a..865edc36 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -104,9 +104,10 @@ false // There's also null and undefined null // used to indicate a deliberate non-value -undefined // used to indicate a value that hasn't been set yet +undefined // used to indicate a value is not currently present (although undefined + // is actually a value itself) -// null, undefined, NaN, 0 and "" are falsy, and everything else is truthy. +// false, null, undefined, NaN, 0 and "" are falsy, and everything else is truthy. // Note that 0 is falsy and "0" is truthy, even though 0 == "0". /////////////////////////////////// @@ -142,7 +143,7 @@ myArray[1] // = 45 // JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other // languages: an unordered collection of key-value pairs. -{key1: "Hello", key2: "World"} +var myObj = {key1: "Hello", key2: "World"} // Keys are strings, but quotes aren't required if they're a valid // JavaScript identifier. Values can be any type. @@ -211,9 +212,13 @@ function myFunction(thing){ myFunction("foo") // = "FOO" // Functions can also be defined "anonymously" - without a name: -function(thing){ +(function(thing){ return thing.toLowerCase() -} +}) +// Note: Stand-alone function declarations require an identifier name. +// Anonymous functions are values, not declarations, so they must be +// treated as a value. We wrap ours here in ( ) to do so, or you could assign +// it to a variable, or pass it as a parameter to another function. // (we can't call our function, since we don't have a name to refer to it with) // JavaScript functions are first class objects, so they can be reassigned to @@ -247,12 +252,15 @@ i // = 5 - not undefined as you'd expect in a block-scoped language // in a web browser is always 'window'. The global object may have a // different name in non-browser environments such as Node.js. window.permanent = 10 - // Or, as previously mentioned, we can just leave the var keyword off. + // Or, as previously mentioned, if you leave the var keyword off, a + // global variable will be created when you assign it a value. However, + // this behavior is frowned upon, and in fact is disallowed in "strict + // mode". permanent2 = 15 })() temporary // raises ReferenceError permanent // = 10 -permanent2 // = 15 +permanent2 // = 15 <-- the accidental global variable // One of JavaScript's most powerful features is closures. If a function is // defined inside another function, the inner function has access to all the @@ -268,6 +276,12 @@ function sayHelloInFiveSeconds(name){ // access to the value of prompt. } sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s +// inner() has access to the variable "prompt" strictly because of lexical scope. +// A closure is being demonstrated because the inner() function is being executed +// at a later time, and in fact being executed "outside" the scope where it was +// declared (inside of the implementation of setTimeout()), but yet inner() STILL +// has access to the variable "prompt". It is said that inner() has a "closure" +// over the variables of sayHelloInFiveSeconds(). /////////////////////////////////// // 5. More about Objects; Constructors and Prototypes -- cgit v1.2.3 From 02ea95377d4963c60a96976b4092bdcc2b638594 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 14 Aug 2013 17:20:34 +0930 Subject: Avoid telling people about techniques that are discouraged anyway --- javascript.html.markdown | 6 ------ 1 file changed, 6 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 865edc36..1f188832 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -252,15 +252,9 @@ i // = 5 - not undefined as you'd expect in a block-scoped language // in a web browser is always 'window'. The global object may have a // different name in non-browser environments such as Node.js. window.permanent = 10 - // Or, as previously mentioned, if you leave the var keyword off, a - // global variable will be created when you assign it a value. However, - // this behavior is frowned upon, and in fact is disallowed in "strict - // mode". - permanent2 = 15 })() temporary // raises ReferenceError permanent // = 10 -permanent2 // = 15 <-- the accidental global variable // One of JavaScript's most powerful features is closures. If a function is // defined inside another function, the inner function has access to all the -- cgit v1.2.3 From a82859f95bb235074d740530dfa60afca7025223 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 14 Aug 2013 17:21:29 +0930 Subject: Explain anonymous functions after first-class, and more concisely --- javascript.html.markdown | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 1f188832..bcaf9a29 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -211,16 +211,6 @@ function myFunction(thing){ } myFunction("foo") // = "FOO" -// Functions can also be defined "anonymously" - without a name: -(function(thing){ - return thing.toLowerCase() -}) -// Note: Stand-alone function declarations require an identifier name. -// Anonymous functions are values, not declarations, so they must be -// treated as a value. We wrap ours here in ( ) to do so, or you could assign -// it to a variable, or pass it as a parameter to another function. -// (we can't call our function, since we don't have a name to refer to it with) - // JavaScript functions are first class objects, so they can be reassigned to // different variable names and passed to other functions as arguments - for // example, when supplying an event handler: @@ -229,9 +219,16 @@ function myFunction(){ } setTimeout(myFunction, 5000) +// Functions can also be defined "anonymously" - without a name: +var lowerFunction = function(thing){ + return thing.toLowerCase() +} +lowerFunction("Foo") // = "foo" +// (note: we've assigned our anonymous function to a variable - if we didn't, we +// wouldn't be able to access it) + // You can even write the function statement directly in the call to the other // function. - setTimeout(function myFunction(){ // this code will be called in 5 seconds' time }, 5000) -- cgit v1.2.3 From 5f2928df6b19337426bed0a60650be79bda437a1 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 14 Aug 2013 17:21:44 +0930 Subject: More concise explanation of closures --- javascript.html.markdown | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index bcaf9a29..9b87b022 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -185,7 +185,7 @@ do { input = getInput() } while (!isValid(input)) -// the for loop is the same as C and Java: +// the for loop is the same as C and Java: // initialisation; continue condition; iteration. for (var i = 0; i < 5; i++){ // will run 5 times @@ -255,24 +255,19 @@ permanent // = 10 // One of JavaScript's most powerful features is closures. If a function is // defined inside another function, the inner function has access to all the -// outer function's variables. +// outer function's variables, even after the outer function exits. function sayHelloInFiveSeconds(name){ var prompt = "Hello, " + name + "!" function inner(){ alert(prompt) } setTimeout(inner, 5000) - // setTimeout is asynchronous, so this function will finish without waiting - // 5 seconds. However, once the 5 seconds is up, inner will still have - // access to the value of prompt. + // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will + // exit immediately, and setTimeout will call inner afterwards. However, + // because inner is "closed over" sayHelloInFiveSeconds, inner still has + // access to the 'prompt' variable when it is finally called. } sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s -// inner() has access to the variable "prompt" strictly because of lexical scope. -// A closure is being demonstrated because the inner() function is being executed -// at a later time, and in fact being executed "outside" the scope where it was -// declared (inside of the implementation of setTimeout()), but yet inner() STILL -// has access to the variable "prompt". It is said that inner() has a "closure" -// over the variables of sayHelloInFiveSeconds(). /////////////////////////////////// // 5. More about Objects; Constructors and Prototypes -- cgit v1.2.3 From 7d015d8cee3a71fb54ed205a348428ab589ef1a6 Mon Sep 17 00:00:00 2001 From: Pablo Elices Date: Wed, 14 Aug 2013 13:12:27 +0200 Subject: Translate CoffeeScript file to spanish. --- es-es/coffeescript-es.html.markdown | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 es-es/coffeescript-es.html.markdown diff --git a/es-es/coffeescript-es.html.markdown b/es-es/coffeescript-es.html.markdown new file mode 100644 index 00000000..eb6825c0 --- /dev/null +++ b/es-es/coffeescript-es.html.markdown @@ -0,0 +1,56 @@ +--- +language: coffeescript +lang: es-es +contributors: + - ["Tenor Biel", "http://github.com/L8D"] +translators: + - ["Pablo Elices", "http://github.com/pabloelices"] +filename: coffeescript-es.coffee +--- + +``` coffeescript +# CoffeeScript es un lenguaje hipster. +# Tiene convenciones de muchos lenguajes modernos. +# Los comentarios son como en Ruby y Python, usan almohadilla. + +### +Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/' para el código JavaScript resultante. + +Deberías entender la mayor parte de la semántica de JavaScript antes de continuar. +### + +# Asignación: +number = 42 #=> var number = 42; +opposite = true #=> var opposite = true; + +# Condiciones: +number = -42 if opposite #=> if(opposite) { number = -42; } + +# Funciones: +square = (x) -> x * x #=> var square = function(x) { return x * x; } + +# Rangos: +list = [1..5] #=> var list = [1, 2, 3, 4, 5]; + +# Objetos: +math = + root: Math.sqrt + square: square + cube: (x) -> x * square x +#=> var math = { +# "root": Math.sqrt, +# "square": square, +# "cube": function(x) { return x * square(x); } +#} + +# Símbolos: +race = (winner, runners...) -> + print winner, runners + +# Existencia: +alert "I knew it!" if elvis? +#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); } + +# Colecciones por comprensión: +cubes = (math.cube num for num in list) #=> ... +``` -- cgit v1.2.3 From f29098292c71a897efbcdbe702146831e5ca4831 Mon Sep 17 00:00:00 2001 From: Pablo Elices Date: Wed, 14 Aug 2013 13:13:56 +0200 Subject: Fix typo. --- es-es/coffeescript-es.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/es-es/coffeescript-es.html.markdown b/es-es/coffeescript-es.html.markdown index eb6825c0..a58c0d07 100644 --- a/es-es/coffeescript-es.html.markdown +++ b/es-es/coffeescript-es.html.markdown @@ -11,7 +11,7 @@ filename: coffeescript-es.coffee ``` coffeescript # CoffeeScript es un lenguaje hipster. # Tiene convenciones de muchos lenguajes modernos. -# Los comentarios son como en Ruby y Python, usan almohadilla. +# Los comentarios son como en Ruby y Python, usan almohadillas. ### Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/' para el código JavaScript resultante. -- cgit v1.2.3 From 89821fd235560bdfddd61fb4ac52aafc4772ee14 Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Wed, 14 Aug 2013 11:56:14 -0300 Subject: =?UTF-8?q?revis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pt-br/ruby-pt.html.markdown | 79 +----- pt-br/ruby-pt.html.markdown~ | 598 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 607 insertions(+), 70 deletions(-) create mode 100644 pt-br/ruby-pt.html.markdown~ diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown index cedd2db1..732d06a4 100644 --- a/pt-br/ruby-pt.html.markdown +++ b/pt-br/ruby-pt.html.markdown @@ -9,19 +9,13 @@ contributors: # Isso é um comentario =begin -This is a multiline comment -No-one uses them -You shouldn't either - -Isso é um comentario multilinha +Isso é um comentário multilinha Ninguém os usa - +Você não deve usar também =end -# First and foremost: Everything is an object. # Primeiro e principal: Tudo é um objeto. -# Numbers are objects # Números são objetos 3.class #=> Fixnum @@ -29,8 +23,7 @@ Ninguém os usa 3.to_s #=> "3" -# Some basic arithmetic -# Aritmética básica +# Um pouco de aritmética básica 1 + 1 #=> 2 8 - 1 #=> 7 @@ -44,7 +37,6 @@ Ninguém os usa 1.+(3) #=> 4 10.* 5 #=> 50 -# Special values are objects # Valores especiais são obejetos nil # Nothing to see here nil # Nada para ver aqui @@ -57,26 +49,22 @@ nil.class #=> NilClass true.class #=> TrueClass false.class #=> FalseClass -# Equality # Igualdade 1 == 1 #=> true 2 == 1 #=> false -# Inequality # Desigualdade 1 != 1 #=> false 2 != 1 #=> true !true #=> false !false #=> true -# apart from false itself, nil is the only other 'falsey' value # além de 'false', 'nil' é o único outro valor falso !nil #=> true !false #=> true !0 #=> false -# More comparisons # Mais comparações 1 < 10 #=> true 1 > 10 #=> false @@ -84,7 +72,6 @@ false.class #=> FalseClass 2 >= 2 #=> true # Strings are objects -# Strings são obejetos 'I am a string'.class #=> String 'Eu sou uma string'.class #=> String @@ -95,47 +82,35 @@ placeholder = "use string interpolation" placeholder = "usar interpolação de string" "I can #{placeholder} when using double quoted strings" "Eu posso #{placeholder} quando estiver usando aspas duplas" -#=> "I can use string interpolation when using double quoted strings" #=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas" - -# print to the output -# imprime para output (saida) +# imprime para output (saída) puts "I'm printing!" puts "Estou imprimindo" -# Variables # Variáveis x = 25 #=> 25 x #=> 25 -# Note that assignment returns the value assigned # Note que uma atribuição retorna o valor atribuido -# This means you can do multiple assignment: -# Isso significa que você pode fazer multiplas atribuições: +# Isso significa que você pode fazer múltiplas atribuições: x = y = 10 #=> 10 x #=> 10 y #=> 10 -# By convention, use snake_case for variable names # Por convenção, use snake_case para nomes de variáveis snake_case = true -# Use descriptive variable names # Use nomes de variáveis descrivos path_to_project_root = '/good/name/' caminho_para_a_raiz_do_projeto = '/bom/nome/' path = '/bad/name/' caminho = '/nome/ruim/' -# Symbols (are objects) # Simbolos (são objetos) -# Symbols are immutable, reusable constants represented internally by an # Simbolos são imultáveis, são constantes reutilizáveis representadadas internamente por um -# integer value. They're often used instead of strings to efficiently convey # valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores -# specific, meaningful values # específicos e significativos :pending.class #=> Symbol @@ -155,68 +130,52 @@ status == :aprovado #=> false # Arrays -# This is an array # Isso é um array [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] -# Arrays can contain different types of items # Arrays podem conter diferentes tipos de itens array = [1, "hello", false] #=> => [1, "hello", false] array = [1, "Oi", false] #=> => [1, "Oi", false] -# Arrays can be indexed # Arrays podem ser indexados -# From the front # a partir do começo array[0] #=> 1 array[12] #=> nil -# Like arithmetic, [var] access # Como aritimetica, o acesso via [var] -# is just syntactic sugar # é apenas açúcar sintático -# for calling a method [] on an object # para chamar o método [] de um objeto array.[] 0 #=> 1 array.[] 12 #=> nil -# From the end # a partir do final array[-1] #=> 5 -# With a start and end index # Com um índice de começo e fim array[2, 4] #=> [3, 4, 5] -# Or with a range # Ou com um intervalo de valores array[1..3] #=> [2, 3, 4] -# Add to an array like this # Adicionar a um array como este array << 6 #=> [1, 2, 3, 4, 5, 6] -# Hashes are Ruby's primary dictionary with keys/value pairs. -# Hashes são dicionário com um par de chave(key)/valor(value) -# Hashes are denoted with curly braces: +# Hashes são o principal dicionário de Ruby com pares de chaves(keys)/valor(value). # Hashes são simbolizados com chaves "{}" hash = {'color' => 'green', 'number' => 5} hash = {'cor' => 'verde', 'numero' => 5} hash.keys #=> ['cor', 'numero'] -# Hashes can be quickly looked up by key: # Hashes podem ser rapidamente pesquisado pela chave (key) hash['cor'] #=> 'verde' hash['numero'] #=> 5 -# Asking a hash for a key that doesn't exist returns nil: # Procurar em um hash por uma chave que não existe retorna nil: hash['nothing here'] #=> nil hash['nada aqui'] #=> nil -# Iterate over hashes with the #each method: # Interar sobre hashes com o método #each: hash.each do |k, v| puts "#{k} is #{v}" @@ -226,7 +185,6 @@ hash.each do |k, v| puts "#{k} é #{v}" end -# Since Ruby 1.9, there's a special syntax when using symbols as keys: # Desde o Ruby 1.9, temos uma sintaxe especial quando usamos simbolos como chaves (keys) new_hash = { defcon: 3, action: true} @@ -235,12 +193,9 @@ novo_hash = { defcon: 3, acao: true} new_hash.keys #=> [:defcon, :action] novo_hash.keys #=> [:defcon, :acao] -# Tip: Both Arrays and Hashes are Enumerable # Dica: Tanto Arrays quanto Hashes são Enumerable -# They share a lot of useful methods such as each, map, count, and more # Eles compartilham um monte de métodos úteis como each, map, count e mais -# Control structures # Estruturas de controle if true @@ -272,11 +227,8 @@ end #=> contador 4 #=> contador 5 -# HOWEVER # PORÉM -# No-one uses for loops # Ninguém usa para loops -# Use `each` instead, like this: # Use "each" em vez, dessa forma: (1..5).each do |counter| @@ -343,19 +295,16 @@ else puts "Alternative grading system, eh?" end -# Functions # Funções def dobrar(x) x * 2 end -# Functions (and all blocks) implcitly return the value of the last statement # Funções (e todos os blocos) retornam implicitamente o valor da última linha double(2) #=> 4 dobrar(2) #=> 4 -# Parentheses are optional where the result is unambiguous # Parênteses são opicionais onde o resultado é claro double 3 #=> 6 dobrar 3 #=> 6 @@ -371,7 +320,6 @@ def somar(x,y) x + y end -# Method arguments are separated by a comma # Argumentos de métodos são separados por uma virgula sum 3, 4 #=> 7 somar 3, 4 #=> 7 @@ -379,9 +327,7 @@ somar 3, 4 #=> 7 somar somar(3,4), 5 #=> 12 # yield -# All methods have an implicit, optional block parameter # Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco -# it can be called with the 'yield' keyword # ele pode ser chamado com a palavra chave 'yield' def surround @@ -406,7 +352,6 @@ ao_redor { puts 'Olá mundo' } # } -# Define a class with the class keyword # Define uma classe com a palavra chave 'class' class Human @@ -480,7 +425,6 @@ class Humano end -# Instantiate a class # Instaciando uma classe jim = Human.new("Jim Halpert") jim = Humano.new("Jim Halpert") @@ -488,7 +432,6 @@ jim = Humano.new("Jim Halpert") dwight = Human.new("Dwight K. Schrute") dwight = Humano.new("Dwight K. Schrute") -# Let's call a couple of methods # Vamos chamar um par de métodos jim.species #=> "H. sapiens" jim.especies #=> "H. sapiens" @@ -508,15 +451,12 @@ dwight.especies #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" dwight.nome #=> "Dwight K. Schrute" -# Call the class method # Chamar o método de classe Human.say("Hi") #=> "Hi" Humano.diz("Oi") #=> "Oi" -# Class also is object in ruby. So class can have instance variables. -# Uma classe também é objeto em Ruby. Então uma classe pode possuir um variavel de instancia -# Class variable is shared among the class and all of its descendants. -# Variavies de classe são compartilhadas entre a classe e todos os seus descendentes. +# Uma classe também é objeto em Ruby. Então uma classe pode possuir variável de instância +# Variáveis de classe são compartilhadas entre a classe e todos os seus descendentes. # base class class Human @@ -559,8 +499,7 @@ Humano.foo = 2 # 2 Worker.foo # 2 Trabalhador.foo # 2 -# Class instance variable is not shared by the class's descendants. -# Uma variavel de instancia não é compartilhada por suas classes decendentes. +# Uma variável de instância não é compartilhada por suas classes decendentes. class Human @bar = 0 diff --git a/pt-br/ruby-pt.html.markdown~ b/pt-br/ruby-pt.html.markdown~ new file mode 100644 index 00000000..cedd2db1 --- /dev/null +++ b/pt-br/ruby-pt.html.markdown~ @@ -0,0 +1,598 @@ +--- +language: ruby +filename: learnruby.rb +contributors: + - ["Bruno Henrique - Garu", "http://garulab.com"] +--- + +```ruby +# Isso é um comentario + +=begin +This is a multiline comment +No-one uses them +You shouldn't either + +Isso é um comentario multilinha +Ninguém os usa + +=end + +# First and foremost: Everything is an object. +# Primeiro e principal: Tudo é um objeto. + +# Numbers are objects +# Números são objetos + +3.class #=> Fixnum + +3.to_s #=> "3" + + +# Some basic arithmetic +# Aritmética básica + +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Arithmetic is just syntactic sugar +# for calling a method on an object +# Arithmetic é apenas açúcar semântico +# para chamar um métoddo de um objeto +1.+(3) #=> 4 +10.* 5 #=> 50 + +# Special values are objects +# Valores especiais são obejetos +nil # Nothing to see here +nil # Nada para ver aqui +true # truth +true # verdadeiro +false # falsehood +false # falso + +nil.class #=> NilClass +true.class #=> TrueClass +false.class #=> FalseClass + +# Equality +# Igualdade +1 == 1 #=> true +2 == 1 #=> false + +# Inequality +# Desigualdade +1 != 1 #=> false +2 != 1 #=> true +!true #=> false +!false #=> true + +# apart from false itself, nil is the only other 'falsey' value +# além de 'false', 'nil' é o único outro valor falso + +!nil #=> true +!false #=> true +!0 #=> false + +# More comparisons +# Mais comparações +1 < 10 #=> true +1 > 10 #=> false +2 <= 2 #=> true +2 >= 2 #=> true + +# Strings are objects +# Strings são obejetos + +'I am a string'.class #=> String +'Eu sou uma string'.class #=> String +"I am a string too".class #=> String +"Eu também sou uma string".class #=> String + +placeholder = "use string interpolation" +placeholder = "usar interpolação de string" +"I can #{placeholder} when using double quoted strings" +"Eu posso #{placeholder} quando estiver usando aspas duplas" +#=> "I can use string interpolation when using double quoted strings" +#=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas" + + +# print to the output +# imprime para output (saida) +puts "I'm printing!" +puts "Estou imprimindo" + +# Variables +# Variáveis +x = 25 #=> 25 +x #=> 25 + +# Note that assignment returns the value assigned +# Note que uma atribuição retorna o valor atribuido +# This means you can do multiple assignment: +# Isso significa que você pode fazer multiplas atribuições: + +x = y = 10 #=> 10 +x #=> 10 +y #=> 10 + +# By convention, use snake_case for variable names +# Por convenção, use snake_case para nomes de variáveis +snake_case = true + +# Use descriptive variable names +# Use nomes de variáveis descrivos +path_to_project_root = '/good/name/' +caminho_para_a_raiz_do_projeto = '/bom/nome/' +path = '/bad/name/' +caminho = '/nome/ruim/' + +# Symbols (are objects) +# Simbolos (são objetos) +# Symbols are immutable, reusable constants represented internally by an +# Simbolos são imultáveis, são constantes reutilizáveis representadadas internamente por um +# integer value. They're often used instead of strings to efficiently convey +# valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores +# specific, meaningful values +# específicos e significativos + +:pending.class #=> Symbol +:pendente.class #=> Symbol + +status = :pending +status = :pendente + +status == :pending #=> true +status == :pendente #=> true + +status == 'pending' #=> false +status == 'pendente' #=> false + +status == :approved #=> false +status == :aprovado #=> false + +# Arrays + +# This is an array +# Isso é um array +[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] + +# Arrays can contain different types of items +# Arrays podem conter diferentes tipos de itens + +array = [1, "hello", false] #=> => [1, "hello", false] +array = [1, "Oi", false] #=> => [1, "Oi", false] + +# Arrays can be indexed +# Arrays podem ser indexados +# From the front +# a partir do começo +array[0] #=> 1 +array[12] #=> nil + +# Like arithmetic, [var] access +# Como aritimetica, o acesso via [var] +# is just syntactic sugar +# é apenas açúcar sintático +# for calling a method [] on an object +# para chamar o método [] de um objeto +array.[] 0 #=> 1 +array.[] 12 #=> nil + +# From the end +# a partir do final +array[-1] #=> 5 + +# With a start and end index +# Com um índice de começo e fim +array[2, 4] #=> [3, 4, 5] + +# Or with a range +# Ou com um intervalo de valores +array[1..3] #=> [2, 3, 4] + +# Add to an array like this +# Adicionar a um array como este +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# Hashes are Ruby's primary dictionary with keys/value pairs. +# Hashes são dicionário com um par de chave(key)/valor(value) +# Hashes are denoted with curly braces: +# Hashes são simbolizados com chaves "{}" +hash = {'color' => 'green', 'number' => 5} +hash = {'cor' => 'verde', 'numero' => 5} + +hash.keys #=> ['cor', 'numero'] + +# Hashes can be quickly looked up by key: +# Hashes podem ser rapidamente pesquisado pela chave (key) +hash['cor'] #=> 'verde' +hash['numero'] #=> 5 + +# Asking a hash for a key that doesn't exist returns nil: +# Procurar em um hash por uma chave que não existe retorna nil: +hash['nothing here'] #=> nil +hash['nada aqui'] #=> nil + +# Iterate over hashes with the #each method: +# Interar sobre hashes com o método #each: +hash.each do |k, v| + puts "#{k} is #{v}" +end + +hash.each do |k, v| + puts "#{k} é #{v}" +end + +# Since Ruby 1.9, there's a special syntax when using symbols as keys: +# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos simbolos como chaves (keys) + +new_hash = { defcon: 3, action: true} +novo_hash = { defcon: 3, acao: true} + +new_hash.keys #=> [:defcon, :action] +novo_hash.keys #=> [:defcon, :acao] + +# Tip: Both Arrays and Hashes are Enumerable +# Dica: Tanto Arrays quanto Hashes são Enumerable +# They share a lot of useful methods such as each, map, count, and more +# Eles compartilham um monte de métodos úteis como each, map, count e mais + +# Control structures +# Estruturas de controle + +if true + "if statement" +elsif false + "else if, optional" +else + "else, also optional" +end + +if true + "Se verdadeiro" +elsif false + "else if, opicional" +else + "else, também é opicional" +end + +for counter in 1..5 + puts "iteration #{counter}" +end + +for contador in 1..5 + puts "interação #{contador}" +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +# HOWEVER +# PORÉM +# No-one uses for loops +# Ninguém usa para loops +# Use `each` instead, like this: +# Use "each" em vez, dessa forma: + +(1..5).each do |counter| + puts "iteration #{counter}" +end + +(1..5).each do |contador| + puts "interação #{contador}" +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +counter = 1 +while counter <= 5 do + puts "iteration #{counter}" + counter += 1 +end + +contador = 1 +while contador <= 5 do + puts "interação #{contador}" + contador += 1 +end +#=> contador 1 +#=> contador 2 +#=> contador 3 +#=> contador 4 +#=> contador 5 + +grade = 'B' + +case grade +when 'A' + puts "Way to go kiddo" +when 'B' + puts "Better luck next time" +when 'C' + puts "You can do better" +when 'D' + puts "Scraping through" +when 'F' + puts "You failed!" +else + puts "Alternative grading system, eh?" +end + +grau = 'B' + +case grau +when 'A' + puts "Um longo caminho a percorrer pequeno gafanhoto" +when 'B' + puts "Melhor sorte da próxima vez" +when 'C' + puts "Você pode fazer melhor" +when 'D' + puts "Scraping through" +when 'F' + puts "Você falhou" +else + puts "Alternative grading system, eh?" +end + +# Functions +# Funções + +def dobrar(x) + x * 2 +end + +# Functions (and all blocks) implcitly return the value of the last statement +# Funções (e todos os blocos) retornam implicitamente o valor da última linha +double(2) #=> 4 +dobrar(2) #=> 4 + +# Parentheses are optional where the result is unambiguous +# Parênteses são opicionais onde o resultado é claro +double 3 #=> 6 +dobrar 3 #=> 6 + +double double 3 #=> 12 +dobrar dobrar 3 #=> 12 + +def sum(x,y) + x + y +end + +def somar(x,y) + x + y +end + +# Method arguments are separated by a comma +# Argumentos de métodos são separados por uma virgula +sum 3, 4 #=> 7 +somar 3, 4 #=> 7 + +somar somar(3,4), 5 #=> 12 + +# yield +# All methods have an implicit, optional block parameter +# Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco +# it can be called with the 'yield' keyword +# ele pode ser chamado com a palavra chave 'yield' + +def surround + puts "{" + yield + puts "}" +end + +surround { puts 'hello world' } + + +def ao_redor + puts "{" + yield + puts "}" +end + +ao_redor { puts 'Olá mundo' } + +# { +# Olá mundo +# } + + +# Define a class with the class keyword +# Define uma classe com a palavra chave 'class' +class Human + + # A class variable. It is shared by all instances of this class. + @@species = "H. sapiens" + + # Basic initializer + def initialize(name, age=0) + # Assign the argument to the "name" instance variable for the instance + @name = name + # If no age given, we will fall back to the default in the arguments list. + @age = age + end + + # Basic setter method + def name=(name) + @name = name + end + + # Basic getter method + def name + @name + end + + # A class method uses self to distinguish from instance methods. + # It can only be called on the class, not an instance. + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end + +end + + +class Humano + + # Uma variavel de classe. Ela é compartilhada por todas as instancias dessa classe + @@especies = "H. sapiens" + + # Inicialização básica (contructor) + def initialize(nome, idade=0) + # Atribui o argumento para a variavel de instacia "nome" do objeto + @nome = nome + # Se a idade não for passada, nós definimos um valor padrão na lista de argumentos + @idade = idade + end + + # Método básico para atribuir valor + def nome=(nome) + @nome = nome + end + + # Método básico de resgatar valor + def nome + @nome + end + + # Um método de classe usa a palavra chave self para se defenciar dos métodos de instancia. + # Ele só pode ser chamado na classe, não na instancia + def self.diz(msg) + puts "#{msg}" + end + + def especies + @@especies + end + +end + + +# Instantiate a class +# Instaciando uma classe +jim = Human.new("Jim Halpert") +jim = Humano.new("Jim Halpert") + +dwight = Human.new("Dwight K. Schrute") +dwight = Humano.new("Dwight K. Schrute") + +# Let's call a couple of methods +# Vamos chamar um par de métodos +jim.species #=> "H. sapiens" +jim.especies #=> "H. sapiens" + +jim.name #=> "Jim Halpert" +jim.nome #=> "Jim Halpert" + +jim.name = "Jim Halpert II" #=> "Jim Halpert II" +jim.nome = "Jim Halpert II" #=> "Jim Halpert II" + +jim.name #=> "Jim Halpert II" +jim.nome #=> "Jim Halpert II" + +dwight.species #=> "H. sapiens" +dwight.especies #=> "H. sapiens" + +dwight.name #=> "Dwight K. Schrute" +dwight.nome #=> "Dwight K. Schrute" + +# Call the class method +# Chamar o método de classe +Human.say("Hi") #=> "Hi" +Humano.diz("Oi") #=> "Oi" + +# Class also is object in ruby. So class can have instance variables. +# Uma classe também é objeto em Ruby. Então uma classe pode possuir um variavel de instancia +# Class variable is shared among the class and all of its descendants. +# Variavies de classe são compartilhadas entre a classe e todos os seus descendentes. + +# base class +class Human + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + + +# Classe base +class Humano + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + +# classe filha +class Trabalhador < Humano +end + +Human.foo # 0 +Humano.foo # 0 +Worker.foo # 0 +Trabalhador.foo # 0 + +Human.foo = 2 # 2 +Humano.foo = 2 # 2 +Worker.foo # 2 +Trabalhador.foo # 2 + +# Class instance variable is not shared by the class's descendants. +# Uma variavel de instancia não é compartilhada por suas classes decendentes. + +class Human + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Humano + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Doctor < Human +end + +class Doutor < Humano +end + +Humano.bar # 0 +Doutor.bar # nil + +``` -- cgit v1.2.3 From 38c3c4d6a8571a4d48a46925f4feb717d7e9f164 Mon Sep 17 00:00:00 2001 From: Peter Mitchell Date: Wed, 14 Aug 2013 10:58:19 -0400 Subject: Update clojure.html.markdown Small typo fix --- clojure.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure.html.markdown b/clojure.html.markdown index 6baae0ce..a502a95c 100644 --- a/clojure.html.markdown +++ b/clojure.html.markdown @@ -365,7 +365,7 @@ my-atom ;=> Atom<#...> (Returns the Atom object) ### Further Reading -This is far from exhaustive, but hopefully it's enought o get you on your feet. +This is far from exhaustive, but hopefully it's enough to get you on your feet. Clojure.org has lots of articles: [http://clojure.org/](http://clojure.org/) -- cgit v1.2.3 From a2432ef31727c56fcfb2528523804da6ad21c4a0 Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Wed, 14 Aug 2013 12:01:30 -0300 Subject: =?UTF-8?q?revis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pt-br/ruby-pt.html.markdown~ | 598 ------------------------------------------- 1 file changed, 598 deletions(-) delete mode 100644 pt-br/ruby-pt.html.markdown~ diff --git a/pt-br/ruby-pt.html.markdown~ b/pt-br/ruby-pt.html.markdown~ deleted file mode 100644 index cedd2db1..00000000 --- a/pt-br/ruby-pt.html.markdown~ +++ /dev/null @@ -1,598 +0,0 @@ ---- -language: ruby -filename: learnruby.rb -contributors: - - ["Bruno Henrique - Garu", "http://garulab.com"] ---- - -```ruby -# Isso é um comentario - -=begin -This is a multiline comment -No-one uses them -You shouldn't either - -Isso é um comentario multilinha -Ninguém os usa - -=end - -# First and foremost: Everything is an object. -# Primeiro e principal: Tudo é um objeto. - -# Numbers are objects -# Números são objetos - -3.class #=> Fixnum - -3.to_s #=> "3" - - -# Some basic arithmetic -# Aritmética básica - -1 + 1 #=> 2 -8 - 1 #=> 7 -10 * 2 #=> 20 -35 / 5 #=> 7 - -# Arithmetic is just syntactic sugar -# for calling a method on an object -# Arithmetic é apenas açúcar semântico -# para chamar um métoddo de um objeto -1.+(3) #=> 4 -10.* 5 #=> 50 - -# Special values are objects -# Valores especiais são obejetos -nil # Nothing to see here -nil # Nada para ver aqui -true # truth -true # verdadeiro -false # falsehood -false # falso - -nil.class #=> NilClass -true.class #=> TrueClass -false.class #=> FalseClass - -# Equality -# Igualdade -1 == 1 #=> true -2 == 1 #=> false - -# Inequality -# Desigualdade -1 != 1 #=> false -2 != 1 #=> true -!true #=> false -!false #=> true - -# apart from false itself, nil is the only other 'falsey' value -# além de 'false', 'nil' é o único outro valor falso - -!nil #=> true -!false #=> true -!0 #=> false - -# More comparisons -# Mais comparações -1 < 10 #=> true -1 > 10 #=> false -2 <= 2 #=> true -2 >= 2 #=> true - -# Strings are objects -# Strings são obejetos - -'I am a string'.class #=> String -'Eu sou uma string'.class #=> String -"I am a string too".class #=> String -"Eu também sou uma string".class #=> String - -placeholder = "use string interpolation" -placeholder = "usar interpolação de string" -"I can #{placeholder} when using double quoted strings" -"Eu posso #{placeholder} quando estiver usando aspas duplas" -#=> "I can use string interpolation when using double quoted strings" -#=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas" - - -# print to the output -# imprime para output (saida) -puts "I'm printing!" -puts "Estou imprimindo" - -# Variables -# Variáveis -x = 25 #=> 25 -x #=> 25 - -# Note that assignment returns the value assigned -# Note que uma atribuição retorna o valor atribuido -# This means you can do multiple assignment: -# Isso significa que você pode fazer multiplas atribuições: - -x = y = 10 #=> 10 -x #=> 10 -y #=> 10 - -# By convention, use snake_case for variable names -# Por convenção, use snake_case para nomes de variáveis -snake_case = true - -# Use descriptive variable names -# Use nomes de variáveis descrivos -path_to_project_root = '/good/name/' -caminho_para_a_raiz_do_projeto = '/bom/nome/' -path = '/bad/name/' -caminho = '/nome/ruim/' - -# Symbols (are objects) -# Simbolos (são objetos) -# Symbols are immutable, reusable constants represented internally by an -# Simbolos são imultáveis, são constantes reutilizáveis representadadas internamente por um -# integer value. They're often used instead of strings to efficiently convey -# valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores -# specific, meaningful values -# específicos e significativos - -:pending.class #=> Symbol -:pendente.class #=> Symbol - -status = :pending -status = :pendente - -status == :pending #=> true -status == :pendente #=> true - -status == 'pending' #=> false -status == 'pendente' #=> false - -status == :approved #=> false -status == :aprovado #=> false - -# Arrays - -# This is an array -# Isso é um array -[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] - -# Arrays can contain different types of items -# Arrays podem conter diferentes tipos de itens - -array = [1, "hello", false] #=> => [1, "hello", false] -array = [1, "Oi", false] #=> => [1, "Oi", false] - -# Arrays can be indexed -# Arrays podem ser indexados -# From the front -# a partir do começo -array[0] #=> 1 -array[12] #=> nil - -# Like arithmetic, [var] access -# Como aritimetica, o acesso via [var] -# is just syntactic sugar -# é apenas açúcar sintático -# for calling a method [] on an object -# para chamar o método [] de um objeto -array.[] 0 #=> 1 -array.[] 12 #=> nil - -# From the end -# a partir do final -array[-1] #=> 5 - -# With a start and end index -# Com um índice de começo e fim -array[2, 4] #=> [3, 4, 5] - -# Or with a range -# Ou com um intervalo de valores -array[1..3] #=> [2, 3, 4] - -# Add to an array like this -# Adicionar a um array como este -array << 6 #=> [1, 2, 3, 4, 5, 6] - -# Hashes are Ruby's primary dictionary with keys/value pairs. -# Hashes são dicionário com um par de chave(key)/valor(value) -# Hashes are denoted with curly braces: -# Hashes são simbolizados com chaves "{}" -hash = {'color' => 'green', 'number' => 5} -hash = {'cor' => 'verde', 'numero' => 5} - -hash.keys #=> ['cor', 'numero'] - -# Hashes can be quickly looked up by key: -# Hashes podem ser rapidamente pesquisado pela chave (key) -hash['cor'] #=> 'verde' -hash['numero'] #=> 5 - -# Asking a hash for a key that doesn't exist returns nil: -# Procurar em um hash por uma chave que não existe retorna nil: -hash['nothing here'] #=> nil -hash['nada aqui'] #=> nil - -# Iterate over hashes with the #each method: -# Interar sobre hashes com o método #each: -hash.each do |k, v| - puts "#{k} is #{v}" -end - -hash.each do |k, v| - puts "#{k} é #{v}" -end - -# Since Ruby 1.9, there's a special syntax when using symbols as keys: -# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos simbolos como chaves (keys) - -new_hash = { defcon: 3, action: true} -novo_hash = { defcon: 3, acao: true} - -new_hash.keys #=> [:defcon, :action] -novo_hash.keys #=> [:defcon, :acao] - -# Tip: Both Arrays and Hashes are Enumerable -# Dica: Tanto Arrays quanto Hashes são Enumerable -# They share a lot of useful methods such as each, map, count, and more -# Eles compartilham um monte de métodos úteis como each, map, count e mais - -# Control structures -# Estruturas de controle - -if true - "if statement" -elsif false - "else if, optional" -else - "else, also optional" -end - -if true - "Se verdadeiro" -elsif false - "else if, opicional" -else - "else, também é opicional" -end - -for counter in 1..5 - puts "iteration #{counter}" -end - -for contador in 1..5 - puts "interação #{contador}" -end -#=> contador 1 -#=> contador 2 -#=> contador 3 -#=> contador 4 -#=> contador 5 - -# HOWEVER -# PORÉM -# No-one uses for loops -# Ninguém usa para loops -# Use `each` instead, like this: -# Use "each" em vez, dessa forma: - -(1..5).each do |counter| - puts "iteration #{counter}" -end - -(1..5).each do |contador| - puts "interação #{contador}" -end -#=> contador 1 -#=> contador 2 -#=> contador 3 -#=> contador 4 -#=> contador 5 - -counter = 1 -while counter <= 5 do - puts "iteration #{counter}" - counter += 1 -end - -contador = 1 -while contador <= 5 do - puts "interação #{contador}" - contador += 1 -end -#=> contador 1 -#=> contador 2 -#=> contador 3 -#=> contador 4 -#=> contador 5 - -grade = 'B' - -case grade -when 'A' - puts "Way to go kiddo" -when 'B' - puts "Better luck next time" -when 'C' - puts "You can do better" -when 'D' - puts "Scraping through" -when 'F' - puts "You failed!" -else - puts "Alternative grading system, eh?" -end - -grau = 'B' - -case grau -when 'A' - puts "Um longo caminho a percorrer pequeno gafanhoto" -when 'B' - puts "Melhor sorte da próxima vez" -when 'C' - puts "Você pode fazer melhor" -when 'D' - puts "Scraping through" -when 'F' - puts "Você falhou" -else - puts "Alternative grading system, eh?" -end - -# Functions -# Funções - -def dobrar(x) - x * 2 -end - -# Functions (and all blocks) implcitly return the value of the last statement -# Funções (e todos os blocos) retornam implicitamente o valor da última linha -double(2) #=> 4 -dobrar(2) #=> 4 - -# Parentheses are optional where the result is unambiguous -# Parênteses são opicionais onde o resultado é claro -double 3 #=> 6 -dobrar 3 #=> 6 - -double double 3 #=> 12 -dobrar dobrar 3 #=> 12 - -def sum(x,y) - x + y -end - -def somar(x,y) - x + y -end - -# Method arguments are separated by a comma -# Argumentos de métodos são separados por uma virgula -sum 3, 4 #=> 7 -somar 3, 4 #=> 7 - -somar somar(3,4), 5 #=> 12 - -# yield -# All methods have an implicit, optional block parameter -# Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco -# it can be called with the 'yield' keyword -# ele pode ser chamado com a palavra chave 'yield' - -def surround - puts "{" - yield - puts "}" -end - -surround { puts 'hello world' } - - -def ao_redor - puts "{" - yield - puts "}" -end - -ao_redor { puts 'Olá mundo' } - -# { -# Olá mundo -# } - - -# Define a class with the class keyword -# Define uma classe com a palavra chave 'class' -class Human - - # A class variable. It is shared by all instances of this class. - @@species = "H. sapiens" - - # Basic initializer - def initialize(name, age=0) - # Assign the argument to the "name" instance variable for the instance - @name = name - # If no age given, we will fall back to the default in the arguments list. - @age = age - end - - # Basic setter method - def name=(name) - @name = name - end - - # Basic getter method - def name - @name - end - - # A class method uses self to distinguish from instance methods. - # It can only be called on the class, not an instance. - def self.say(msg) - puts "#{msg}" - end - - def species - @@species - end - -end - - -class Humano - - # Uma variavel de classe. Ela é compartilhada por todas as instancias dessa classe - @@especies = "H. sapiens" - - # Inicialização básica (contructor) - def initialize(nome, idade=0) - # Atribui o argumento para a variavel de instacia "nome" do objeto - @nome = nome - # Se a idade não for passada, nós definimos um valor padrão na lista de argumentos - @idade = idade - end - - # Método básico para atribuir valor - def nome=(nome) - @nome = nome - end - - # Método básico de resgatar valor - def nome - @nome - end - - # Um método de classe usa a palavra chave self para se defenciar dos métodos de instancia. - # Ele só pode ser chamado na classe, não na instancia - def self.diz(msg) - puts "#{msg}" - end - - def especies - @@especies - end - -end - - -# Instantiate a class -# Instaciando uma classe -jim = Human.new("Jim Halpert") -jim = Humano.new("Jim Halpert") - -dwight = Human.new("Dwight K. Schrute") -dwight = Humano.new("Dwight K. Schrute") - -# Let's call a couple of methods -# Vamos chamar um par de métodos -jim.species #=> "H. sapiens" -jim.especies #=> "H. sapiens" - -jim.name #=> "Jim Halpert" -jim.nome #=> "Jim Halpert" - -jim.name = "Jim Halpert II" #=> "Jim Halpert II" -jim.nome = "Jim Halpert II" #=> "Jim Halpert II" - -jim.name #=> "Jim Halpert II" -jim.nome #=> "Jim Halpert II" - -dwight.species #=> "H. sapiens" -dwight.especies #=> "H. sapiens" - -dwight.name #=> "Dwight K. Schrute" -dwight.nome #=> "Dwight K. Schrute" - -# Call the class method -# Chamar o método de classe -Human.say("Hi") #=> "Hi" -Humano.diz("Oi") #=> "Oi" - -# Class also is object in ruby. So class can have instance variables. -# Uma classe também é objeto em Ruby. Então uma classe pode possuir um variavel de instancia -# Class variable is shared among the class and all of its descendants. -# Variavies de classe são compartilhadas entre a classe e todos os seus descendentes. - -# base class -class Human - @@foo = 0 - - def self.foo - @@foo - end - - def self.foo=(value) - @@foo = value - end -end - - -# Classe base -class Humano - @@foo = 0 - - def self.foo - @@foo - end - - def self.foo=(value) - @@foo = value - end -end - -# classe filha -class Trabalhador < Humano -end - -Human.foo # 0 -Humano.foo # 0 -Worker.foo # 0 -Trabalhador.foo # 0 - -Human.foo = 2 # 2 -Humano.foo = 2 # 2 -Worker.foo # 2 -Trabalhador.foo # 2 - -# Class instance variable is not shared by the class's descendants. -# Uma variavel de instancia não é compartilhada por suas classes decendentes. - -class Human - @bar = 0 - - def self.bar - @bar - end - - def self.bar=(value) - @bar = value - end -end - -class Humano - @bar = 0 - - def self.bar - @bar - end - - def self.bar=(value) - @bar = value - end -end - -class Doctor < Human -end - -class Doutor < Humano -end - -Humano.bar # 0 -Doutor.bar # nil - -``` -- cgit v1.2.3 From d9a27d3aa497dd1bb360c24566130fa3b154cb34 Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Wed, 14 Aug 2013 12:52:39 -0300 Subject: adding name and removing lines in English --- pt-br/ruby-pt.html.markdown | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown index 732d06a4..a3e60043 100644 --- a/pt-br/ruby-pt.html.markdown +++ b/pt-br/ruby-pt.html.markdown @@ -3,6 +3,7 @@ language: ruby filename: learnruby.rb contributors: - ["Bruno Henrique - Garu", "http://garulab.com"] + - ["Katyanna Moura"] --- ```ruby @@ -30,14 +31,12 @@ Você não deve usar também 10 * 2 #=> 20 35 / 5 #=> 7 -# Arithmetic is just syntactic sugar -# for calling a method on an object -# Arithmetic é apenas açúcar semântico -# para chamar um métoddo de um objeto +# Aritimética é apenas açúcar sintático +# para chamar um método de um objeto 1.+(3) #=> 4 10.* 5 #=> 50 -# Valores especiais são obejetos +# Valores especiais são objetos nil # Nothing to see here nil # Nada para ver aqui true # truth @@ -71,7 +70,7 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true -# Strings are objects +# Strings são objects 'I am a string'.class #=> String 'Eu sou uma string'.class #=> String @@ -102,14 +101,14 @@ y #=> 10 # Por convenção, use snake_case para nomes de variáveis snake_case = true -# Use nomes de variáveis descrivos +# Use nomes de variáveis descritivos path_to_project_root = '/good/name/' caminho_para_a_raiz_do_projeto = '/bom/nome/' path = '/bad/name/' caminho = '/nome/ruim/' -# Simbolos (são objetos) -# Simbolos são imultáveis, são constantes reutilizáveis representadadas internamente por um +# Símbolos (são objetos) +# Símbolos são imutáveis, são constantes reutilizáveis representadadas internamente por um # valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores # específicos e significativos @@ -143,7 +142,7 @@ array = [1, "Oi", false] #=> => [1, "Oi", false] array[0] #=> 1 array[12] #=> nil -# Como aritimetica, o acesso via [var] +# Como aritimética, o acesso via [var] # é apenas açúcar sintático # para chamar o método [] de um objeto array.[] 0 #=> 1 @@ -168,7 +167,7 @@ hash = {'cor' => 'verde', 'numero' => 5} hash.keys #=> ['cor', 'numero'] -# Hashes podem ser rapidamente pesquisado pela chave (key) +# Hashes podem ser rapidamente pesquisados pela chave (key) hash['cor'] #=> 'verde' hash['numero'] #=> 5 @@ -185,7 +184,7 @@ hash.each do |k, v| puts "#{k} é #{v}" end -# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos simbolos como chaves (keys) +# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos símbolos como chaves (keys) new_hash = { defcon: 3, action: true} novo_hash = { defcon: 3, acao: true} @@ -193,7 +192,7 @@ novo_hash = { defcon: 3, acao: true} new_hash.keys #=> [:defcon, :action] novo_hash.keys #=> [:defcon, :acao] -# Dica: Tanto Arrays quanto Hashes são Enumerable +# Dica: Tanto Arrays quanto Hashes são Enumerable. # Eles compartilham um monte de métodos úteis como each, map, count e mais # Estruturas de controle @@ -320,7 +319,7 @@ def somar(x,y) x + y end -# Argumentos de métodos são separados por uma virgula +# Argumentos de métodos são separados por uma vírgula sum 3, 4 #=> 7 somar 3, 4 #=> 7 @@ -425,7 +424,7 @@ class Humano end -# Instaciando uma classe +# Instanciando uma classe jim = Human.new("Jim Halpert") jim = Humano.new("Jim Halpert") -- cgit v1.2.3 From e938d244fcd4095a6dc8be117fdabb4d2370ca2f Mon Sep 17 00:00:00 2001 From: Pablo Elices Date: Wed, 14 Aug 2013 17:56:15 +0200 Subject: Split long line. --- es-es/coffeescript-es.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/es-es/coffeescript-es.html.markdown b/es-es/coffeescript-es.html.markdown index a58c0d07..78bb9be5 100644 --- a/es-es/coffeescript-es.html.markdown +++ b/es-es/coffeescript-es.html.markdown @@ -14,7 +14,8 @@ filename: coffeescript-es.coffee # Los comentarios son como en Ruby y Python, usan almohadillas. ### -Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/' para el código JavaScript resultante. +Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/' +para el código JavaScript resultante. Deberías entender la mayor parte de la semántica de JavaScript antes de continuar. ### -- cgit v1.2.3 From c1b08624fb7b4fa36cb55ad7fcefe98e073ab32c Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Wed, 14 Aug 2013 13:20:47 -0300 Subject: removing lines of code in english --- pt-br/ruby-pt.html.markdown | 162 ++------------------------------------------ 1 file changed, 5 insertions(+), 157 deletions(-) diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown index a3e60043..a6021b06 100644 --- a/pt-br/ruby-pt.html.markdown +++ b/pt-br/ruby-pt.html.markdown @@ -7,7 +7,7 @@ contributors: --- ```ruby -# Isso é um comentario +# Isso é um comentário =begin Isso é um comentário multilinha @@ -37,11 +37,8 @@ Você não deve usar também 10.* 5 #=> 50 # Valores especiais são objetos -nil # Nothing to see here nil # Nada para ver aqui -true # truth true # verdadeiro -false # falsehood false # falso nil.class #=> NilClass @@ -72,19 +69,14 @@ false.class #=> FalseClass # Strings são objects -'I am a string'.class #=> String 'Eu sou uma string'.class #=> String -"I am a string too".class #=> String "Eu também sou uma string".class #=> String -placeholder = "use string interpolation" placeholder = "usar interpolação de string" -"I can #{placeholder} when using double quoted strings" "Eu posso #{placeholder} quando estiver usando aspas duplas" #=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas" # imprime para output (saída) -puts "I'm printing!" puts "Estou imprimindo" # Variáveis @@ -102,9 +94,7 @@ y #=> 10 snake_case = true # Use nomes de variáveis descritivos -path_to_project_root = '/good/name/' caminho_para_a_raiz_do_projeto = '/bom/nome/' -path = '/bad/name/' caminho = '/nome/ruim/' # Símbolos (são objetos) @@ -112,19 +102,14 @@ caminho = '/nome/ruim/' # valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores # específicos e significativos -:pending.class #=> Symbol :pendente.class #=> Symbol -status = :pending status = :pendente -status == :pending #=> true status == :pendente #=> true -status == 'pending' #=> false status == 'pendente' #=> false -status == :approved #=> false status == :aprovado #=> false # Arrays @@ -134,7 +119,6 @@ status == :aprovado #=> false # Arrays podem conter diferentes tipos de itens -array = [1, "hello", false] #=> => [1, "hello", false] array = [1, "Oi", false] #=> => [1, "Oi", false] # Arrays podem ser indexados @@ -162,7 +146,6 @@ array << 6 #=> [1, 2, 3, 4, 5, 6] # Hashes são o principal dicionário de Ruby com pares de chaves(keys)/valor(value). # Hashes são simbolizados com chaves "{}" -hash = {'color' => 'green', 'number' => 5} hash = {'cor' => 'verde', 'numero' => 5} hash.keys #=> ['cor', 'numero'] @@ -172,7 +155,6 @@ hash['cor'] #=> 'verde' hash['numero'] #=> 5 # Procurar em um hash por uma chave que não existe retorna nil: -hash['nothing here'] #=> nil hash['nada aqui'] #=> nil # Interar sobre hashes com o método #each: @@ -186,10 +168,8 @@ end # Desde o Ruby 1.9, temos uma sintaxe especial quando usamos símbolos como chaves (keys) -new_hash = { defcon: 3, action: true} novo_hash = { defcon: 3, acao: true} -new_hash.keys #=> [:defcon, :action] novo_hash.keys #=> [:defcon, :acao] # Dica: Tanto Arrays quanto Hashes são Enumerable. @@ -197,14 +177,6 @@ novo_hash.keys #=> [:defcon, :acao] # Estruturas de controle -if true - "if statement" -elsif false - "else if, optional" -else - "else, also optional" -end - if true "Se verdadeiro" elsif false @@ -213,10 +185,6 @@ else "else, também é opicional" end -for counter in 1..5 - puts "iteration #{counter}" -end - for contador in 1..5 puts "interação #{contador}" end @@ -230,10 +198,6 @@ end # Ninguém usa para loops # Use "each" em vez, dessa forma: -(1..5).each do |counter| - puts "iteration #{counter}" -end - (1..5).each do |contador| puts "interação #{contador}" end @@ -243,12 +207,6 @@ end #=> contador 4 #=> contador 5 -counter = 1 -while counter <= 5 do - puts "iteration #{counter}" - counter += 1 -end - contador = 1 while contador <= 5 do puts "interação #{contador}" @@ -260,28 +218,11 @@ end #=> contador 4 #=> contador 5 -grade = 'B' - -case grade -when 'A' - puts "Way to go kiddo" -when 'B' - puts "Better luck next time" -when 'C' - puts "You can do better" -when 'D' - puts "Scraping through" -when 'F' - puts "You failed!" -else - puts "Alternative grading system, eh?" -end - grau = 'B' case grau when 'A' - puts "Um longo caminho a percorrer pequeno gafanhoto" + puts "Um longo caminho a percorrer, pequeno gafanhoto" when 'B' puts "Melhor sorte da próxima vez" when 'C' @@ -301,26 +242,18 @@ def dobrar(x) end # Funções (e todos os blocos) retornam implicitamente o valor da última linha -double(2) #=> 4 dobrar(2) #=> 4 # Parênteses são opicionais onde o resultado é claro -double 3 #=> 6 dobrar 3 #=> 6 -double double 3 #=> 12 dobrar dobrar 3 #=> 12 -def sum(x,y) - x + y -end - def somar(x,y) x + y end # Argumentos de métodos são separados por uma vírgula -sum 3, 4 #=> 7 somar 3, 4 #=> 7 somar somar(3,4), 5 #=> 12 @@ -329,15 +262,6 @@ somar somar(3,4), 5 #=> 12 # Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco # ele pode ser chamado com a palavra chave 'yield' -def surround - puts "{" - yield - puts "}" -end - -surround { puts 'hello world' } - - def ao_redor puts "{" yield @@ -352,50 +276,15 @@ ao_redor { puts 'Olá mundo' } # Define uma classe com a palavra chave 'class' -class Human - - # A class variable. It is shared by all instances of this class. - @@species = "H. sapiens" - - # Basic initializer - def initialize(name, age=0) - # Assign the argument to the "name" instance variable for the instance - @name = name - # If no age given, we will fall back to the default in the arguments list. - @age = age - end - - # Basic setter method - def name=(name) - @name = name - end - - # Basic getter method - def name - @name - end - - # A class method uses self to distinguish from instance methods. - # It can only be called on the class, not an instance. - def self.say(msg) - puts "#{msg}" - end - - def species - @@species - end - -end - class Humano - # Uma variavel de classe. Ela é compartilhada por todas as instancias dessa classe + # Uma variável de classe. Ela é compartilhada por todas as instâncias dessa classe @@especies = "H. sapiens" # Inicialização básica (contructor) def initialize(nome, idade=0) - # Atribui o argumento para a variavel de instacia "nome" do objeto + # Atribui o argumento para a variável de instancia "nome" do objeto @nome = nome # Se a idade não for passada, nós definimos um valor padrão na lista de argumentos @idade = idade @@ -411,7 +300,7 @@ class Humano @nome end - # Um método de classe usa a palavra chave self para se defenciar dos métodos de instancia. + # Um método de classe usa a palavra chave self para se defenciar dos métodos de instância. # Ele só pode ser chamado na classe, não na instancia def self.diz(msg) puts "#{msg}" @@ -425,51 +314,29 @@ end # Instanciando uma classe -jim = Human.new("Jim Halpert") jim = Humano.new("Jim Halpert") -dwight = Human.new("Dwight K. Schrute") dwight = Humano.new("Dwight K. Schrute") # Vamos chamar um par de métodos -jim.species #=> "H. sapiens" jim.especies #=> "H. sapiens" -jim.name #=> "Jim Halpert" jim.nome #=> "Jim Halpert" -jim.name = "Jim Halpert II" #=> "Jim Halpert II" jim.nome = "Jim Halpert II" #=> "Jim Halpert II" -jim.name #=> "Jim Halpert II" jim.nome #=> "Jim Halpert II" -dwight.species #=> "H. sapiens" dwight.especies #=> "H. sapiens" -dwight.name #=> "Dwight K. Schrute" dwight.nome #=> "Dwight K. Schrute" # Chamar o método de classe -Human.say("Hi") #=> "Hi" Humano.diz("Oi") #=> "Oi" # Uma classe também é objeto em Ruby. Então uma classe pode possuir variável de instância # Variáveis de classe são compartilhadas entre a classe e todos os seus descendentes. -# base class -class Human - @@foo = 0 - - def self.foo - @@foo - end - - def self.foo=(value) - @@foo = value - end -end - # Classe base class Humano @@ -488,30 +355,14 @@ end class Trabalhador < Humano end -Human.foo # 0 Humano.foo # 0 -Worker.foo # 0 Trabalhador.foo # 0 -Human.foo = 2 # 2 Humano.foo = 2 # 2 -Worker.foo # 2 Trabalhador.foo # 2 # Uma variável de instância não é compartilhada por suas classes decendentes. -class Human - @bar = 0 - - def self.bar - @bar - end - - def self.bar=(value) - @bar = value - end -end - class Humano @bar = 0 @@ -524,9 +375,6 @@ class Humano end end -class Doctor < Human -end - class Doutor < Humano end -- cgit v1.2.3 From c0d84572021330c404fb689241369878cff200e4 Mon Sep 17 00:00:00 2001 From: Bruno Henrique - Garu Date: Wed, 14 Aug 2013 13:34:10 -0300 Subject: Add link to contributor --- pt-br/ruby-pt.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown index a6021b06..8e8ce6a8 100644 --- a/pt-br/ruby-pt.html.markdown +++ b/pt-br/ruby-pt.html.markdown @@ -3,7 +3,7 @@ language: ruby filename: learnruby.rb contributors: - ["Bruno Henrique - Garu", "http://garulab.com"] - - ["Katyanna Moura"] + - ["Katyanna Moura", "https://twitter.com/amelie_kn"] --- ```ruby @@ -34,7 +34,7 @@ Você não deve usar também # Aritimética é apenas açúcar sintático # para chamar um método de um objeto 1.+(3) #=> 4 -10.* 5 #=> 50 +10.* 5 #=> 50 # Valores especiais são objetos nil # Nada para ver aqui @@ -135,7 +135,7 @@ array.[] 12 #=> nil # a partir do final array[-1] #=> 5 -# Com um índice de começo e fim +# Com um índice de começo e fim array[2, 4] #=> [3, 4, 5] # Ou com um intervalo de valores @@ -231,7 +231,7 @@ when 'D' puts "Scraping through" when 'F' puts "Você falhou" -else +else puts "Alternative grading system, eh?" end @@ -259,7 +259,7 @@ somar 3, 4 #=> 7 somar somar(3,4), 5 #=> 12 # yield -# Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco +# Todos os métodos possuem implicitamente um paramêntro opcional que é um bloco # ele pode ser chamado com a palavra chave 'yield' def ao_redor -- cgit v1.2.3 From 6062618d358839e22f4bbe6348658253ad2e0209 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Wed, 14 Aug 2013 19:11:13 +0200 Subject: Fix a missing character in the comment. remove useless whitespace at the end of the lines. --- ruby.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 19f2ec86..3a233d98 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -36,7 +36,7 @@ You shouldn't either # Arithmetic is just syntactic sugar # for calling a method on an object 1.+(3) #=> 4 -10.* 5 #=> 50 +10.* 5 #=> 50 # Special values are objects nil # Nothing to see here @@ -242,7 +242,7 @@ when 'D' puts "Scraping through" when 'F' puts "You failed!" -else +else puts "Alternative grading system, eh?" end @@ -252,7 +252,7 @@ def double(x) x * 2 end -# Functions (and all blocks) implcitly return the value of the last statement +# Functions (and all blocks) implicitly return the value of the last statement double(2) #=> 4 # Parentheses are optional where the result is unambiguous -- cgit v1.2.3 From ae73dd671f521527f6e58a877026c35d58173671 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Wed, 14 Aug 2013 19:12:21 +0200 Subject: Add the French translation of the Ruby language. --- fr-fr/ruby-fr.html.markdown | 406 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 406 insertions(+) create mode 100644 fr-fr/ruby-fr.html.markdown diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown new file mode 100644 index 00000000..f6bfe42a --- /dev/null +++ b/fr-fr/ruby-fr.html.markdown @@ -0,0 +1,406 @@ +--- +language: ruby +filename: learnruby-fr.rb +contributors: + - ["David Underwood", "http://theflyingdeveloper.com"] + - ["Joel Walden", "http://joelwalden.net"] + - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] +translators: + - ["Geoffrey Roguelon", "https://github.com/GRoguelon"] +lang: fr-fr +--- + +```ruby +# Ceci est un commentaire + +=begin +Ceci est un commentaire multiligne +Personne ne les utilise +Vous devriez en faire de même +=end + +# Tout d'abord : Tout est un objet. + +# Les nombres sont des objets + +3.class #=> Fixnum + +3.to_s #=> "3" + +# Arithmétique de base +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# L'arithmétique est juste un raccourci +# pour appeler la méthode d'un objet +1.+(3) #=> 4 +10.* 5 #=> 50 + +# Les valeurs spéciales sont des objets +nil # Nothing to see here +true # truth +false # falsehood + +nil.class #=> NilClass +true.class #=> TrueClass +false.class #=> FalseClass + +# Égalité +1 == 1 #=> true +2 == 1 #=> false + +# Inégalité +1 != 1 #=> false +2 != 1 #=> true +!true #=> false +!false #=> true + +# à part false lui-même, nil est la seule autre valeur 'false' + +!nil #=> true +!false #=> true +!0 #=> false + +# Plus de comparaisons +1 < 10 #=> true +1 > 10 #=> false +2 <= 2 #=> true +2 >= 2 #=> true + +# Les chaînes de caractères sont des objets + +'Je suis une chaîne de caractères'.class #=> String +"Je suis également une chaîne de caractères".class #=> String + +placeholder = "utiliser l'interpolation de chaîne de caractères" +"Je peux #{placeholder} quand j'utilise les guillemets" +#=> "Je peux utiliser l'interpolation de chaîne de caractères quand j'utilise les guillemets" + +# Afficher sur la sortie standard +puts "J'affiche à l'écran!" + +# Variables +x = 25 #=> 25 +x #=> 25 + +# Notez que l'affectation retourne la valeur affectée. +# Cela signifie que vous pouvez affecter plusieurs fois de suite : + +x = y = 10 #=> 10 +x #=> 10 +y #=> 10 + +# Par convention, utiliser snake_case (à base d'underscore) +# comme nom de variable +snake_case = true + +# Utiliser des noms de variable explicites +path_to_project_root = '/nom/correct/' +path = '/mauvais/nom/' + +# Symboles (sont des objets) +# Les symboles sont immuables, constants, +# réutilisables et représentés en interne +# par une valeur entière. Ils sont souvent +# utilisés à la place des chaînes de caractères +# pour transmettre efficacement des valeurs +# spécifiques ou significatives + +:pending.class #=> Symbol + +status = :pending + +status == :pending #=> true + +status == 'pending' #=> false + +status == :approved #=> false + +# Tableaux + +# Ceci est un tableau +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] + +# Les tableaux contiennent différents types d'élément. + +[1, "hello", false] #=> [1, "hello", false] + +# Les tableaux peuvent être indéxés +# Du début +array[0] #=> 1 +array[12] #=> nil + +# Comme l'arithmétique, la syntaxe [var] est juste un raccourci +# pour appeler la méthode [] d'un objet +array.[] 0 #=> 1 +array.[] 12 #=> nil + +# À la fin +array[-1] #=> 5 + +# Avec un index de début et de fin +array[2, 4] #=> [3, 4, 5] + +# Ou avec un interval +array[1..3] #=> [2, 3, 4] + +# Ajouter un élément au tableau comme ceci +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# Les Hashes sont des dictionnaires Ruby contenant des paires de clé/valeur. +# Les Hashes sont délimitées par des accolades : +hash = {'color' => 'green', 'number' => 5} + +hash.keys #=> ['color', 'number'] + +# Les Hashes peuvent retourner rapidement +# la valeur en utilisant la clé : +hash['color'] #=> 'green' +hash['number'] #=> 5 + +# Rechercher une clé inexistante dans une Hash retourne nil: +hash['nothing here'] #=> nil + +# Depuis Ruby 1.9, Une syntaxe spécifique est apparue en utilisant les symboles comme clés : + +new_hash = { defcon: 3, action: true} + +new_hash.keys #=> [:defcon, :action] + +# Astuce : Les tableaux et les Hashes sont dénombrables +# Ils partagent un certain nombre de méthodes pratiques +# telle que each, map, count, etc... + +# Structures de contrôle + +if true + "si instruction" +elsif false + "autrement si, facultatif" +else + "autrement, également facultatif" +end + +for compteur in 1..5 + puts "itération #{compteur}" +end +#=> itération 1 +#=> itération 2 +#=> itération 3 +#=> itération 4 +#=> itération 5 + +# CEPENDANT, Personne n'utilise la boucle for. +# À la place, vous devrez utiliser la méthode "each" +# et lui passer un bloc de code. +# Un bloc de code est un ensemble d'instructions que vous pouvez passer à une methode comme "each". +# Les blocs sont similaires aux lambdas, les fonctions anonymes ou les closures dans d'autres langages. +# +# La méthode "each" exécute le bloc de code pour chaque élément de l'interval d'éléments. +# Le bloc de code passe un paramètre compteur. +# Appeler la méthode "each" avec un bloc de code comme ceci : + +(1..5).each do |compteur| + puts "itération #{compteur}" +end +#=> itération 1 +#=> itération 2 +#=> itération 3 +#=> itération 4 +#=> itération 5 + +# Vous pouvez également mettre un bloc de code entre accolades : +(1..5).each {|compteur| puts "itération #{compteur}"} + +# Le contenu des structures de données peut être parcouru +# en utilisant la méthode each. +array.each do |element| + puts "#{element} est une partie du tableau" +end +hash.each do |cle, valeur| + puts "#{cle} est #{valeur}" +end + +compteur = 1 +while compteur <= 5 do + puts "itération #{compteur}" + compteur += 1 +end +#=> itération 1 +#=> itération 2 +#=> itération 3 +#=> itération 4 +#=> itération 5 + +grade = 'B' + +case grade +when 'A' + puts "Excellence" +when 'B' + puts "Plus de chance la première fois" +when 'C' + puts "Vous pouvez faire mieux" +when 'D' + puts "C'est pas terrible" +when 'F' + puts "Vous avez échoué!" +else + puts "Sytème de notation alternatif" +end + +# Fonctions + +def double(x) + x * 2 +end + +# Les fonctions (et tous les blocs de code) retournent +# implicitement la valeur de la dernière instruction évaluée +double(2) #=> 4 + +# Les paranthèses sont facultative +# où il n'y a pas d'ambiguïté sur le résultat +double 3 #=> 6 + +double double 3 #=> 12 + +def sum(x,y) + x + y +end + +# Les paramètres de méthode sont séparés par des virgules +sum 3, 4 #=> 7 + +sum sum(3,4), 5 #=> 12 + +# yield +# Toutes les méthodes ont un argument de type bloc de code +# facultatif et implicite +# il peut être appelé avec le mot clé 'yield' + +def surround + puts "{" + yield + puts "}" +end + +surround { puts 'Bonjour tout le monde' } + +# { +# Bonjour tout le monde +# } + + +# Définir une classe avec le mot clé 'class' +class Humain + + # Une variable de classe, + # elle est partagée par toutes les instances de cette classe. + @@espece = "H. sapiens" + + # Constructeur de classe + def initialize(nom, age = 0) + # Affecter l'argument à la variable d'instance 'nom' + # pour la durée de vie de l'instance de cette classe + @nom = nom + # Si le paramètre 'age' est absent, + # la valeur par défaut définie dans la liste des arguments sera utilisée. + @age = age + end + + # Une simple méthode setter + def nom=(nom) + @nom = nom + end + + # Une simple méthode getter + def nom + @nom + end + + # Une méthode de classe utilise le mot clé 'self' + # pour se distinguer de la méthode d'instance. + # La méthode sera alors accessible à partir de la classe + # et non pas de l'instance. + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end + +end + + +# Instancier une classe +jim = Humain.new("Jim Halpert") + +dwight = Humain.new("Dwight K. Schrute") + +# Appelons quelques méthodes +jim.espece #=> "H. sapiens" +jim.nom #=> "Jim Halpert" +jim.nom = "Jim Halpert II" #=> "Jim Halpert II" +jim.nom #=> "Jim Halpert II" +dwight.espece #=> "H. sapiens" +dwight.nom #=> "Dwight K. Schrute" + +# Appelons la méthode de classe +Humain.say("Hi") #=> "Hi" + +# Les classes sont également des objets en Ruby. +# Par conséquent, les classes peuvent avoir des variables d'instance. +# Les variables de classe sont partagées parmi +# la classe et ses descendants. + +# Classe de base +class Humain + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(valeur) + @@foo = valeur + end +end + +# Héritage de classe +class Travailleur < Humain +end + +Humain.foo # 0 +Travailleur.foo # 0 + +Humain.foo = 2 # 2 +Travailleur.foo # 2 + +# Les variables d'instance de classe ne sont pas partagées +# avec les classes héritées. + +class Humain + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(valeur) + @bar = valeur + end +end + +class Docteur < Humain +end + +Humain.bar # 0 +Docteur.bar # nil + +``` -- cgit v1.2.3 From 162b5bb60dea93dac591005a73956be663313761 Mon Sep 17 00:00:00 2001 From: Sergey Avseyev Date: Wed, 14 Aug 2013 23:15:17 +0300 Subject: Fix typo about how pointers are declared Of course it is completely valid that star can go right after type name, but it might be not that obvious that during declaration the star is associated to the variable, not with the type name. --- c.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index d243b19d..2b50efa0 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -230,10 +230,13 @@ printf("%p\n", &x); // Use & to retrieve the address of a variable // (%p formats a pointer) // => Prints some address in memory; -// Pointer types end with * in their declaration -int* px; // px is a pointer to an int + +// Pointers start with * in their declaration +int *px, not_a_pointer; // px is a pointer to an int px = &x; // Stores the address of x in px printf("%p\n", px); // => Prints some address in memory +printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer)); +// => Prints "8, 4" on 64-bit system // To retreive the value at the address a pointer is pointing to, // put * in front to de-reference it. -- cgit v1.2.3 From c4f86cbf70a6c8d3b03be87df32aeff88183da63 Mon Sep 17 00:00:00 2001 From: Vyacheslav Redkin Date: Thu, 15 Aug 2013 09:26:41 +0400 Subject: Add russian translate for php language --- ru-ru/php-ru.html.markdown | 658 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 658 insertions(+) create mode 100644 ru-ru/php-ru.html.markdown diff --git a/ru-ru/php-ru.html.markdown b/ru-ru/php-ru.html.markdown new file mode 100644 index 00000000..6c5720e3 --- /dev/null +++ b/ru-ru/php-ru.html.markdown @@ -0,0 +1,658 @@ +--- +language: php +contributors: + - ["Malcolm Fell", "http://emarref.net/"] + - ["Trismegiste", "https://github.com/Trismegiste"] + - ["SlaF", "https://github.com/SlaF"] +lang: ru-ru +filename: learnphp-ru.php +--- + +Этот документ описывает версию PHP 5 и выше. + +```php + + +// А так начинаются комментарии + +# Это тоже комментарий но // предпочтительнее + +/* + Окруженный /* и */ текст превращается + в многострочный комментарий +*/ + +// Используйте "echo" или "print" для вывода. +print('Hello '); // Напечатать "Hello " без перевода строки + +// () необязательно применять для print и echo +echo "World\n"; // Печатать "World" и перейти на новую строку. +// (все утверждения должны заканчиваться ;) + +// Любые символы за пределами закрывающегося тега выводятся автоматически: +?> +Hello World Again! + 12 +$int2 = -12; // => -12- +$int3 = 012; // => 10 (ведущий 0 обозначает восьмеричное число) +$int4 = 0x0F; // => 15 (ведущие символы 0x означает шестнадцатеричное число) + +// Дробные числа +$float = 1.234; +$float = 1.2e3; +$float = 7E-10; + +// Арифметика +$sum = 1 + 1; // 2 +$difference = 2 - 1; // 1 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 + +// Арифметические сокращения +$number = 0; +$number += 1; // Увеличивает $number на 1 +echo $number++; // Печатает 1 (инкрементируется после вывода) +echo ++$number; // Печатает 3 (инкрементируется до вывода) +$number /= $float; // Делится и результат присваивается $number + +// Строки должны быть заключены в одинарные кавычки; +$sgl_quotes = '$String'; // => '$String' + +// Избегайте двойных кавычек за исключением случаев интерполирования переменной +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' + +// Специальные (escape) символы работают только в двойных кавычках +$escaped = "This contains a \t tab character."; +$unescaped = 'This just contains a slash and a t: \t'; + +// Заключайте переменные в фигурные скобки если это необходимо +$money = "I have $${number} in the bank."; + +// Начиная с PHP 5.3, синтаксис nowdocs может использоваться для неинтерполированного многострочного текста +$nowdoc = <<<'END' +Multi line +string +END; + +// Heredocs поддерживает интерполяцию переменных +$heredoc = << 1, 'Two' => 2, 'Three' => 3); + +// В PHP 5.4 появился новый синтаксис +$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; + +echo $associative['One']; // печатает 1 + +// Список тоже содержит целочисленные ключи +$array = ['One', 'Two', 'Three']; +echo $array[0]; // => "One" + + +/******************************** + * Вывод + */ + +echo('Hello World!'); +// Печатает Hello World! на stdout. +// Stdout это веб-страница запущенная в браузере. + +print('Hello World!'); // Аналогично echo + +// echo - это конструкция языка, вы можете опустить скобки. +echo 'Hello World!'; +print 'Hello World!'; // Выводит Hello World! + +$paragraph = 'paragraph'; + +echo 100; // Печать скалярной переменной напрямую +echo $paragraph; // или печать переменной + +// Если короткие теги разрешены, или ваша версия PHP >= 5.4 +// вы можете использовать сокращенный синтаксис echo +?> +

+ 2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 + + +/******************************** + * Логические выражения + */ +$a = 0; +$b = '0'; +$c = '1'; +$d = '1'; + +// Утверждение (assert) выдает предупреждение если аргумент не true + +// Эти сравнения всегда будут истинными, даже если типы будут различаться +assert($a == $b); // "равно" +assert($c != $a); // "не равно" +assert($c <> $a); // другое обозначение "не равно" +assert($a < $c); // меньше +assert($c > $b); // больше +assert($a <= $b); // меньше или равно +assert($c >= $d); // больше или равно + +// Следующие утверждения истинны если переменные имеют одинаковый тип. +assert($c === $d); +assert($a !== $d); +assert(1 == '1'); +assert(1 !== '1'); + +// Переменные могут изменять тип, в зависимости от их использования. +$integer = 1; +echo $integer + $integer; // => 2 + +$string = '1'; +echo $string + $string; // => 2 (строка превращается в число) + +// Выводится 0 по той причине, что оператор + не может привести строку 'one' к числовому типу +$string = 'one'; +echo $string + $string; // => 0 + +// Приведение типов (type casting) может быть использовано для преобразование переменной в другой тип +$boolean = (boolean) 1; // => true + +$zero = 0; +$boolean = (boolean) $zero; // => false + +// Также существуют функции выполняющие приведение типов +$integer = 5; +$string = strval($integer); +$float = floatval($integer); + +$var = null; // Null + +// И похожая по действию функция +$integer = 10; +$boolen = settype($integer, "string") // теперь $integer имеет строковый тип + +// settype возвращает true - если преобразование удалось и false в противном случае + +/******************************** + * Управляющие структуры + */ + +if (true) { + print 'I get printed'; +} + +if (false) { + print 'I don\'t'; +} else { + print 'I get printed'; +} + +if (false) { + print 'Does not get printed'; +} elseif(true) { + print 'Does'; +} + +// Тернарный оператор +print (false ? 'Does not get printed' : 'Does'); + +$x = 0; +if ($x === '0') { + print 'Does not print'; +} elseif($x == '1') { + print 'Does not print'; +} else { + print 'Does print'; +} + +// Альтернативный синтаксис полезный для шаблонов +?> + + +This is displayed if the test is truthy. + +This is displayed otherwise. + + + 2, 'car' => 4]; + +// Циклы foreach могут обходить массивы +foreach ($wheels as $wheel_count) { + echo $wheel_count; +} // Prints "24" + +echo "\n"; + +// Вы можете обходить как ключи, так и их значения +foreach ($wheels as $vehicle => $wheel_count) { + echo "A $vehicle has $wheel_count wheels"; +} + +echo "\n"; + +$i = 0; +while ($i < 5) { + if ($i === 3) { + break; // Exit out of the while loop + } + echo $i++; +} // Prints "012" + +for ($i = 0; $i < 5; $i++) { + if ($i === 3) { + continue; // Skip this iteration of the loop + } + echo $i; +} // Prints "0124" + + +/******************************** + * Функции + */ + +// Определение функции: +function my_function () { + return 'Hello'; +} + +echo my_function(); // => "Hello" + +// Правильное имя функции начинается с буквы или символа подчеркивания +// и состоит из букв, цифр или знаков подчеркивания. + +function add ($x, $y = 1) { // $y по умолчанию равно 1 + $result = $x + $y; + return $result; +} + +echo add(4); // => 5 +echo add(4, 2); // => 6 + +// $result недоступна за пределами функции +// print $result; // Выдает предупреждение + +// Начиная с PHP 5.3 вы можете объявлять анонимные функции: +$inc = function ($x) { + return $x + 1; +}; + +echo $inc(2); // => 3 + +function foo ($x, $y, $z) { + echo "$x - $y - $z"; +} + +// Функции могут возвращать функции +function bar ($x, $y) { + // Используйте 'use' для передачи внешних переменных + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; +} + +$bar = bar('A', 'B'); +$bar('C'); // Prints "A - B - C" + +// Вы можете вызывать именованные функции используя строки +$function_name = 'add'; +echo $function_name(1, 2); // => 3 +// Полезно для программного определения запущенной функции. +// Или используйте call_user_func(callable $callback [, $parameter [, ... ]]); + + +/******************************** + * Includes + */ + +instanceProp = $instanceProp; + } + + // Methods are declared as functions inside a class + public function myMethod() + { + print 'MyClass'; + } + + final function youCannotOverrideMe() + { + } + + public static function myStaticMethod() + { + print 'I am static'; + } +} + +echo MyClass::MY_CONST; // Выведет 'value'; +echo MyClass::$staticVar; // Выведет 'static'; +MyClass::myStaticMethod(); // Выведет 'I am static'; + +// Новый экземпляр класса используя new +$my_class = new MyClass('An instance property'); + +// Если аргументы отсутствуют, можно не ставить круглые скобки + +// Доступ к членам класса используя -> +echo $my_class->property; // => "public" +echo $my_class->instanceProp; // => "An instance property" +$my_class->myMethod(); // => "MyClass" + +// Наследование классов используя "extends" +class MyOtherClass extends MyClass +{ + function printProtectedProperty() + { + echo $this->prot; + } + + // Override a method + function myMethod() + { + parent::myMethod(); + print ' > MyOtherClass'; + } +} + +$my_other_class = new MyOtherClass('Instance prop'); +$my_other_class->printProtectedProperty(); // => Выведет "protected" +$my_other_class->myMethod(); // Выведет "MyClass > MyOtherClass" + +final class YouCannotExtendMe +{ +} + +// Вы можете использовать "магические методы" для создания геттеров и сеттеров +class MyMapClass +{ + private $property; + + public function __get($key) + { + return $this->$key; + } + + public function __set($key, $value) + { + $this->$key = $value; + } +} + +$x = new MyMapClass(); +echo $x->property; // Будет использован метод __get() +$x->property = 'Something'; // Будет использован метод __set() + +// Классы могут быть абстрактными (используя ключевое слово abstract) +// или реализовывать интерфейсы (используя ключевое слово implements). +// Интерфейсы определяются при помощи ключевого слова interface + +interface InterfaceOne +{ + public function doSomething(); +} + +interface InterfaceTwo +{ + public function doSomethingElse(); +} + +// Интерфейсы могут быть расширены +interface InterfaceThree extends InterfaceTwo +{ + public function doAnotherContract(); +} + +abstract class MyAbstractClass implements InterfaceOne +{ + public $x = 'doSomething'; +} + +class MyConcreteClass extends MyAbstractClass implements InterfaceTwo +{ + public function doSomething() + { + echo $x; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } +} + +// Классы могут реализовывать более одного интерфейса +class SomeOtherClass implements InterfaceOne, InterfaceTwo +{ + public function doSomething() + { + echo 'doSomething'; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } +} + + +/******************************** + * Трейты + */ + +// Трейты появились в PHP 5.4.0 и объявляются при помощи ключевого слова trait + +trait MyTrait +{ + public function myTraitMethod() + { + print 'I have MyTrait'; + } +} + +class MyTraitfulClass +{ + use MyTrait; +} + +$cls = new MyTraitfulClass(); +$cls->myTraitMethod(); // Prints "I have MyTrait" + + +/******************************** + * Пространства имен + */ + +// Это секция особая, ведь объявление пространства имен +// должно быть самым первым в файле. Позвольте сделать вид, что это не так + + Date: Thu, 15 Aug 2013 10:45:06 +0200 Subject: Fix the comments of French Ruby file from the Nami-Doc's comments (adambard/learnxinyminutes-docs#223). Thanks to him. --- fr-fr/ruby-fr.html.markdown | 81 +++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown index f6bfe42a..f5212787 100644 --- a/fr-fr/ruby-fr.html.markdown +++ b/fr-fr/ruby-fr.html.markdown @@ -9,6 +9,7 @@ contributors: - ["Nick LaMuro", "https://github.com/NickLaMuro"] translators: - ["Geoffrey Roguelon", "https://github.com/GRoguelon"] + - ["Nami-Doc", "https://github.com/Nami-Doc"] lang: fr-fr --- @@ -29,21 +30,21 @@ Vous devriez en faire de même 3.to_s #=> "3" -# Arithmétique de base +# Les opérateurs de base 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -# L'arithmétique est juste un raccourci -# pour appeler la méthode d'un objet +# Les opérateurs sont juste des raccourcis +# pour appeler une méthode sur un objet 1.+(3) #=> 4 10.* 5 #=> 50 # Les valeurs spéciales sont des objets -nil # Nothing to see here -true # truth -false # falsehood +nil # Nul +true # Vrai +false # Faux nil.class #=> NilClass true.class #=> TrueClass @@ -80,7 +81,7 @@ placeholder = "utiliser l'interpolation de chaîne de caractères" "Je peux #{placeholder} quand j'utilise les guillemets" #=> "Je peux utiliser l'interpolation de chaîne de caractères quand j'utilise les guillemets" -# Afficher sur la sortie standard +# Affichez un message puts "J'affiche à l'écran!" # Variables @@ -94,15 +95,15 @@ x = y = 10 #=> 10 x #=> 10 y #=> 10 -# Par convention, utiliser snake_case (à base d'underscore) -# comme nom de variable +# Par convention, utilisez la notation underscore +# pour nommer les variables snake_case = true -# Utiliser des noms de variable explicites +# Utilisez des noms de variable explicites path_to_project_root = '/nom/correct/' path = '/mauvais/nom/' -# Symboles (sont des objets) +# Symboles (aussi des objets) # Les symboles sont immuables, constants, # réutilisables et représentés en interne # par une valeur entière. Ils sont souvent @@ -129,40 +130,40 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] [1, "hello", false] #=> [1, "hello", false] -# Les tableaux peuvent être indéxés +# Les tableaux peuvent être indexés # Du début array[0] #=> 1 array[12] #=> nil -# Comme l'arithmétique, la syntaxe [var] est juste un raccourci +# Comme les opérateurs, la syntaxe [var] est juste un raccourci # pour appeler la méthode [] d'un objet array.[] 0 #=> 1 array.[] 12 #=> nil -# À la fin +# Depuis la fin array[-1] #=> 5 # Avec un index de début et de fin array[2, 4] #=> [3, 4, 5] -# Ou avec un interval +# Ou avec un intervalle array[1..3] #=> [2, 3, 4] -# Ajouter un élément au tableau comme ceci +# Ajoutez un élément au tableau comme ceci array << 6 #=> [1, 2, 3, 4, 5, 6] -# Les Hashes sont des dictionnaires Ruby contenant des paires de clé/valeur. -# Les Hashes sont délimitées par des accolades : +# Les Hash sont des dictionnaires Ruby contenant des paires de clé/valeur. +# Les Hash sont délimitées par des accolades : hash = {'color' => 'green', 'number' => 5} hash.keys #=> ['color', 'number'] -# Les Hashes peuvent retourner rapidement -# la valeur en utilisant la clé : +# Les Hash retournent la valeur +# en utilisant la clé associée à la valeur : hash['color'] #=> 'green' hash['number'] #=> 5 -# Rechercher une clé inexistante dans une Hash retourne nil: +# Recherchez une clé inexistante dans une Hash retourne nil : hash['nothing here'] #=> nil # Depuis Ruby 1.9, Une syntaxe spécifique est apparue en utilisant les symboles comme clés : @@ -171,7 +172,7 @@ new_hash = { defcon: 3, action: true} new_hash.keys #=> [:defcon, :action] -# Astuce : Les tableaux et les Hashes sont dénombrables +# Astuce : Les tableaux et les Hash sont dénombrables # Ils partagent un certain nombre de méthodes pratiques # telle que each, map, count, etc... @@ -194,15 +195,15 @@ end #=> itération 4 #=> itération 5 -# CEPENDANT, Personne n'utilise la boucle for. -# À la place, vous devrez utiliser la méthode "each" -# et lui passer un bloc de code. +# CEPENDANT, l'usage de la boucle for est très rare. +# À la place, utilisez la méthode "each" +# et passez lui un bloc de code. # Un bloc de code est un ensemble d'instructions que vous pouvez passer à une methode comme "each". # Les blocs sont similaires aux lambdas, les fonctions anonymes ou les closures dans d'autres langages. # -# La méthode "each" exécute le bloc de code pour chaque élément de l'interval d'éléments. +# La méthode "each" exécute le bloc de code pour chaque élément de l'intervalle d'éléments. # Le bloc de code passe un paramètre compteur. -# Appeler la méthode "each" avec un bloc de code comme ceci : +# Appelez la méthode "each" avec un bloc de code comme ceci : (1..5).each do |compteur| puts "itération #{compteur}" @@ -240,7 +241,7 @@ grade = 'B' case grade when 'A' - puts "Excellence" + puts "Excellent" when 'B' puts "Plus de chance la première fois" when 'C' @@ -264,7 +265,7 @@ end double(2) #=> 4 # Les paranthèses sont facultative -# où il n'y a pas d'ambiguïté sur le résultat +# lorsqu'il n'y a pas d'ambiguïté sur le résultat double 3 #=> 6 double double 3 #=> 12 @@ -279,8 +280,8 @@ sum 3, 4 #=> 7 sum sum(3,4), 5 #=> 12 # yield -# Toutes les méthodes ont un argument de type bloc de code -# facultatif et implicite +# Toutes les méthodes ont un argument facultatif et implicite +# de type bloc de code # il peut être appelé avec le mot clé 'yield' def surround @@ -296,16 +297,16 @@ surround { puts 'Bonjour tout le monde' } # } -# Définir une classe avec le mot clé 'class' +# Définissez une classe avec le mot clé 'class' class Humain - # Une variable de classe, - # elle est partagée par toutes les instances de cette classe. + # Une variable de classe + # est partagée par toutes les instances de cette classe. @@espece = "H. sapiens" # Constructeur de classe def initialize(nom, age = 0) - # Affecter l'argument à la variable d'instance 'nom' + # Affectez l'argument à la variable d'instance 'nom' # pour la durée de vie de l'instance de cette classe @nom = nom # Si le paramètre 'age' est absent, @@ -338,12 +339,12 @@ class Humain end -# Instancier une classe +# Instanciez une classe jim = Humain.new("Jim Halpert") dwight = Humain.new("Dwight K. Schrute") -# Appelons quelques méthodes +# Appelez quelques méthodes jim.espece #=> "H. sapiens" jim.nom #=> "Jim Halpert" jim.nom = "Jim Halpert II" #=> "Jim Halpert II" @@ -351,7 +352,7 @@ jim.nom #=> "Jim Halpert II" dwight.espece #=> "H. sapiens" dwight.nom #=> "Dwight K. Schrute" -# Appelons la méthode de classe +# Appelez la méthode de classe Humain.say("Hi") #=> "Hi" # Les classes sont également des objets en Ruby. @@ -359,7 +360,7 @@ Humain.say("Hi") #=> "Hi" # Les variables de classe sont partagées parmi # la classe et ses descendants. -# Classe de base +# Classe parente class Humain @@foo = 0 @@ -372,7 +373,7 @@ class Humain end end -# Héritage de classe +# Classe fille class Travailleur < Humain end -- cgit v1.2.3 From 7cb78e94d24d7c768868fa4bd575a7572126cbe5 Mon Sep 17 00:00:00 2001 From: dacechavez Date: Thu, 15 Aug 2013 12:12:19 +0200 Subject: Fixed 2 typos around function pointers --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 2b50efa0..e8a26056 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -373,7 +373,7 @@ int area(rect r){ /////////////////////////////////////// /* At runtime, functions are located at known memory addresses. Function pointers are -much likely any other pointer (they just store a memory address), but can be used +much like any other pointer (they just store a memory address), but can be used to invoke functions directly, and to pass handlers (or callback functions) around. However, definition syntax may be initially confusing. @@ -394,7 +394,7 @@ Function pointers are usually typedef'd for simplicity and readability, as follo typedef void (*my_fnp_type)(char *); -// The used when declaring the actual pointer variable: +// Then used when declaring the actual pointer variable: // ... // my_fnp_type f; -- cgit v1.2.3 From 7f8e5deba129c4b9fb34b89077a63829546b0bd3 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Thu, 15 Aug 2013 12:22:36 +0200 Subject: Fix a word of French Ruby language. --- fr-fr/ruby-fr.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown index f5212787..5efb2f3c 100644 --- a/fr-fr/ruby-fr.html.markdown +++ b/fr-fr/ruby-fr.html.markdown @@ -243,7 +243,7 @@ case grade when 'A' puts "Excellent" when 'B' - puts "Plus de chance la première fois" + puts "Plus de chance la prochaine fois" when 'C' puts "Vous pouvez faire mieux" when 'D' -- cgit v1.2.3 From 24fd870589d863618e6ba2571b6a0c8339d35edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rp=C3=A1d=20Goretity=20=EF=A3=BF?= Date: Thu, 15 Aug 2013 12:30:22 +0200 Subject: Update c.html.markdown --- c.html.markdown | 629 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 356 insertions(+), 273 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 2b50efa0..ac6c7ab4 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -5,19 +5,20 @@ language: c filename: learnc.c contributors: - ["Adam Bard", "http://adambard.com/"] + - ["Árpád Goretity", "http://twitter.com/h2co3_ios"] --- -Ah, C. Still the language of modern high-performance computing. +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 // +// Single-line comments start with // - only available in C99 and later. /* -Multi-line comments look like this. +Multi-line comments look like this. They work in C89 as well. */ // Import headers with #include @@ -25,6 +26,11 @@ Multi-line comments look like this. #include #include +// file names between are headers from the C standard library. +// They are searched for by the preprocessor in the system include paths +// (usually /usr/lib on Unices, can be controlled with the -I option if you are using GCC or clang.) +// For your + // Declare function signatures in advance in a .h file, or at the top of // your .c file. void function_1(); @@ -33,264 +39,317 @@ 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 -/////////////////////////////////////// - -// You have to declare variables before using them. A variable declaration -// requires you to specify its type; a variable's type determines its size -// in bytes. - -// ints are usually 4 bytes -int x_int = 0; - -// shorts are usually 2 bytes -short x_short = 0; - -// chars are guaranteed to be 1 byte -char x_char = 0; -char y_char = 'y'; // Char literals are quoted with '' - -// longs are often 4 to 8 bytes; long longs are guaranteed to be at least -// 64 bits -long x_long = 0; -long long x_long_long = 0; - -// floats are usually 32-bit floating point numbers -float x_float = 0.0; - -// doubles are usually 64-bit floating-point numbers -double x_double = 0.0; - -// Integral types may be unsigned. This means they can't be negative, but -// the maximum value of an unsigned variable is greater than the maximum -// signed value of the same size. -unsigned char ux_char; -unsigned short ux_short; -unsigned int ux_int; -unsigned long long ux_long_long; - -// Other than char, which is always 1 byte, these types vary in size depending -// on your machine. sizeof(T) gives you the size of a variable with type T in -// bytes so you can express the size of these types in a portable way. -// For example, -printf("%lu\n", sizeof(int)); // => 4 (on machines with 4-byte words) - -// 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 - // (assuming 4-byte words) - - -// 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 arrays of chars terminated by a NUL (0x00) byte, -// represented in strings as the special character '\0'. -// (We don't have to include the NUL byte in string literals; the compiler -// inserts it at the end of the array for us.) -char a_string[20] = "This is a string"; -printf("%s\n", a_string); // %s formats a string - -/* -You may have noticed that a_string is only 16 chars long. -Char #17 is the NUL byte. -Chars #18, 19 and 20 have undefined values. -*/ - -printf("%d\n", a_string[16]); // => 0 - -/////////////////////////////////////// -// 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. (The comparison -// operators always return 0 or 1.) -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; // => 0x0B (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 -/////////////////////////////////////// - -// Every value in C has a type, but you can cast one value into another type -// if you want. - -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) - -// Integral types can be cast to floating-point types, and vice-versa. -printf("%f\n", (float)100); // %f formats a float -printf("%lf\n", (double)100); // %lf formats a double -printf("%d\n", (char)100.0); - -/////////////////////////////////////// -// Pointers -/////////////////////////////////////// - -// A pointer is a variable declared to store a memory address. Its declaration will -// also tell you the type of data it points to. You can retrieve the memory address -// of your variables, then mess with them. - -int x = 0; -printf("%p\n", &x); // Use & to retrieve the address of a variable -// (%p formats a pointer) -// => Prints some address in memory; - - -// Pointers start with * in their declaration -int *px, not_a_pointer; // px is a pointer to an int -px = &x; // Stores the address of x in px -printf("%p\n", px); // => Prints some address in memory -printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer)); -// => Prints "8, 4" on 64-bit system - -// To retreive the value at the address a pointer is pointing to, -// put * in front to de-reference it. -printf("%d\n", *px); // => Prints 0, the value of x, which is what px is pointing to the address of - -// You can also change the value the pointer is pointing to. -// We'll have to wrap the de-reference in parenthesis because -// ++ has a higher precedence than *. -(*px)++; // Increment the value px is pointing to by 1 -printf("%d\n", *px); // => Prints 1 -printf("%d\n", x); // => Prints 1 - -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 - -// Declare a pointer of type int and initialize it to point to x_array -int* x_ptr = x_array; -// x_ptr now points to the first element in the array (the integer 20). -// This works because arrays are actually just pointers to their first element. - -// Arrays are pointers to their first element -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 - -// You can also dynamically allocate contiguous blocks of memory with the -// standard library function malloc, which takes one integer argument -// representing the number of bytes to allocate from the heap. -int* my_ptr = (int*) malloc(sizeof(int) * 20); -for (xx=0; xx<20; xx++) { - *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx would also work here -} // 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 of memory, you need to free it, -// or else no one else can use it until your program terminates -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("%c\n", *my_str); // => 'T' - -function_1(); + // 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 + /////////////////////////////////////// + + // You have to declare variables before using them. A variable declaration + // requires you to specify its type; a variable's type determines its size + // in bytes. + + // ints are usually 4 bytes + int x_int = 0; + + // shorts are usually 2 bytes + short x_short = 0; + + // chars are guaranteed to be 1 byte + char x_char = 0; + char y_char = 'y'; // Char literals are quoted with '' + + // longs are often 4 to 8 bytes; long longs are guaranteed to be at least + // 64 bits + long x_long = 0; + long long x_long_long = 0; + + // floats are usually 32-bit floating point numbers + float x_float = 0.0; + + // doubles are usually 64-bit floating-point numbers + double x_double = 0.0; + + // Integral types may be unsigned. This means they can't be negative, but + // the maximum value of an unsigned variable is greater than the maximum + // signed value of the same size. + unsigned char ux_char; + unsigned short ux_short; + unsigned int ux_int; + unsigned long long ux_long_long; + + // Other than char, which is always 1 byte (but not necessarily 8 bits!), + // these types vary in size depending on your machine and compiler. + // sizeof(T) gives you the size of a variable with type T in + // bytes so you can express the size of these types in a portable way. + // sizeof(obj) yields the size of an actual expression (variable, literal, etc.). + // For example, + printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) + + + // It's worth noting that if the argument of the `sizeof` operator is not a type but an expression, + // then its argument is not evaluated except VLAs (see below). Also, `sizeof()` is an operator, not a function, + // furthermore, the value it yields is a compile-time constant (except when used on VLAs, again.) + int a = 1; + size_t size = sizeof(a++); // a++ is not evaluated + printf("sizeof(a++) = %zu where a = %d\n", size, a); + // the above code prints "sizeof(a++) = 4 where a = 1" (on a usual 32-bit architecture) + + // 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 + // (assuming 4-byte words) + + + // 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 + + // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) can be declared as well. + // The size of such an array need not be a compile time constant: + printf("Enter the array size: "); // ask the user for an array size + char buf[0x100]; + fgets(buf, sizeof buf, stdin); + size_t size = strtoul(buf, NULL, 10); // strtoul parses a string to an unsigned integer + int var_length_array[size]; // declare the VLA + printf("sizeof array = %zu\n", sizeof var_length_array); + + // A possible outcome of this program may be: + Enter the array size: 10 + sizeof array = 40 + + // Strings are just arrays of chars terminated by a NUL (0x00) byte, + // represented in strings as the special character '\0'. + // (We don't have to include the NUL byte in string literals; the compiler + // inserts it at the end of the array for us.) + char a_string[20] = "This is a string"; + printf("%s\n", a_string); // %s formats a string + + /* + You may have noticed that a_string is only 16 chars long. + Char #17 is the NUL byte. + Chars #18, 19 and 20 are 0 as well - if an initializer list (in this case, the string literal) + has less elements than the array it is initializing, then excess array elements are implicitly + initialized to zero. This is why int ar[10] = { 0 } works as expected intuitively. + */ + + printf("%d\n", a_string[16]); // => 0 + + // So string literals are strings enclosed within double quotes, but if we have characters + // between single quotes, that's a character literal. + // It's of type `int`, and *not* `char` (for hystorical reasons). + int cha = 'a'; // fine + char chb = 'a'; // fine too (implicit conversion from int to char - truncation) + + /////////////////////////////////////// + // 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 - floating-point numbers and calculations are not exact + + // 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. (The comparison + // operators always yield 0 or 1.) + 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, "1's complement") + 0x0F & 0xF0; // => 0x00 (bitwise AND) + 0x0F | 0xF0; // => 0xFF (bitwise OR) + 0x04 ^ 0x0F; // => 0x0B (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 yielding its value ("postincrement"). + } // => 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, and yields the already incremented value ("preincrement") + // => 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 + /////////////////////////////////////// + + // Every value in C has a type, but you can cast one value into another type + // if you want (with some constraints). + + 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", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long) + // printf("%d\n", (unsigned char) 257); would be undefined behavior - `char' is usually signed + // on most modern systems, and signed integer overflow invokes UB. + // Also, for determining the maximal value of a `char`, a `signed char` and an `unisigned char`, + // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from + + // Integral types can be cast to floating-point types, and vice-versa. + printf("%f\n", (float)100); // %f formats a float + printf("%lf\n", (double)100); // %lf formats a double + printf("%d\n", (char)100.0); + + /////////////////////////////////////// + // Pointers + /////////////////////////////////////// + + // A pointer is a variable declared to store a memory address. Its declaration will + // also tell you the type of data it points to. You can retrieve the memory address + // of your variables, then mess with them. + + int x = 0; + printf("%p\n", (void *)&x); // Use & to retrieve the address of a variable + // (%p formats an object pointer of type void *) + // => Prints some address in memory; + + + // Pointers start with * in their declaration + int *px, not_a_pointer; // px is a pointer to an int + px = &x; // Stores the address of x in px + printf("%p\n", (void *)px); // => Prints some address in memory + printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer)); + // => Prints "8, 4" on a typical 64-bit system + + // To retreive the value at the address a pointer is pointing to, + // put * in front to de-reference it. + // Note: yes, it may be confusing that '*' is used for _both_ declaring a pointer and dereferencing it. + printf("%d\n", *px); // => Prints 0, the value of x, which is what px is pointing to the address of + + // You can also change the value the pointer is pointing to. + // We'll have to wrap the de-reference in parenthesis because + // ++ has a higher precedence than *. + (*px)++; // Increment the value px is pointing to by 1 + printf("%d\n", *px); // => Prints 1 + printf("%d\n", x); // => Prints 1 + + 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 + + // Declare a pointer of type int and initialize it to point to x_array + int* x_ptr = x_array; + // x_ptr now points to the first element in the array (the integer 20). + // This works because arrays often decay into pointers to their first element. + // For example, when an array is passed to a function or is assigned to a pointer, + // it decays into (implicitly converted to) a pointer. + // Exceptions: when the array is the argument of the `&` (address-od) operator: + int arr[10]; + int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! It's of type "pointer to array" (of ten `int`s). + // or when the array is a string literal used for initializing a char array: + char arr[] = "foobarbazquirk"; + // or when it's the argument of the `sizeof` or `alignof` operator: + int arr[10]; + int *ptr = arr; // equivalent with int *ptr = &arr[0]; + printf("%zu %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8" + + + // Pointers are incremented and decremented based on their type + // (this is called pointer arithmetic) + printf("%d\n", *(x_ptr + 1)); // => Prints 19 + printf("%d\n", x_array[1]); // => Prints 19 + + // You can also dynamically allocate contiguous blocks of memory with the + // standard library function malloc, which takes one argument of type size_t + // representing the number of bytes to allocate (usually from the heap, although this + // may not be true on e. g. embedded systems - the C standard says nothing about it). + int *my_ptr = malloc(sizeof(*my_ptr) * 20); + for (xx = 0; xx < 20; xx++) { + *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx would also work here, and it's also more readable + } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) + + // Dereferencing memory that you haven't allocated gives + // "unpredictable results" - the program is said to invoke "undefined behavior" + printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash. + + // When you're done with a malloc'd block of memory, you need to free it, + // or else no one else can use it until your program terminates + // (this is called a "memory leak"): + free(my_ptr); + + // Strings are arrays of char, but they are usually represented as a + // pointer-to-char (which is a pointer to the first element of the array). + // It's good practice to use `const char *' when referring to a string literal, + // since string literals shall not be modified (i. e. "foo"[0] = 'a' is ILLEGAL.) + const char *my_str = "This is my very own string literal"; + printf("%c\n", *my_str); // => 'T' + + // This is not the case if the string is an array (potentially initialized with a string literal) + // that resides in writable memory, as in: + char foo[] = "foo"; + foo[0] = 'a'; // this is legal, foo now contains "aoo" + + function_1(); } // end main function /////////////////////////////////////// @@ -300,7 +359,8 @@ function_1(); // Function declaration syntax: // () -int add_two_ints(int x1, int x2){ +int add_two_ints(int x1, int x2) +{ return x1 + x2; // Use return to return a value } @@ -312,10 +372,12 @@ Example: in-place string reversal */ // A void function returns no value -void str_reverse(char* str_in){ +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" typedef int my_type; my_type my_type_var = 0; -// Structs are just collections of data +// Structs are just collections of data, the members are allocated sequentially, in the order they are written: struct rectangle { int width; int height; }; +// it's generally not true that sizeof(struct rectangle) == sizeof(int) + sizeof(int) due to +// potential padding between the structure members (this is for alignment reasons. Probably won't +// happen if all members are of the same type, but watch out! +// See http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member +// for further information. -void function_1(){ - +void function_1() +{ struct rectangle my_rec; // Access struct members with . @@ -352,22 +419,29 @@ void function_1(){ my_rec.height = 20; // You can declare pointers to structs - struct rectangle* my_rec_ptr = &my_rec; + struct rectangle *my_rec_ptr = &my_rec; // Use dereferencing to set struct pointer members... (*my_rec_ptr).width = 30; - // ... or use the -> shorthand + // ... or even better: prefer the -> shorthand for the sake of readability 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){ +int area(rect r) +{ return r.width * r.height; } +// if you have large structs, you can pass them "by pointer" to avoid copying the whole struct: +int area(const rect *r) +{ + return r->width * r->height; +} + /////////////////////////////////////// // Function pointers /////////////////////////////////////// @@ -379,10 +453,11 @@ However, definition syntax may be initially confusing. Example: use str_reverse from a pointer */ -void str_reverse_through_pointer(char * str_in) { +void str_reverse_through_pointer(char *str_in) { // Define a function pointer variable, named f. void (*f)(char *); // Signature should exactly match the target function. f = &str_reverse; // Assign the address for the actual function (determined at runtime) + // f = str_reverse; would work as well - functions decay into pointers, similar to arrays (*f)(str_in); // Just calling the function through the pointer // f(str_in); // That's an alternative but equally valid syntax for calling it. } @@ -403,7 +478,15 @@ typedef void (*my_fnp_type)(char *); ## 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) +It is *the* book about C, written by the creators of C. Be careful, though - it's ancient and it contains some +inaccuracies (well, ideas that are not considered good anymore) or now-changed practices. + +Another good resource is [Learn C the hard way](http://c.learncodethehardway.org/book/). + +If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com). -Another good resource is [Learn C the hard way](http://c.learncodethehardway.org/book/) +It's very important to use proper spacing, indentation and to be consistent with your coding style in general. +Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the +[Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). Other than that, Google is your friend. -- cgit v1.2.3 From f2c41a8a9915606ed801a87f153d91fd347069a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rp=C3=A1d=20Goretity=20=EF=A3=BF?= Date: Thu, 15 Aug 2013 12:36:29 +0200 Subject: whoops, fixed typos and added missing info --- c.html.markdown | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index ac6c7ab4..ec5c5e0d 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -5,7 +5,6 @@ language: c filename: learnc.c contributors: - ["Adam Bard", "http://adambard.com/"] - - ["Árpád Goretity", "http://twitter.com/h2co3_ios"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -29,7 +28,13 @@ Multi-line comments look like this. They work in C89 as well. // file names between are headers from the C standard library. // They are searched for by the preprocessor in the system include paths // (usually /usr/lib on Unices, can be controlled with the -I option if you are using GCC or clang.) -// For your +// For your own headers, use double quotes instead of angle brackets: +#include "my_header.h" + +// The C preprocessor introduces an almost fully-featured macro language. It's useful, but +// it can be confusing (and what's even worse, it can be misused). Read the +// Wikipedia article on the C preprocessor for further information: +// http://en.wikipedia.org/wiki/C_preprocessor // Declare function signatures in advance in a .h file, or at the top of // your .c file. -- cgit v1.2.3 From 05c3ca90447e6f63fa02847468e8a95f250fbe50 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Thu, 15 Aug 2013 20:33:44 +0930 Subject: [javascript] Remove anonymous function assignment example. ref #215 --- javascript.html.markdown | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 9b87b022..25777578 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -219,17 +219,9 @@ function myFunction(){ } setTimeout(myFunction, 5000) -// Functions can also be defined "anonymously" - without a name: -var lowerFunction = function(thing){ - return thing.toLowerCase() -} -lowerFunction("Foo") // = "foo" -// (note: we've assigned our anonymous function to a variable - if we didn't, we -// wouldn't be able to access it) - -// You can even write the function statement directly in the call to the other -// function. -setTimeout(function myFunction(){ +// Function objects don't even have to be declared with a name - you can write +// an anonymous function definition directly into the arguments of another. +setTimeout(function(){ // this code will be called in 5 seconds' time }, 5000) -- cgit v1.2.3 From 273c08d1fe2ebe362826fd0707d55b728538c33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rp=C3=A1d=20Goretity=20=EF=A3=BF?= Date: Thu, 15 Aug 2013 13:08:52 +0200 Subject: fixed header, added switch statement --- c.html.markdown | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index ec5c5e0d..8e16837c 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -1,10 +1,12 @@ --- -name: c -category: language -language: c -filename: learnc.c -contributors: - - ["Adam Bard", "http://adambard.com/"] +- name: c +- category: language +- language: c +- filename: learnc.c +- contributors: + - [Adam Bard](http://adambard.com/) + - [Árpád Goretity](http://twitter.com/H2CO3_iOS) + --- Ah, C. Still **the** language of modern high-performance computing. @@ -152,7 +154,7 @@ int main() { // So string literals are strings enclosed within double quotes, but if we have characters // between single quotes, that's a character literal. - // It's of type `int`, and *not* `char` (for hystorical reasons). + // It's of type `int`, and *not* `char` (for historical reasons). int cha = 'a'; // fine char chb = 'a'; // fine too (implicit conversion from int to char - truncation) @@ -176,6 +178,7 @@ int main() { // Comparison operators are probably familiar, but // there is no boolean type in c. We use ints instead. + // (Or _Bool or bool in C99.) // 0 is false, anything else is true. (The comparison // operators always yield 0 or 1.) 3 == 2; // => 0 (false) @@ -185,6 +188,13 @@ int main() { 2 <= 2; // => 1 2 >= 2; // => 1 + // C is not Python - comparisons don't chain. + int a = 1; + // WRONG: + int between_0_and_2 = 0 < a < 2; + // Correct: + int between_0_and_2 = 0 < a && a < 2; + // Logic works on ints !3; // => 0 (Logical not) !0; // => 1 @@ -201,6 +211,12 @@ int main() { 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) 0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) + // Be careful when shifting signed integers - the following are all undefined behavior: + // - shifting into the sign bit of a signed integer (int a = 1 << 32) + // - left-shifting a negative number (int a = -1 << 2) + // - shifting by an offset which is more than or equal to the width of the type of the LHS: + // int a = 1 << 32; // UB if int is 32 bits wide + /////////////////////////////////////// // Control Structures /////////////////////////////////////// @@ -237,6 +253,22 @@ int main() { printf("\n"); + // branching with multiple choices: switch() + switch (some_integral_expression) { + case 0: // labels need to be integral *constant* epxressions + do_stuff(); + break; // if you don't break, control flow falls over labels - you usually don't want that. + case 1: + do_something_else(); + break; + default: + // if `some_integral_expression` didn't match any of the labels + fputs("error!\n", stderr); + exit(-1); + break; + } + + /////////////////////////////////////// // Typecasting /////////////////////////////////////// -- cgit v1.2.3 From 80f97751a21b0c04ee077c23b715a8adc0063788 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Thu, 15 Aug 2013 21:05:27 +0930 Subject: [javascript] Add semicolons. Closes #214. --- javascript.html.markdown | 218 +++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 109 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 25777578..fb79949e 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -30,82 +30,82 @@ doStuff(); // wherever there's a newline, except in certain cases. doStuff() -// We'll leave semicolons off here; whether you do or not will depend on your -// personal preference or your project's style guide. +// So that we don't have to worry about those certain cases (for now), we'll +// leave them on. /////////////////////////////////// // 1. Numbers, Strings and Operators // Javascript has one number type (which is a 64-bit IEEE 754 double). -3 // = 3 -1.5 // = 1.5 +3; // = 3 +1.5; // = 1.5 // All the basic arithmetic works as you'd expect. -1 + 1 // = 2 -8 - 1 // = 7 -10 * 2 // = 20 -35 / 5 // = 7 +1 + 1; // = 2 +8 - 1; // = 7 +10 * 2; // = 20 +35 / 5; // = 7 // Including uneven division. -5 / 2 // = 2.5 +5 / 2; // = 2.5 // Bitwise operations also work; when you perform a bitwise operation your float // is converted to a signed int *up to* 32 bits. -1 << 2 // = 4 +1 << 2; // = 4 // Precedence is enforced with parentheses. -(1 + 3) * 2 // = 8 +(1 + 3) * 2; // = 8 // There are three special not-a-real-number values: -Infinity // result of e.g. 1/0 --Infinity // result of e.g. -1/0 -NaN // result of e.g. 0/0 +Infinity; // result of e.g. 1/0 +-Infinity; // result of e.g. -1/0 +NaN; // result of e.g. 0/0 // There's also a boolean type. -true -false +true; +false; // Strings are created with ' or ". -'abc' -"Hello, world" +'abc'; +"Hello, world"; // Negation uses the ! symbol -!true // = false -!false // = true +!true; // = false +!false; // = true // Equality is == -1 == 1 // = true -2 == 1 // = false +1 == 1; // = true +2 == 1; // = false // Inequality is != -1 != 1 // = false -2 != 1 // = true +1 != 1; // = false +2 != 1; // = true // More comparisons -1 < 10 // = true -1 > 10 // = false -2 <= 2 // = true -2 >= 2 // = true +1 < 10; // = true +1 > 10; // = false +2 <= 2; // = true +2 >= 2; // = true // Strings are concatenated with + -"Hello " + "world!" // = "Hello world!" +"Hello " + "world!"; // = "Hello world!" // and are compared with < and > -"a" < "b" // = true +"a" < "b"; // = true // Type coercion is performed for comparisons... -"5" == 5 // = true +"5" == 5; // = true // ...unless you use === -"5" === 5 // = false +"5" === 5; // = false // You can access characters in a string with charAt -"This is a string".charAt(0) +"This is a string".charAt(0); // There's also null and undefined -null // used to indicate a deliberate non-value -undefined // used to indicate a value is not currently present (although undefined - // is actually a value itself) +null; // used to indicate a deliberate non-value +undefined; // used to indicate a value is not currently present (although undefined + // is actually a value itself) // false, null, undefined, NaN, 0 and "" are falsy, and everything else is truthy. // Note that 0 is falsy and "0" is truthy, even though 0 == "0". @@ -115,57 +115,57 @@ undefined // used to indicate a value is not currently present (although undefin // Variables are declared with the var keyword. Javascript is dynamically typed, // so you don't need to specify type. Assignment uses a single = character. -var someVar = 5 +var someVar = 5; // if you leave the var keyword off, you won't get an error... -someOtherVar = 10 +someOtherVar = 10; // ...but your variable will be created in the global scope, not in the scope // you defined it in. // Variables declared without being assigned to are set to undefined. -var someThirdVar // = undefined +var someThirdVar; // = undefined // There's shorthand for performing math operations on variables: -someVar += 5 // equivalent to someVar = someVar + 5; someVar is 10 now -someVar *= 10 // now someVar is 100 +someVar += 5; // equivalent to someVar = someVar + 5; someVar is 10 now +someVar *= 10; // now someVar is 100 // and an even-shorter-hand for adding or subtracting 1 -someVar++ // now someVar is 101 -someVar-- // back to 100 +someVar++; // now someVar is 101 +someVar--; // back to 100 // Arrays are ordered lists of values, of any type. -var myArray = ["Hello", 45, true] +var myArray = ["Hello", 45, true]; // Their members can be accessed using the square-brackets subscript syntax. // Array indices start at zero. -myArray[1] // = 45 +myArray[1]; // = 45 // JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other // languages: an unordered collection of key-value pairs. -var myObj = {key1: "Hello", key2: "World"} +var myObj = {key1: "Hello", key2: "World"}; // Keys are strings, but quotes aren't required if they're a valid // JavaScript identifier. Values can be any type. -var myObj = {myKey: "myValue", "my other key": 4} +var myObj = {myKey: "myValue", "my other key": 4}; // Object attributes can also be accessed using the subscript syntax, -myObj["my other key"] // = 4 +myObj["my other key"]; // = 4 // ... or using the dot syntax, provided the key is a valid identifier. -myObj.myKey // = "myValue" +myObj.myKey; // = "myValue" // Objects are mutable; values can be changed and new keys added. -myObj.myThirdKey = true +myObj.myThirdKey = true; // If you try to access a value that's not yet set, you'll get undefined. -myObj.myFourthKey // = undefined +myObj.myFourthKey; // = undefined /////////////////////////////////// // 3. Logic and Control Structures // The if structure works as you'd expect. -var count = 1 +var count = 1; if (count == 3){ // evaluated if count is 3 } else if (count == 4) { @@ -182,7 +182,7 @@ while (true) { // Do-while loops are like while loops, except they always run at least once. var input do { - input = getInput() + input = getInput(); } while (!isValid(input)) // the for loop is the same as C and Java: @@ -193,23 +193,23 @@ for (var i = 0; i < 5; i++){ // && is logical and, || is logical or if (house.size == "big" && house.colour == "blue"){ - house.contains = "bear" + house.contains = "bear"; } if (colour == "red" || colour == "blue"){ // colour is either red or blue } // && and || "short circuit", which is useful for setting default values. -var name = otherName || "default" +var name = otherName || "default"; /////////////////////////////////// // 4. Functions, Scope and Closures // JavaScript functions are declared with the function keyword. function myFunction(thing){ - return thing.toUpperCase() + return thing.toUpperCase(); } -myFunction("foo") // = "FOO" +myFunction("foo"); // = "FOO" // JavaScript functions are first class objects, so they can be reassigned to // different variable names and passed to other functions as arguments - for @@ -217,49 +217,49 @@ myFunction("foo") // = "FOO" function myFunction(){ // this code will be called in 5 seconds' time } -setTimeout(myFunction, 5000) +setTimeout(myFunction, 5000); // Function objects don't even have to be declared with a name - you can write // an anonymous function definition directly into the arguments of another. setTimeout(function(){ // this code will be called in 5 seconds' time -}, 5000) +}, 5000); // JavaScript has function scope; functions get their own scope but other blocks // do not. if (true){ - var i = 5 + var i = 5; } -i // = 5 - not undefined as you'd expect in a block-scoped language +i; // = 5 - not undefined as you'd expect in a block-scoped language // This has led to a common pattern of "immediately-executing anonymous // functions", which prevent temporary variables from leaking into the global // scope. (function(){ - var temporary = 5 + var temporary = 5; // We can access the global scope by assiging to the 'global object', which // in a web browser is always 'window'. The global object may have a // different name in non-browser environments such as Node.js. - window.permanent = 10 -})() -temporary // raises ReferenceError -permanent // = 10 + window.permanent = 10; +})(); +temporary; // raises ReferenceError +permanent; // = 10 // One of JavaScript's most powerful features is closures. If a function is // defined inside another function, the inner function has access to all the // outer function's variables, even after the outer function exits. function sayHelloInFiveSeconds(name){ - var prompt = "Hello, " + name + "!" + var prompt = "Hello, " + name + "!"; function inner(){ - alert(prompt) + alert(prompt); } - setTimeout(inner, 5000) + setTimeout(inner, 5000); // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will // exit immediately, and setTimeout will call inner afterwards. However, // because inner is "closed over" sayHelloInFiveSeconds, inner still has // access to the 'prompt' variable when it is finally called. } -sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s +sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s /////////////////////////////////// // 5. More about Objects; Constructors and Prototypes @@ -267,44 +267,44 @@ sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s // Objects can contain functions. var myObj = { myFunc: function(){ - return "Hello world!" + return "Hello world!"; } -} -myObj.myFunc() // = "Hello world!" +}; +myObj.myFunc(); // = "Hello world!" // When functions attached to an object are called, they can access the object // they're attached to using the this keyword. myObj = { myString: "Hello world!", myFunc: function(){ - return this.myString + return this.myString; } -} -myObj.myFunc() // = "Hello world!" +}; +myObj.myFunc(); // = "Hello world!" // What this is set to has to do with how the function is called, not where // it's defined. So, our function doesn't work if it isn't called in the // context of the object. -var myFunc = myObj.myFunc -myFunc() // = undefined +var myFunc = myObj.myFunc; +myFunc(); // = undefined // Inversely, a function can be assigned to the object and gain access to it // through this, even if it wasn't attached when it was defined. var myOtherFunc = function(){ - return this.myString.toUpperCase() + return this.myString.toUpperCase(); } -myObj.myOtherFunc = myOtherFunc -myObj.myOtherFunc() // = "HELLO WORLD!" +myObj.myOtherFunc = myOtherFunc; +myObj.myOtherFunc(); // = "HELLO WORLD!" // When you call a function with the new keyword, a new object is created, and // made available to the function via this. Functions designed to be called // like this are called constructors. var MyConstructor = function(){ - this.myNumber = 5 + this.myNumber = 5; } -myNewObj = new MyConstructor() // = {myNumber: 5} -myNewObj.myNumber // = 5 +myNewObj = new MyConstructor(); // = {myNumber: 5} +myNewObj.myNumber; // = 5 // Every JavaScript object has a 'prototype'. When you go to access a property // on an object that doesn't exist on the actual object, the interpreter will @@ -315,31 +315,31 @@ myNewObj.myNumber // = 5 // part of the standard; we'll get to standard ways of using prototypes later. var myObj = { myString: "Hello world!", -} +}; var myPrototype = { meaningOfLife: 42, myFunc: function(){ return this.myString.toLowerCase() } -} -myObj.__proto__ = myPrototype -myObj.meaningOfLife // = 42 +}; +myObj.__proto__ = myPrototype; +myObj.meaningOfLife; // = 42 // This works for functions, too. -myObj.myFunc() // = "hello world!" +myObj.myFunc(); // = "hello world!" // Of course, if your property isn't on your prototype, the prototype's // prototype is searched, and so on. myPrototype.__proto__ = { myBoolean: true -} -myObj.myBoolean // = true +}; +myObj.myBoolean; // = true // There's no copying involved here; each object stores a reference to its // prototype. This means we can alter the prototype and our changes will be // reflected everywhere. -myPrototype.meaningOfLife = 43 -myObj.meaningOfLife // = 43 +myPrototype.meaningOfLife = 43; +myObj.meaningOfLife; // = 43 // We mentioned that __proto__ was non-standard, and there's no standard way to // change the prototype of an existing object. However, there's two ways to @@ -347,8 +347,8 @@ myObj.meaningOfLife // = 43 // The first is Object.create, which is a recent addition to JS, and therefore // not available in all implementations yet. -var myObj = Object.create(myPrototype) -myObj.meaningOfLife // = 43 +var myObj = Object.create(myPrototype); +myObj.meaningOfLife; // = 43 // The second way, which works anywhere, has to do with constructors. // Constructors have a property called prototype. This is *not* the prototype of @@ -358,20 +358,20 @@ myConstructor.prototype = { getMyNumber: function(){ return this.myNumber } -} -var myNewObj2 = new myConstructor() -myNewObj2.getMyNumber() // = 5 +}; +var myNewObj2 = new myConstructor(); +myNewObj2.getMyNumber(); // = 5 // Built-in types like strings and numbers also have constructors that create // equivalent wrapper objects. -var myNumber = 12 -var myNumberObj = new Number(12) -myNumber == myNumberObj // = true +var myNumber = 12; +var myNumberObj = new Number(12); +myNumber == myNumberObj; // = true // Except, they aren't exactly equivalent. -typeof(myNumber) // = 'number' -typeof(myNumberObj) // = 'object' -myNumber === myNumberObj // = false +typeof(myNumber); // = 'number' +typeof(myNumberObj); // = 'object' +myNumber === myNumberObj; // = false if (0){ // This code won't execute, because 0 is falsy. } @@ -382,9 +382,9 @@ if (Number(0)){ // However, the wrapper objects and the regular builtins share a prototype, so // you can actually add functionality to a string, for instance. String.prototype.firstCharacter = function(){ - return this.charAt(0) + return this.charAt(0); } -"abc".firstCharacter() // = "a" +"abc".firstCharacter(); // = "a" // This fact is often used in "polyfilling", which is implementing newer // features of JavaScript in an older subset of JavaScript, so that they can be @@ -395,10 +395,10 @@ String.prototype.firstCharacter = function(){ if (Object.create === undefined){ // don't overwrite it if it exists Object.create = function(proto){ // make a temporary constructor with the right prototype - var Constructor = function(){} - Constructor.prototype = proto + var Constructor = function(){}; + Constructor.prototype = proto; // then use it to create a new, appropriately-prototyped object - return new Constructor() + return new Constructor(); } } ``` -- cgit v1.2.3 From 3497a0858a8aec1c0eab850b432f5a0c166df2a0 Mon Sep 17 00:00:00 2001 From: godtao Date: Thu, 15 Aug 2013 19:40:05 +0800 Subject: Add go-zh.html.markdown --- zh-cn/go-zh.html.markdown | 280 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 zh-cn/go-zh.html.markdown diff --git a/zh-cn/go-zh.html.markdown b/zh-cn/go-zh.html.markdown new file mode 100644 index 00000000..6a4c09be --- /dev/null +++ b/zh-cn/go-zh.html.markdown @@ -0,0 +1,280 @@ +--- +名字:Go +分类:编程语言 +文件名:learngo.go +贡献者: + - ["Sonia Keys", "https://github.com/soniakeys"] +翻译者: + - ["pantaovay", "https://github.com/pantaovay"] +--- + +发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流,但它却提供了解决现实问题的最新最快的方法。 + +Go拥有命令式语言的静态类型,编译很快,执行也很快,同时加入了对于目前多核CPU的并发计算支持,也有相应的特性来大规模编程。 + +Go语言有非常棒的标准库,还有一个充满热情的社区。 + +```Go +// 单行注释 +/* 多行 + 注释 */ + +// 导入包的子句在每个源文件的开头。 +// Main比较特殊,它用来声明可执行文件,而不是一个库。 +package main + +// Import语句声明了当前文件引用的包。 +import ( + "fmt" // Go语言标准库中的包 + "net/http" // 一个web服务器包 + "strconv" // 字符串转换 +) + +//函数声明:Main是程序执行的入口。不管你喜欢还是不喜欢,反正G就用了花括号来包住函数体。 +func main() { + // 往标准输出打印一行。 + // 用包名fmt限制打印函数。 + fmt.Println("Hello world!") + + // 调用当前包的另一个函数。 + beyondHello() +} + +// 函数可以在括号里加参数。 +// 如果没有参数的话,也需要一个空括号。 +func beyondHello() { + var x int // 变量声明,变量必须在使用之前声明。 + x = 3 // 变量赋值。 + // 可以用:=来偷懒,它自动把变量类型、声明和赋值都搞定了。 + y := 4 + sum, prod := learnMultiple(x, y) // 多个返回变量的函数 + fmt.Println("sum:", sum, "prod:", prod) // 简单输出 + learnTypes() // 少于y分钟,学的更多! +} + +// 多变量和多返回值的函数 +func learnMultiple(x, y int) (sum, prod int) { + return x + y, x * y // 返回两个值 +} + +// 内置变量类型和关键词 +func learnTypes() { + // 短声明给你所想。 + s := "Learn Go!" // String类型 + + s2 := `A "raw" string literal +can include line breaks.` // 同样是String类型 + + // 非ascii字符。Go使用UTF-8编码。 + g := 'Σ' // rune类型,uint32的别名,使用UTF-8编码 + + f := 3.14195 // float64类型,IEEE-754 64位浮点数 + c := 3 + 4i // complex128类型,内部使用两个float64表示 + + // Var变量可以直接初始化。 + var u uint = 7 // unsigned 无符号变量,但是实现依赖int型变量的长度 + var pi float32 = 22. / 7 + + // 字符转换 + n := byte('\n') // byte是uint8的别名 + + // 数组类型编译的时候大小固定。 + var a4 [4] int // 有4个int变量的数组,初始为0 + a3 := [...]int{3, 1, 5} // 有3个int变量的数组,同时进行了初始化 + + // Slice 有动态大小。Array和Slice各有千秋,但是使用slice的地方更多些。 + s3 := []int{4, 5, 9} // 和a3相比,这里没有省略号 + s4 := make([]int, 4) // 分配一个有4个int型变量的slice,全部被初始化为0 + + var d2 [][]float64 // 声明而已,什么都没有分配 + bs := []byte("a slice") // 类型转换的语法 + + p, q := learnMemory() // 声明p,q为int型变量的指针 + fmt.Println(*p, *q) // * 取值 + + // Map是动态可增长关联数组,和其他语言中的hash或者字典相似。 + m := map[string]int{"three": 3, "four": 4} + m["one"] = 1 + + // 在Go语言中未使用的变量在编译的时候会报错,而不是warning。 + // 下划线 _ 可以使你“使用”一个变量,但是丢弃它的值。 + _,_,_,_,_,_,_,_,_ = s2, g, f, u, pi, n, a3, s4, bs + // 输出变量 + fmt.Println(s, c, a4, s3, d2, m) + + learnFlowControl() // 回到流程控制 +} + +// Go全面支持垃圾回收。Go有指针,但是不支持指针运算。 +// 你会因为空指针而犯错,但是不会因为增加指针而犯错。 +func learnMemory() (p, q *int) { + // 返回int型变量指针p和q + p = new(int) // 内置函数new分配内存 + // 自动将分配的int赋值0,p不再是空的了。 + s := make([]int, 20) // 给20个int变量分配一块内存 + s[3] = 7 // 赋值 + r := -2 // 声明另一个局部变量 + return &s[3], &r // & 取址 +} + +func expensiveComputation() int { + return 1e6 +} + +func learnFlowControl() { + // If需要花括号,括号就免了 + if true { + fmt.Println("told ya") + } + // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了,也不用容忍被人的代码风格。 + if false { + // pout + } else { + // gloat + } + // 如果太多嵌套的if语句,推荐使用switch + x := 1 + switch x { + case 0: + case 1: + // 隐式调用break语句,匹配上一个即停止 + case 2: + // 不会运行 + } + // 和if一样,for也不用括号 + for x := 0; x < 3; x++ { // ++ 自增 + fmt.Println("iteration", x) + } + // x在这里还是1。为什么? + + // for 是go里唯一的循环关键字,不过它有很多变种 + for { // 无限循环 + break // 骗你的 + continue // 不会运行的 + } + // 和for一样,if中的:=先给y赋值,然后再和x作比较。 + if y := expensiveComputation(); y > x { + x = y + } + // 闭包函数 + xBig := func() bool { + return x > 100 // x是上面声明的变量引用 + } + fmt.Println("xBig:", xBig()) // true (上面把y赋给x了) + x /= 1e5 // x变成10 + fmt.Println("xBig:", xBig()) // 现在是false + + // 当你需要goto的时候,你会爱死它的! + goto love +love: + + learnInterfaces() // 好东西来了! +} + +// 定义Stringer为一个接口类型,有一个方法String +type Stringer interface { + String() string +} + +// 定义pair为一个结构体,有x和y两个int型变量。 +type pair struct { + x, y int +} + +// 定义pair类型的方法,实现Stringer接口。 +func (p pair) String() string { // p被叫做“接收器” + // Sprintf是fmt包中的另一个公有函数。 + // 用 . 调用p中的元素。 + return fmt.Sprintf("(%d, %d)", p.x, p.y) +} + +func learnInterfaces() { + // 花括号用来定义结构体变量,:=在这里将一个结构体变量赋值给p。 + p := pair{3, 4} + fmt.Println(p.String()) // 调用pair类型p的String方法 + var i Stringer // 声明i为Stringer接口类型 + i = p // 有效!因为p实现了Stringer接口(类似java中的塑型) + // 调用i的String方法,输出和上面一样 + fmt.Println(i.String()) + + // fmt包中的Println函数向对象要它们的string输出,实现了String方法就可以这样使用了。(类似java中的序列化) + fmt.Println(p) // 输出和上面一样,自动调用String函数。 + fmt.Println(i) // 输出和上面一样。 + + learnErrorHandling() +} + +func learnErrorHandling() { + // ", ok"用来判断有没有正常工作 + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // ok 为false,因为m中没有1 + fmt.Println("no one there") + } else { + fmt.Print(x) // 如果x在map中的话,x就是那个值喽。 + } + // 错误可不只是ok,它还可以给出关于问题的更多细节。 + if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value + // 输出"strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } + // 待会再说接口吧。同时, + learnConcurrency() +} + +// c是channel类型,一个并发安全的通信对象。 +func inc(i int, c chan int) { + c <- i + 1 // <-把右边的发送到左边的channel。 +} + +// 我们将用inc函数来并发地增加一些数字。 +func learnConcurrency() { + // 用make来声明一个slice,make会分配和初始化slice,map和channel。 + c := make(chan int) + // 用go关键字开始三个并发的goroutine,如果机器支持的话,还可能是并行执行。三个都被发送到同一个channel。 + go inc(0, c) // go is a statement that starts a new goroutine. + go inc(10, c) + go inc(-805, c) + // 从channel中独处结果并打印。 + // 打印出什么东西是不可预知的。 + fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是接收操作。 + + cs := make(chan string) // 操作string的channel + cc := make(chan chan string) // 操作channel的channel + go func() { c <- 84 }() // 开始一个goroutine来发送一个新的数字 + go func() { cs <- "wordy" }() // 发送给cs + // Select类似于switch,但是每个case包括一个channel操作。它随机选择一个准备好通讯的case。 + select { + case i := <-c: // 从channel接收的值可以赋给其他变量 + fmt.Println("it's a", i) + case <-cs: // 或者直接丢弃 + fmt.Println("it's a string") + case <-cc: // 空的,还没作好通讯的准备 + fmt.Println("didn't happen.") + } + // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个保持阻塞。 + + learnWebProgramming() // Go很适合web编程,我知道你也想学! +} + +// http包中的一个简单的函数就可以开启web服务器。 +func learnWebProgramming() { + // ListenAndServe第一个参数指定了监听端口,第二个参数是一个接口,特定是http.Handler。 + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // 不要无视错误。 +} + +// 使pair实现http.Handler接口的ServeHTTP方法。 +func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // 使用http.ResponseWriter返回数据 + w.Write([]byte("You learned Go in Y minutes!")) +} +``` + +## 更进一步 + +Go的根源在[Go官方网站](http://golang.org/)。 +在那里你可以学习入门教程,通过浏览器交互式地学习,而且可以读到很多东西。 + +强烈推荐阅读语言定义部分,很简单而且很简洁!(as language definitions go these days.) + +学习Go还要阅读Go标准库的源代码,全部文档化了,可读性非常好,可以学到go,go style和go idioms。在文档中点击函数名,源代码就出来了! -- cgit v1.2.3 From dffa1b4f7426b61af77b0a13954c0fe49eb4a578 Mon Sep 17 00:00:00 2001 From: Tao Pan Date: Thu, 15 Aug 2013 19:44:39 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zh-cn/go-zh.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zh-cn/go-zh.html.markdown b/zh-cn/go-zh.html.markdown index 6a4c09be..25fd1f03 100644 --- a/zh-cn/go-zh.html.markdown +++ b/zh-cn/go-zh.html.markdown @@ -4,13 +4,12 @@ 文件名:learngo.go 贡献者: - ["Sonia Keys", "https://github.com/soniakeys"] -翻译者: - ["pantaovay", "https://github.com/pantaovay"] --- 发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流,但它却提供了解决现实问题的最新最快的方法。 -Go拥有命令式语言的静态类型,编译很快,执行也很快,同时加入了对于目前多核CPU的并发计算支持,也有相应的特性来大规模编程。 +Go拥有命令式语言的静态类型,编译很快,执行也很快,同时加入了对于目前多核CPU的并发计算支持,也有相应的特性来实现大规模编程。 Go语言有非常棒的标准库,还有一个充满热情的社区。 -- cgit v1.2.3 From e0b44e5aad796fe6c08cfee22f8fcfe84703250d Mon Sep 17 00:00:00 2001 From: Yury Date: Thu, 15 Aug 2013 23:47:46 +0400 Subject: Russian translate for python --- ru-ru/python-ru.html.markdown | 486 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 486 insertions(+) create mode 100644 ru-ru/python-ru.html.markdown diff --git a/ru-ru/python-ru.html.markdown b/ru-ru/python-ru.html.markdown new file mode 100644 index 00000000..58b0adcc --- /dev/null +++ b/ru-ru/python-ru.html.markdown @@ -0,0 +1,486 @@ +--- +language: python +contributors: + - ["Yury Timofeev", "http://twitter.com/gagar1n"] +filename: learnpython-ru.py +--- + +Язык Python был создан Гвидо ван Россумом в ранние 90-е. Сегодня это один из самых популярных +языков. Я влюбился в него благодаря его понятному и доходчивому синтаксису - это почти что исполняемый псевдокод. + +Обратная связь будет высоко оценена! Вы можете связаться со мной: [@louiedinh](http://twitter.com/louiedinh) или louiedinh [at] [google's email service] + +Замечание: Эта статья относится к Python 2.7, но должна быть применима к Python 2.x. Скоро ожидается версия и для Python 3! + +```python +# Однострочные комментарии начинаются с hash-символа. +""" Многострочный текст может быть + записан, используя 3 знака " и обычно используется + в качестве комментария +""" + +#################################################### +## 1. Примитивные типы данных и операторв +#################################################### + +# У вас есть числа +3 #=> 3 + +# Математика работает так, как вы и думаете +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Деление немного сложнее. Это деление целых чисел и результат +# автоматически округляется в меньшую сторону. +5 / 2 #=> 2 + +# Чтобы научиться делить, сначала нужно немного узнать о дробных числах. +2.0 # Это дробное число. +11.0 / 4.0 #=> 2.75 вооот... гораздо лучше + +# Приоритет операций указывается скобками +(1 + 3) * 2 #=> 8 + +# Логические значения являются примитивами +True +False + +# Для отрицания используется ключевое слово not +not True #=> False +not False #=> True + +# Равенство это == +1 == 1 #=> True +2 == 1 #=> False + +# Неравенство это != +1 != 1 #=> False +2 != 1 #=> True + +# Больше сравнений +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +# Сравнения могут быть соединены в цепь! +1 < 2 < 3 #=> True +2 < 3 < 2 #=> False + +# Строки создаются при символом " или ' +"Это строка." +'Это тоже строка.' + +# Строки тоже могут складываться! +"Привет " + "мир!" #=> "Привет мир!" + +# Со строкой можно работать как со списком символов +"Это строка"[0] #=> 'Э' + +# % используется для форматирования строк, например: +"%s могут быть %s" % ("строки", "интерполированы") + +# Новый метод форматирования строк - использование метода format. +# Это предпочитаемый способ. +"{0} могут быть {1}".format("строки", "форматированы") +# Вы можете использовать ключевые слова, если не хотите считать. +"{name} хочет есть {food}".format(name="Боб", food="лазанью") + +# None является объектом +None #=> None + +# Не используйте оператор равенства `==` для сравнения +# объектов с None. Используйте для этого `is` +"etc" is None #=> False +None is None #=> True + +# Оператор 'is' проверяет идентичность объектов. Он не +# очень полезен при работе с примитивными типами, но +# очень полезен при работе с объектами. + +# None, 0, и пустые строки/списки равны False. +# Все остальные значения равны True +0 == False #=> True +"" == False #=> True + + +#################################################### +## 2. Переменные и коллекции +#################################################### + +# Печать довольно проста +print "Я Python. Приятно познакомиться!" + + +# Необязательно объявлять переменные перед присваиванием им значения. +some_var = 5 # По соглашению используется нижний_регистр_с_подчеркиваниями +some_var #=> 5 + +# При попытке доступа к переменной, которой не было ранее присвоено значение, +# выбрасывается исключение. +# См. раздел "Поток управления" для информации об исключениях. +some_other_var # Выбрасывает ошибку именования + +# if может быть использован как выражение +"yahoo!" if 3 > 2 else 2 #=> "yahoo!" + +# Списки хранят последовательности +li = [] +# Можно сразу начать с заполненным списком +other_li = [4, 5, 6] + +# Объекты добавляются в конец списка методом append +li.append(1) #li содержит [1] +li.append(2) #li содержит [1, 2] +li.append(4) #li содержит [1, 2, 4] +li.append(3) #li содержит [1, 2, 4, 3] +# Удаляются с конца методом pop +li.pop() #=> 3 и li содержит [1, 2, 4] +# Положим его обратно +li.append(3) # li содержит [1, 2, 4, 3] опять. + +# Обращайтесь со списком, как с обычным массивом +li[0] #=> 1 +# Посмотрим на последний элемент +li[-1] #=> 3 + +# Попытка выйти за границы массива приводит к IndexError +li[4] # Выдает IndexError + +# Можно обращаться к диапазону, используя "кусочный синтаксис" (slice syntax) +# (Для тех из вас, кто любит математику, это замкнуто/открытый интервал.) +li[1:3] #=> [2, 4] +# Опускаем начало +li[2:] #=> [4, 3] +# Опускаем конец +li[:3] #=> [1, 2, 4] + +# Удаляем произвольные элементы из списка оператором del +del li[2] # li содержит [1, 2, 3] + +# Вы можете складывать списки +li + other_li #=> [1, 2, 3, 4, 5, 6] - ЗАмечание: li и other_li остаются нетронутыми + +# Конкатенировать списки можно методом extend +li.extend(other_li) # Теперь li содержит [1, 2, 3, 4, 5, 6] + +# Проверять элемент на вхождение на список оператором in +1 in li #=> True + +# Длина списка вычисляется при помощи len +len(li) #=> 6 + + +# Кортежи - это как списки, только неизменяемые +tup = (1, 2, 3) +tup[0] #=> 1 +tup[0] = 3 # Выдает TypeError + +# Все те же штуки можно делать и с кортежами +len(tup) #=> 3 +tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) +tup[:2] #=> (1, 2) +2 in tup #=> True + +# Вы можете распаковывать кортежи (или списки) в переменные +a, b, c = (1, 2, 3) # a теперь равно 1, b равно 2 и c равно 3 +# Кортежи создаются по умолчанию, если опущены скобки +d, e, f = 4, 5, 6 +# Обратите внимание, как легко поменять местами значения двух переменных +e, d = d, e # d теперь равно 5 and e равно 4 + + +# Словари содержат ассоциативные массивы +empty_dict = {} +# Вот так описывается предзаполненный словарь +filled_dict = {"one": 1, "two": 2, "three": 3} + +# Значения ищутся по ключу с помощью оператора [] +filled_dict["one"] #=> 1 + +# Можно получить все ключи в виде списка +filled_dict.keys() #=> ["three", "two", "one"] +# Замечание - сохранение порядка ключей в словаре не гарантируется +# Ваши результаты могут не совпадать с этими. + +# Можно получить и все значения в виде списка +filled_dict.values() #=> [3, 2, 1] +# Замечание - то же самое, что и выше, насчет порядка ключей + +# При помощи оператора in можно проверять ключи на вхождение в словарь +"one" in filled_dict #=> True +1 in filled_dict #=> False + +# Попытка получить значение по несуществующему ключу выбросит KeyError +filled_dict["four"] # KeyError + +# Чтобы избежать этого, используйте метод get +filled_dict.get("one") #=> 1 +filled_dict.get("four") #=> None +# Метод get также принимает аргумент default, значение которого будет возвращено при отсутствии указанного ключа +filled_dict.get("one", 4) #=> 1 +filled_dict.get("four", 4) #=> 4 + +# Метод setdefault - это безопасный способ добавить новую пару ключ-значение в словарь +filled_dict.setdefault("five", 5) #filled_dict["five"] возвращает 5 +filled_dict.setdefault("five", 6) #filled_dict["five"] по прежнему возвращает 5 + + +# Множества содержат... ну, в общем, множества +empty_set = set() +# Инициализация множества набором значений +some_set = set([1,2,2,3,4]) # some_set теперь равно set([1, 2, 3, 4]) + +# Начиная с Python 2.7, вы можете использовать {} чтобы обьявить множество +filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} + +# Добавление новых элементов в множество +filled_set.add(5) # filled_set равно {1, 2, 3, 4, 5} + +# Пересечение множеств: & +other_set = {3, 4, 5, 6} +filled_set & other_set #=> {3, 4, 5} + +# Объединение множеств: | +filled_set | other_set #=> {1, 2, 3, 4, 5, 6} + +# Разность множеств: - +{1,2,3,4} - {2,3,5} #=> {1, 4} + +# Проверка на вхождение во множество: in +2 in filled_set #=> True +10 in filled_set #=> False + + +#################################################### +## 3. Поток управления +#################################################### + +# Давайте заведем переменную +some_var = 5 + +# Так выглядит выражение if. Отступы в python очень важны! +# результат: "some_var меньше, чем 10" +if some_var > 10: + print "some_var намного больше, чем 10." +elif some_var < 10: # Выражение elif необязательно. + print "some_var меньше, чем 10." +else: # Это тоже необязательно. + print "some_var равно 10." + + +""" +Циклы For проходят по циклам +результат: + собака это млекопитающее + кошка это млекопитающее + мышь это млекопитающее +""" +for animal in ["собака", "кошка", "мышь"]: + # Можете использовать оператор % для интерполяции форматированных строк + print "%s это млекопитающее" % animal + +""" +`range(number)` возвращает список чисел +от нуля до заданного числа +результат: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i + +""" +Циклы while продолжаются до тех пор, пока указанное условие не станет ложным. +результат: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print x + x += 1 # То же самое, что x = x + 1 + +# Обрабывайте исключения блоками try/except + +# Работает в Python 2.6 и выше: +try: + # Для выбора ошибки используется raise + raise IndexError("Это IndexError") +except IndexError as e: + pass # pass это просто отсутствие оператора. Обычно здесь происходит восстановление от ошибки. + + +#################################################### +## 4. Функции +#################################################### + +# Используйте def для создания новых функций +def add(x, y): + print "x равен %s, а y равен %s" % (x, y) + return x + y # Возвращайте результат выражением return + +# Вызов функции с аргументами +add(5, 6) #=> prints out "x равен 5, а y равен 6" и возвращает 11 + +# Другой способ вызова функции с аргументами +add(y=6, x=5) # Именованные аргументы можно указывать в любом порядке. + +# Вы можете определить функцию, принимающую неизвестное количество аргументов +def varargs(*args): + return args + +varargs(1, 2, 3) #=> (1,2,3) + + +# А также можете определить функцию, принимающую изменяющееся количество +# именованных аргументов +def keyword_args(**kwargs): + return kwargs + +# Вызовем эту функцию и посмотрим, что из этого получится +keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} + +# Если хотите, можете использовать оба способа одновременно +def all_the_args(*args, **kwargs): + print args + print kwargs +""" +all_the_args(1, 2, a=3, b=4) выводит: + (1, 2) + {"a": 3, "b": 4} +""" + +# Вызывая функции, можете сделать наоборот! +# Используйте символ * для передачи кортежей и ** для передачи словарей +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +all_the_args(*args) # эквивалент foo(1, 2, 3, 4) +all_the_args(**kwargs) # эквивалент foo(a=3, b=4) +all_the_args(*args, **kwargs) # эквивалент foo(1, 2, 3, 4, a=3, b=4) + +# Python имеет функции первого класса +def create_adder(x): + def adder(y): + return x + y + return adder + +add_10 = create_adder(10) +add_10(3) #=> 13 + +# Также есть и анонимные функции +(lambda x: x > 2)(3) #=> True + +# Есть встроенные функции высшего порядка +map(add_10, [1,2,3]) #=> [11, 12, 13] +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] + +# Мы можем использовать списки для удобного отображения и фильтрации +[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] +[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] + +#################################################### +## 5. Классы +#################################################### + +# Чтобы получить класс, мы наследуемся от object. +class Human(object): + + # Атрибут класса. Он разделяется всеми экземплярами этого класса + species = "H. sapiens" + + # Обычный конструктор + def __init__(self, name): + # Присваивание значения аргумента атрибуту класса name + self.name = name + + # Метод экземпляра. Все методы принимают self в качестве первого аргумента + def say(self, msg): + return "%s: %s" % (self.name, msg) + + # Метод класса разделяется между всеми экземплярами + # Они вызываются с указыванием вызывающего класса в качестве первого аргумента + @classmethod + def get_species(cls): + return cls.species + + # Статический метод вызывается без ссылки на класс или экземпляр + @staticmethod + def grunt(): + return "*grunt*" + + +# Инстанцирование класса +i = Human(name="Иван") +print i.say("привет") # выводит "Иван: привет" + +j = Human("Петр") +print j.say("Привет") #выводит "Петр: привет" + +# Вызов метода класса +i.get_species() #=> "H. sapiens" + +# Присвоение разделяемому атрибуту +Human.species = "H. neanderthalensis" +i.get_species() #=> "H. neanderthalensis" +j.get_species() #=> "H. neanderthalensis" + +# Вызов статического метода +Human.grunt() #=> "*grunt*" + + +#################################################### +## 6. Модули +#################################################### + +# Вы можете импортировать модули +import math +print math.sqrt(16) #=> 4 + +# Вы можете импортировать отдельные функции модуля +from math import ceil, floor +print ceil(3.7) #=> 4.0 +print floor(3.7) #=> 3.0 + +# Можете импортировать все функции модуля. +# Предупреждение: не рекомендуется +from math import * + +# Можете сокращать имена модулей +import math as m +math.sqrt(16) == m.sqrt(16) #=> True + +# Модули в Python это обычные файлы с кодом python. Вы +# можете писать свои модули и импортировать их. Название +# модуля совпадает с названием файла. + +# Вы можете узнать, какие функции и атрибуты определены +# в модуле +import math +dir(math) + + +``` + +## Хочется большего? + +### Бесплатные онлайн-материалы + +* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +* [Dive Into Python](http://www.diveintopython.net/) +* [The Official Docs](http://docs.python.org/2.6/) +* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) +* [Python Module of the Week](http://pymotw.com/2/) + +### Готовьте деньги + +* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) +* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) +* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) + -- cgit v1.2.3 From cf5730cebc5756e5b5cb18e93fe53092c3d6a036 Mon Sep 17 00:00:00 2001 From: Yury Date: Fri, 16 Aug 2013 00:21:28 +0400 Subject: fixed typo in C spanish version adressed in #230 --- es-es/c-es.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/es-es/c-es.html.markdown b/es-es/c-es.html.markdown index b109f761..5d3aae0c 100644 --- a/es-es/c-es.html.markdown +++ b/es-es/c-es.html.markdown @@ -284,7 +284,7 @@ for (xx=0; xx<20; xx++) { // impredecibles printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? -// Cuando hallas acabado con el bloque de memoría malloc, necesitas +// Cuando hayas acabado con el bloque de memoría malloc, necesitas // liberarlo o sino nadie más podrá usarlo hasta que tu programa se cierre free(my_ptr); -- cgit v1.2.3 From 50ac50f05f82c3b3069329634f373c1ba55263c0 Mon Sep 17 00:00:00 2001 From: Seva Baskin Date: Thu, 15 Aug 2013 23:25:51 +0100 Subject: Update objective-c.html.markdown Fixed a few typos --- objective-c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 2b1b3c67..b92e3218 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -160,7 +160,7 @@ int main (int argc, const char * argv[]) int jj; for (jj=0; jj < 4; jj++) { - NSLog(@"%d,", ii++); + NSLog(@"%d,", jj++); } // => prints "0," // "1," // "2," @@ -256,7 +256,7 @@ int main (int argc, const char * argv[]) } // Constructors are a way of creating classes -// This is a default constructor which is call when the object is creating +// This is a default constructor which is called when the object is creating - (id)init { if ((self = [super init])) -- cgit v1.2.3 From 91db568e0dec9f062bab03d4643f59356ed5d574 Mon Sep 17 00:00:00 2001 From: sergiokas Date: Thu, 15 Aug 2013 19:29:27 -0300 Subject: Fixed typo Fixed a typo. Nice tutorial, by the way. --- ruby-ecosystem.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index cae55cd3..54c1d178 100644 --- a/ruby-ecosystem.html.markdown +++ b/ruby-ecosystem.html.markdown @@ -81,7 +81,7 @@ development. Less mature/compatible: * Topaz - Written in RPython (using the PyPy toolchain), Topaz is fairly young - and not yet compatable. It shows promise to be a high-performance ruby + and not yet compatible. It shows promise to be a high-performance ruby implementation. * IronRuby - Written in C# targeting the .NET platform, work on IronRuby seems to have stopped since Microsoft pulled their support. -- cgit v1.2.3 From 43d602b0c0ca473421cda2c43fd87f76cf1620e7 Mon Sep 17 00:00:00 2001 From: rduerig Date: Fri, 16 Aug 2013 12:37:40 +0200 Subject: Update java.html.markdown added a reference to a book --- java.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/java.html.markdown b/java.html.markdown index b4531635..cdcf620c 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -405,3 +405,7 @@ Other Topics To Research: * [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html) * [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html) + +Books: + +* [Head First Java] (http://www.headfirstlabs.com/books/hfjava/) -- cgit v1.2.3 From 41be9b284306ea70d44061fcbca1c71b8f201a97 Mon Sep 17 00:00:00 2001 From: Astynax84 Date: Fri, 16 Aug 2013 15:25:10 +0400 Subject: Russian translation of clojure.html.markdown --- ru-ru/clojure-ru.html.markdown | 425 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 425 insertions(+) create mode 100644 ru-ru/clojure-ru.html.markdown diff --git a/ru-ru/clojure-ru.html.markdown b/ru-ru/clojure-ru.html.markdown new file mode 100644 index 00000000..9bc32856 --- /dev/null +++ b/ru-ru/clojure-ru.html.markdown @@ -0,0 +1,425 @@ +--- +language: clojure +filename: learnclojure-ru.clj +contributors: + - ["Adam Bard", "http://adambard.com/"] + - ["Alexey Pirogov", "http://twitter.com/alex_pir"] +--- + +Clojure, это представитель семейства Lisp-подобных языков, разработанный +для Java Virtual Machine. Язык идейно гораздо ближе к чистому +[функциональному программированию](https://ru.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%BE%D0%BD%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5) чем его прародитель Common Lisp, но в то же время обладает набором инструментов для работы с состоянием, +таких как [STM](https://ru.wikipedia.org/wiki/Software_transactional_memory). + +Благодаря такому сочетанию технологий в одном языке, разработка программ, +предполагающих конкурентное выполнение, значительно упрощается +и даже может быть автоматизирована. + +(Последующие примеры кода предполагают выполнение в Clojure версии 1.2 и выше) + + +```clojure +; Комментарии начинаются символом ";". + +; Код на языке Clojure записывается в виде "форм", +; которые представляют собой обычные списки элементов, разделенных пробелами, +; заключённые в круглые скобки +; +; Clojure Reader (инструмент языка, отвечающий за чтение исходного кода), +; анализируя форму, предполагает, что первым элементом формы (т.е. списка) +; является функция или макрос, который следует вызвать, передав ему +; в качестве аргументов остальные элементы списка-формы. + +; Первым вызовом в файле должен быть вызов функции ns, +; которая отвечает за выбор текущего пространства имен (namespace) +(ns learnclojure-ru) + +; Несколько простых примеров: + +; str объединяет в единую строку все свои аргументы +(str "Hello" " " "World") ; => "Hello World" + +; Арифметика тоже выглядит несложно +(+ 1 1) ; => 2 +(- 2 1) ; => 1 +(* 1 2) ; => 2 +(/ 2 1) ; => 2 + +; Проверка на равенство (Equality) +(= 1 1) ; => true +(= 2 1) ; => false + +; Для булевой логики вам может понадобиться not +(not true) ; => false + +; Вложенные формы, конечно же, допустимы и работают вполне предсказуемо +(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2 + +; Типы +;;;;;;;;;;;;; + +; Clojure использует типы Java для представления булевых значений, +; строк и чисел +; Узнать тип мы можем, использую функцию `class +(class 1) ; Целочисленные литералы типа по-умолчанию являются java.lang.Long +(class 1.) ; Числа с плавающей точкой, это java.lang.Double +(class "") ; Строки всегда заключаются в двойные кавычки + ; и представляют собой java.lang.String +(class false) ; Булевы значения, это экземпляры java.lang.Boolean +(class nil) ; "Пустое" значение называется "nil" + +; Если Вы захотите создать список из чисел, вы можете просто +; предварить форму списка символом "'", который подскажет Reader`у, +; что эта форма не требует вычисления +'(+ 1 2) ; => (+ 1 2) +; ("'", это краткая запись формы (quote (+ 1 2)) + +; "Квотированный" список можно вычислить, передав его функции eval +(eval '(+ 1 2)) ; => 3 + +; Коллекции и Последовательности +;;;;;;;;;;;;;;;;;;; + +; Списки (Lists) в clojure структурно представляют собой "связанные списки", +; тогда как Векторы (Vectors), устроены как массивы. +; Векторы и Списки тоже являются классами Java! +(class [1 2 3]); => clojure.lang.PersistentVector +(class '(1 2 3)); => clojure.lang.PersistentList + +; Список может быть записан, как (1 2 3), но в этом случае +; он будет воспринят reader`ом, как вызов функции. +; Есть два способа этого избежать: +; '(1 2 3) - квотирование, +; (list 1 2 3) - явное конструирование списка с помощью функции list. + +; "Коллекции", это некие наборы данных +; И списки, и векторы являются коллекциями: +(coll? '(1 2 3)) ; => true +(coll? [1 2 3]) ; => true + +; "Последовательности" (seqs), это абстракция над наборами данных, +; элементы которых "упакованы" последовательно. +; Списки - последовательности, а вектора - нет. +(seq? '(1 2 3)) ; => true +(seq? [1 2 3]) ; => false + +; Любая seq предоставляет доступ только к началу последовательности данных, +; не предоставляя информацию о её длине. +; При этом последовательности могут быть и бесконечными, +; т.к. являются ленивыми и предоставляют данные только по требованию! +(range 4) ; => (0 1 2 3) +(range) ; => (0 1 2 3 4 ...) (бесконечная последовательность!) +(take 4 (range)) ; (0 1 2 3) + +; Добавить элемент в начало списка или вектора можно с помощью функции cons +(cons 4 [1 2 3]) ; => (4 1 2 3) +(cons 4 '(1 2 3)) ; => (4 1 2 3) + +; Функция conj добавляет элемент в коллекцию +; максимально эффективным для неё способом. +; Для списков эффективно добавление в начло, а для векторов - в конец. +(conj [1 2 3] 4) ; => [1 2 3 4] +(conj '(1 2 3) 4) ; => (4 1 2 3) + +; Функция concat объединяет несколько списков и векторов в единый список +(concat [1 2] '(3 4)) ; => (1 2 3 4) + +; Работать с коллекциями удобно с помощью функций filter и map +(map inc [1 2 3]) ; => (2 3 4) +(filter even? [1 2 3]) ; => (2) + +; reduce поможет "свернуть" коллекцию +(reduce + [1 2 3 4]) +; = (+ (+ (+ 1 2) 3) 4) +; => 10 + +; Вызывая reduce, мы можем указать начальное значение +(reduce conj [] '(3 2 1)) +; = (conj (conj (conj [] 3) 2) 1) +; => [3 2 1] + +; Функции +;;;;;;;;;;;;;;;;;;;;; + +; Функция создается специальной формой fn. +; "Тело"" функции может состоять из нескольких форм, +; но результатом вызова функции всегда будет результат вычисления +; последней из них. +(fn [] "Hello World") ; => fn + +; (Вызов функции требует "оборачивания" fn-формы в форму вызова) +((fn [] "Hello World")) ; => "Hello World" + +; Назначить значению имя можно специальной формой def +(def x 1) +x ; => 1 + +; Назначить имя можно любому значению, в т.ч. и функции: +(def hello-world (fn [] "Hello World")) +(hello-world) ; => "Hello World" + +; Поскольку именование функций - очень частая операция, +; clojure позволяет, сделать это проще: +(defn hello-world [] "Hello World") + +; Вектор [] в форме описания функции, следующий сразу за именем, +; описывает параметры функции: +(defn hello [name] + (str "Hello " name)) +(hello "Steve") ; => "Hello Steve" + +; Одна функция может иметь сразу несколько наборов аргументов: +(defn hello3 + ([] "Hello World") + ([name] (str "Hello " name))) +(hello3 "Jake") ; => "Hello Jake" +(hello3) ; => "Hello World" + +; Также функция может иметь набор аргументов переменной длины +(defn count-args [& args] ; args будет содержать seq аргументов + (str "You passed " (count args) " args: " args)) +(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)" + +; Можно комбинировать оба подхода задания аргументов +(defn hello-count [name & args] + (str "Hello " name ", you passed " (count args) " extra args")) +(hello-count "Finn" 1 2 3) +; => "Hello Finn, you passed 3 extra args" + +; Для создания анонимных функций есть специальный синтаксис: +; функциональные литералы +(def hello2 #(str "Hello " %1)) +(hello2 "Fanny") ; => "Hello Fanny" + +; такие функциональные литералы удобно использовать с map, filter и reduce +(map #(* 10 %1) [1 2 3 5]) ; => (10 20 30 50) +(filter #(> %1 3) [1 2 3 4 5 6 7]) ; => (4 5 6 7) +(reduce #(str %1 "," %2) [1 2 3 4]) ; => "1,2,3,4" + +; Отображения (Maps) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; Hash maps и array maps имеют одинаковый интерфейс. +; Hash maps производят поиск по ключу быстрее, но не сохраняют порядок ключей +(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap +(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap + +; Array maps автоматически преобразуются в hash maps, +; как только разрастутся до определенного размера + +; Отображения могут использовать в качестве ключей любые хэшируемые значения, +; однако предпочтительными являются ключи, +; являющиеся "ключевыми словами" (keywords) +(class :a) ; => clojure.lang.Keyword + +(def stringmap {"a" 1, "b" 2, "c" 3}) +stringmap ; => {"a" 1, "b" 2, "c" 3} + +(def keymap {:a 1, :b 2, :c 3}) +keymap ; => {:a 1, :c 3, :b 2} + +; Предыдущий пример содержит запятые в коде, однако reader не использует их, +; при обработке литералов - запятые просто воспринимаются, +; как "пробельные символы" (whitespaces) + +; Отображение может выступать в роли функции, возвращающей значение по ключу +(stringmap "a") ; => 1 +(keymap :a) ; => 1 + +; При попытке получить отсутствующее значение, будет возвращён nil +(stringmap "d") ; => nil + +; Иногда бывает удобно указать конкретное значение по-умолчанию: +({:a 1 :b 2} :c "Oops!") ; => "Oops!" + +; Keywords тоже могут использоваться в роли функций! +(:b keymap) ; => 2 + +; Однако этот фокус не пройдёт со строками. +;("a" stringmap) +; => Exception: java.lang.String cannot be cast to clojure.lang.IFn + +; Добавить пару ключ-значение в отображение можно функцией assoc +(def newkeymap (assoc keymap :d 4)) +newkeymap ; => {:a 1, :b 2, :c 3, :d 4} + +; Но всегда следует помнить, что значения в Clojure - неизменяемые! +keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут + +; dissoc позволяет исключить значение по ключу +(dissoc keymap :a :b) ; => {:c 3} + +; Множества (Sets) +;;;;;;;;;;;;;;;;;; + +(class #{1 2 3}) ; => clojure.lang.PersistentHashSet +(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3} + +; Добавляются элементы посредством conj +(conj #{1 2 3} 4) ; => #{1 2 3 4} + +; Исключаются - посредством disj +(disj #{1 2 3} 1) ; => #{2 3} + +; Вызов множества, как функции, позволяет проверить +; принадлежность элемента этому множеству: +(#{1 2 3} 1) ; => 1 +(#{1 2 3} 4) ; => nil + +; В пространстве имен clojure.sets +; содержится множество функций для работы с множествами + +; Полезные формы +;;;;;;;;;;;;;;;;; + +; Конструкции ветвления в clojure, это обычные макросы +; и подобны их собратьям в других языках: +(if false "a" "b") ; => "b" +(if false "a") ; => nil + +; Специальная форма let позволяет присвоить имена значениям локально. +; При этом все изменения будут видны только вложенным формам: +(def a 10) +(let [a 1 b 2] + (> a b)) ; => false + +; Несколько форм можно объединить в одну форму посредством do +; Значением do-формы будет значение последней формы из списка вложенных в неё: +(do + (print "Hello") + "World") ; => "World" (prints "Hello") + +; Множество макросов содержит внутри себя неявную do-форму. +; Пример - макрос определения функции: +(defn print-and-say-hello [name] + (print "Saying hello to " name) + (str "Hello " name)) +(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff") + +; Ещё один пример - let: +(let [name "Urkel"] + (print "Saying hello to " name) + (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel") + +; Модули +;;;;;;;;; + +; Форма "use" позволяет добавить в текущее пространство имен +; все имена (вместе со значениями) из указанного модуля: +(use 'clojure.set) + +; Теперь нам доступны операции над множествами: +(intersection #{1 2 3} #{2 3 4}) ; => #{2 3} +(difference #{1 2 3} #{2 3 4}) ; => #{1} + +; use позволяет указать, какие конкретно имена +; должны быть импортированы из модуля: +(use '[clojure.set :only [intersection]]) + +; Также модуль может быть импортирован формой require +(require 'clojure.string) + +; После этого модуль становится доступе в текущем пространстве имен, +; а вызов его функций может быть осуществлен указанием полного имени функции: +(clojure.string/blank? "") ; => true + +; Импортируемому модулю можно назначить короткое имя: +(require '[clojure.string :as str]) +(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst." +; (Литерал вида #"" обозначает регулярное выражение) + +; Вместо отдельной формы require (и use, хотя это и не приветствуется) можно +; указать необходимые модули прямо в форме ns: +(ns test + (:require + [clojure.string :as str] ; Внимание: при указании внутри формы ns + [clojure.set :as set])) ; имена пакетов не квотируются! + +; Java +;;;;;;; + +; Стандартная библиотека Java очень богата, +; и всё это богатство доступно и для Clojure! + +; import позволяет импортировать модули Java +(import java.util.Date) + +; В том числе и из ns +(ns test + (:import java.util.Date + java.util.Calendar)) + +; Имя класса, сопровождаемое символом "." позволяет +; инстанцировать объекты Java-классов: +(Date.) ; + +; форма . позволяет вызывать методы: +(. (Date.) getTime) ; +(.getTime (Date.)) ; а можно и так + +; Статические методы вызываются как функции модуля: +(System/currentTimeMillis) ; (Модуль system всегда доступен!) + +; doto позволяет удобно работать с объектами, изменяющими свое состояние +(import java.util.Calendar) +(doto (Calendar/getInstance) + (.set 2000 1 1 0 0 0) + .getTime) ; => A Date. set to 2000-01-01 00:00:00 + +; Работа с изменяемым сотоянием +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; Clojure предоставляет набор инструментов +; для работы с изменяемым состоянием: Software Transactional Memory. +; Структуры STM представлены тремя типами: +; - атомы (atoms) +; - агенты (agents) +: - ссылки (references) + +; Самые простые хранители состояния - атомы: +(def my-atom (atom {})) ; {} - начальное состояние атома + +; Обновляется атом посредством swap!. +; swap! применяет функцию аргумент к текущему значению +; атома и помещает в атом результат +(swap! my-atom assoc :a 1) ; Обновляет my-atom, помещая в него (assoc {} :a 1) +(swap! my-atom assoc :b 2) ; Обновляет my-atom, помещая в него (assoc {:a 1} :b 2) + +; Получить значение атома можно посредством '@' +; (провести так называемую операцию dereference) +my-atom ;=> Atom<#...> (Возвращает объект типа Atom) +@my-atom ; => {:a 1 :b 2} + +; Пример реализации счётчика на атоме +(def counter (atom 0)) +(defn inc-counter [] + (swap! counter inc)) + +(inc-counter) +(inc-counter) +(inc-counter) +(inc-counter) +(inc-counter) + +@counter ; => 5 + +; С другими STM-конструкциями - refs и agents - можно ознакомиться тут: +; Refs: http://clojure.org/refs +; Agents: http://clojure.org/agents +``` + +### Для будущего чтения + +Это руководство не претендует на полноту, но мы смеем надеяться, способно вызвать интерес к дальнейшему изучению языка. + +Clojure.org - сайт содержит большое количество статей по языку: +[http://clojure.org/](http://clojure.org/) + +Clojuredocs.org - сайт документации языка с примерами использования функций: +[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core) + +4Clojure - отличный способ закрепить навыки программирования на clojure, решая задачи вместе с коллегами со всего мира: +[http://www.4clojure.com/](http://www.4clojure.com/) + +Clojure-doc.org (да, именно) неплохой перечень статей для начинающих: +[http://clojure-doc.org/](http://clojure-doc.org/) -- cgit v1.2.3 From 0746979b8f1cf801cb22b6a5fff66b4caf6ca6d4 Mon Sep 17 00:00:00 2001 From: Astynax84 Date: Fri, 16 Aug 2013 15:28:16 +0400 Subject: fixed typos in russian translation of clojure.html.markdown --- ru-ru/clojure-ru.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ru-ru/clojure-ru.html.markdown b/ru-ru/clojure-ru.html.markdown index 9bc32856..48c16192 100644 --- a/ru-ru/clojure-ru.html.markdown +++ b/ru-ru/clojure-ru.html.markdown @@ -4,6 +4,7 @@ filename: learnclojure-ru.clj contributors: - ["Adam Bard", "http://adambard.com/"] - ["Alexey Pirogov", "http://twitter.com/alex_pir"] + --- Clojure, это представитель семейства Lisp-подобных языков, разработанный @@ -374,7 +375,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут ; Структуры STM представлены тремя типами: ; - атомы (atoms) ; - агенты (agents) -: - ссылки (references) +; - ссылки (references) ; Самые простые хранители состояния - атомы: (def my-atom (atom {})) ; {} - начальное состояние атома -- cgit v1.2.3 From 8aa0ab6068a13491ec9cba59bce32911ab9f8061 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 16 Aug 2013 09:44:22 -0700 Subject: Edits --- c.html.markdown | 250 +++++++++++++++++++---------------------- ko-kr/java-kr.html.markdown | 10 +- pt-br/ruby-pt.html.markdown | 8 +- ru-ru/clojure-ru.html.markdown | 2 +- ru-ru/php-ru.html.markdown | 18 +-- ru-ru/python-ru.html.markdown | 8 +- zh-cn/go-zh.html.markdown | 183 +++++++++++++++--------------- 7 files changed, 240 insertions(+), 239 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 00b13cb0..24a96463 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -1,11 +1,9 @@ --- -- name: c -- category: language -- language: c -- filename: learnc.c -- contributors: - - [Adam Bard](http://adambard.com/) - - [Árpád Goretity](http://twitter.com/H2CO3_iOS) +language: c +filename: learnc.c +contributors: + - ["Adam Bard", "http://adambard.com/"] + - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] --- @@ -27,17 +25,10 @@ Multi-line comments look like this. They work in C89 as well. #include #include -// file names between are headers from the C standard library. -// They are searched for by the preprocessor in the system include paths -// (usually /usr/lib on Unices, can be controlled with the -I option if you are using GCC or clang.) +// (File names between are headers from the C standard library.) // For your own headers, use double quotes instead of angle brackets: #include "my_header.h" -// The C preprocessor introduces an almost fully-featured macro language. It's useful, but -// it can be confusing (and what's even worse, it can be misused). Read the -// Wikipedia article on the C preprocessor for further information: -// http://en.wikipedia.org/wiki/C_preprocessor - // Declare function signatures in advance in a .h file, or at the top of // your .c file. void function_1(); @@ -50,132 +41,117 @@ int main() { // %d is an integer, \n is a newline printf("%d\n", 0); // => Prints 0 // All statements must end with a semicolon - + /////////////////////////////////////// // Types /////////////////////////////////////// - - // You have to declare variables before using them. A variable declaration - // requires you to specify its type; a variable's type determines its size - // in bytes. - + // ints are usually 4 bytes int x_int = 0; - + // shorts are usually 2 bytes short x_short = 0; - + // chars are guaranteed to be 1 byte char x_char = 0; char y_char = 'y'; // Char literals are quoted with '' - + // longs are often 4 to 8 bytes; long longs are guaranteed to be at least // 64 bits long x_long = 0; long long x_long_long = 0; - + // floats are usually 32-bit floating point numbers float x_float = 0.0; - + // doubles are usually 64-bit floating-point numbers double x_double = 0.0; - - // Integral types may be unsigned. This means they can't be negative, but - // the maximum value of an unsigned variable is greater than the maximum - // signed value of the same size. - unsigned char ux_char; + + // Integral types may be unsigned. unsigned short ux_short; unsigned int ux_int; unsigned long long ux_long_long; - - // Other than char, which is always 1 byte (but not necessarily 8 bits!), - // these types vary in size depending on your machine and compiler. - // sizeof(T) gives you the size of a variable with type T in - // bytes so you can express the size of these types in a portable way. - // sizeof(obj) yields the size of an actual expression (variable, literal, etc.). - // For example, + + // sizeof(T) gives you the size of a variable with type T in bytes + // sizeof(obj) yields the size of the expression (variable, literal, etc.). printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) - - - // It's worth noting that if the argument of the `sizeof` operator is not a type but an expression, - // then its argument is not evaluated except VLAs (see below). Also, `sizeof()` is an operator, not a function, - // furthermore, the value it yields is a compile-time constant (except when used on VLAs, again.) + + + // If the argument of the `sizeof` operator an expression, then its argument + // is not evaluated (except VLAs (see below)). + // The value it yields in this case is a compile-time constant. int a = 1; size_t size = sizeof(a++); // a++ is not evaluated printf("sizeof(a++) = %zu where a = %d\n", size, a); - // the above code prints "sizeof(a++) = 4 where a = 1" (on a usual 32-bit architecture) - + // prints "sizeof(a++) = 4 where a = 1" (on a 32-bit architecture) + // 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 // (assuming 4-byte words) - - + + // 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 - - // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) can be declared as well. - // The size of such an array need not be a compile time constant: + + // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) + // can be declared as well. The size of such an array need not be a compile + // time constant: printf("Enter the array size: "); // ask the user for an array size char buf[0x100]; fgets(buf, sizeof buf, stdin); - size_t size = strtoul(buf, NULL, 10); // strtoul parses a string to an unsigned integer + + // strtoul parses a string to an unsigned integer + size_t size = strtoul(buf, NULL, 10); int var_length_array[size]; // declare the VLA printf("sizeof array = %zu\n", sizeof var_length_array); - + // A possible outcome of this program may be: - Enter the array size: 10 - sizeof array = 40 - + // > Enter the array size: 10 + // > sizeof array = 40 + // Strings are just arrays of chars terminated by a NUL (0x00) byte, // represented in strings as the special character '\0'. // (We don't have to include the NUL byte in string literals; the compiler // inserts it at the end of the array for us.) char a_string[20] = "This is a string"; printf("%s\n", a_string); // %s formats a string - - /* - You may have noticed that a_string is only 16 chars long. - Char #17 is the NUL byte. - Chars #18, 19 and 20 are 0 as well - if an initializer list (in this case, the string literal) - has less elements than the array it is initializing, then excess array elements are implicitly - initialized to zero. This is why int ar[10] = { 0 } works as expected intuitively. - */ - + printf("%d\n", a_string[16]); // => 0 - - // So string literals are strings enclosed within double quotes, but if we have characters - // between single quotes, that's a character literal. + // i.e., byte #17 is 0 (as are 18, 19, and 20) + + // If we have characters between single quotes, that's a character literal. // It's of type `int`, and *not* `char` (for historical reasons). int cha = 'a'; // fine - char chb = 'a'; // fine too (implicit conversion from int to char - truncation) - + char chb = 'a'; // fine too (implicit conversion from int to char) + /////////////////////////////////////// // 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 - floating-point numbers and calculations are not exact - + + f1 / f2; // => 0.5, plus or minus epsilon + // Floating-point numbers and calculations are not exact + // 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. // (Or _Bool or bool in C99.) @@ -187,14 +163,14 @@ int main() { 3 < 2; // => 0 2 <= 2; // => 1 2 >= 2; // => 1 - + // C is not Python - comparisons don't chain. int a = 1; // WRONG: int between_0_and_2 = 0 < a < 2; // Correct: int between_0_and_2 = 0 < a && a < 2; - + // Logic works on ints !3; // => 0 (Logical not) !0; // => 1 @@ -202,7 +178,7 @@ int main() { 0 && 1; // => 0 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 - + // Bitwise operators! ~0x0F; // => 0xF0 (bitwise negation, "1's complement") 0x0F & 0xF0; // => 0x00 (bitwise AND) @@ -210,17 +186,17 @@ int main() { 0x04 ^ 0x0F; // => 0x0B (bitwise XOR) 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) 0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) - - // Be careful when shifting signed integers - the following are all undefined behavior: + + // Be careful when shifting signed integers - the following are undefined: // - shifting into the sign bit of a signed integer (int a = 1 << 32) // - left-shifting a negative number (int a = -1 << 2) - // - shifting by an offset which is more than or equal to the width of the type of the LHS: + // - shifting by an offset which is >= the width of the type of the LHS: // int a = 1 << 32; // UB if int is 32 bits wide - + /////////////////////////////////////// // Control Structures /////////////////////////////////////// - + if (0) { printf("I am never run\n"); } else if (0) { @@ -228,36 +204,38 @@ int main() { } else { printf("I print\n"); } - + // While loops exist int ii = 0; while (ii < 10) { - printf("%d, ", ii++); // ii++ increments ii in-place, after yielding its value ("postincrement"). + printf("%d, ", ii++); // ii++ increments ii in-place + // after yielding its value ("postincrement"). } // => 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, and yields the already incremented value ("preincrement") + } while (++kk < 10); // ++kk increments kk in-place, and yields + // the already incremented value ("preincrement") // => 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"); - + // branching with multiple choices: switch() switch (some_integral_expression) { case 0: // labels need to be integral *constant* epxressions do_stuff(); - break; // if you don't break, control flow falls over labels - you usually don't want that. + break; // if you don't break, control flow falls over labels case 1: do_something_else(); break; @@ -267,73 +245,74 @@ int main() { exit(-1); break; } - + /////////////////////////////////////// // Typecasting /////////////////////////////////////// - + // Every value in C has a type, but you can cast one value into another type // if you want (with some constraints). - + 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", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long) - // printf("%d\n", (unsigned char) 257); would be undefined behavior - `char' is usually signed - // on most modern systems, and signed integer overflow invokes UB. - // Also, for determining the maximal value of a `char`, a `signed char` and an `unisigned char`, + + // For determining the max value of a `char`, a `signed char` and an `unisigned char`, // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from - + // Integral types can be cast to floating-point types, and vice-versa. printf("%f\n", (float)100); // %f formats a float printf("%lf\n", (double)100); // %lf formats a double printf("%d\n", (char)100.0); - + /////////////////////////////////////// // Pointers /////////////////////////////////////// - + // A pointer is a variable declared to store a memory address. Its declaration will // also tell you the type of data it points to. You can retrieve the memory address // of your variables, then mess with them. - + int x = 0; printf("%p\n", (void *)&x); // Use & to retrieve the address of a variable // (%p formats an object pointer of type void *) // => Prints some address in memory; - - + + // Pointers start with * in their declaration int *px, not_a_pointer; // px is a pointer to an int px = &x; // Stores the address of x in px printf("%p\n", (void *)px); // => Prints some address in memory printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer)); // => Prints "8, 4" on a typical 64-bit system - + // To retreive the value at the address a pointer is pointing to, // put * in front to de-reference it. - // Note: yes, it may be confusing that '*' is used for _both_ declaring a pointer and dereferencing it. - printf("%d\n", *px); // => Prints 0, the value of x, which is what px is pointing to the address of - + // Note: yes, it may be confusing that '*' is used for _both_ declaring a + // pointer and dereferencing it. + printf("%d\n", *px); // => Prints 0, the value of x + // You can also change the value the pointer is pointing to. // We'll have to wrap the de-reference in parenthesis because // ++ has a higher precedence than *. (*px)++; // Increment the value px is pointing to by 1 printf("%d\n", *px); // => Prints 1 printf("%d\n", x); // => Prints 1 - - int x_array[20]; // Arrays are a good way to allocate a contiguous block of memory + + // Arrays are a good way to allocate a contiguous block of memory + int x_array[20]; int xx; for (xx = 0; xx < 20; xx++) { x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 - + // Declare a pointer of type int and initialize it to point to x_array int* x_ptr = x_array; // x_ptr now points to the first element in the array (the integer 20). @@ -342,50 +321,52 @@ int main() { // it decays into (implicitly converted to) a pointer. // Exceptions: when the array is the argument of the `&` (address-od) operator: int arr[10]; - int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! It's of type "pointer to array" (of ten `int`s). + int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! + // It's of type "pointer to array" (of ten `int`s). // or when the array is a string literal used for initializing a char array: char arr[] = "foobarbazquirk"; // or when it's the argument of the `sizeof` or `alignof` operator: int arr[10]; int *ptr = arr; // equivalent with int *ptr = &arr[0]; printf("%zu %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8" - + // Pointers are incremented and decremented based on their type // (this is called pointer arithmetic) printf("%d\n", *(x_ptr + 1)); // => Prints 19 printf("%d\n", x_array[1]); // => Prints 19 - + // You can also dynamically allocate contiguous blocks of memory with the // standard library function malloc, which takes one argument of type size_t // representing the number of bytes to allocate (usually from the heap, although this // may not be true on e. g. embedded systems - the C standard says nothing about it). int *my_ptr = malloc(sizeof(*my_ptr) * 20); for (xx = 0; xx < 20; xx++) { - *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx would also work here, and it's also more readable + *(my_ptr + 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" - the program is said to invoke "undefined behavior" printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash. - + // When you're done with a malloc'd block of memory, you need to free it, // or else no one else can use it until your program terminates // (this is called a "memory leak"): free(my_ptr); - + // Strings are arrays of char, but they are usually represented as a // pointer-to-char (which is a pointer to the first element of the array). // It's good practice to use `const char *' when referring to a string literal, // since string literals shall not be modified (i. e. "foo"[0] = 'a' is ILLEGAL.) const char *my_str = "This is my very own string literal"; printf("%c\n", *my_str); // => 'T' - - // This is not the case if the string is an array (potentially initialized with a string literal) + + // This is not the case if the string is an array + // (potentially initialized with a string literal) // that resides in writable memory, as in: char foo[] = "foo"; foo[0] = 'a'; // this is legal, foo now contains "aoo" - + function_1(); } // end main function @@ -435,17 +416,17 @@ printf("%s\n", c); // => ".tset a si sihT" typedef int my_type; my_type my_type_var = 0; -// Structs are just collections of data, the members are allocated sequentially, in the order they are written: +// Structs are just collections of data, the members are allocated sequentially, +// in the order they are written: struct rectangle { int width; int height; }; -// it's generally not true that sizeof(struct rectangle) == sizeof(int) + sizeof(int) due to -// potential padding between the structure members (this is for alignment reasons. Probably won't -// happen if all members are of the same type, but watch out! -// See http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member -// for further information. +// It's not generally true that +// sizeof(struct rectangle) == sizeof(int) + sizeof(int) +// due to potential padding between the structure members (this is for alignment +// reasons). [1] void function_1() { @@ -473,7 +454,8 @@ int area(rect r) return r.width * r.height; } -// if you have large structs, you can pass them "by pointer" to avoid copying the whole struct: +// if you have large structs, you can pass them "by pointer" to avoid copying +// the whole struct: int area(const rect *r) { return r->width * r->height; @@ -527,3 +509,5 @@ Readable code is better than clever code and fast code. For a good, sane coding [Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). Other than that, Google is your friend. + +[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member diff --git a/ko-kr/java-kr.html.markdown b/ko-kr/java-kr.html.markdown index dcca9b2e..371b4665 100644 --- a/ko-kr/java-kr.html.markdown +++ b/ko-kr/java-kr.html.markdown @@ -1,5 +1,6 @@ --- language: java +filename: java-kr.java category: language contributors: - ["Jake Prather", "http://github.com/JakeHP"] @@ -26,7 +27,8 @@ import java.util.ArrayList; // java.security 패키지 안에 있는 모든 클래스를 임포트합니다. import java.security.*; -// 각 .java 파일에는 공용(public) 클래스가 들어 있으며, 클래스의 이름은 파일명과 동일합니다. +// 각 .java 파일에는 공용(public) 클래스가 들어 있으며, 클래스의 이름은 +// 파일명과 동일합니다. public class LearnJava { // 프로그램에는 반드시 진입점 역할을 하는 main 메서드가 하나 있어야 합니다. @@ -253,8 +255,8 @@ public class LearnJava { // String // 형변환 - // 자바 객채 또한 형변환할 수 있으며, 이와 관련해서 알아야 할 세부사항이 많을뿐더러 - // 다소 중급 수준에 해당하는 개념들도 다뤄야 합니다. + // 자바 객채 또한 형변환할 수 있으며, 이와 관련해서 알아야 할 세부사항이 + // 많을뿐더러 다소 중급 수준에 해당하는 개념들도 다뤄야 합니다. // 이와 관련된 사항은 아래 링크를 참고하세요. // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html @@ -403,4 +405,4 @@ class PennyFarthing extends Bicycle { * [제네릭(Generics)](http://docs.oracle.com/javase/tutorial/java/generics/index.html) -* [자바 코딩 관례(Java Code Conventions)](http://www.oracle.com/technetwork/java/codeconv-138413.html) \ No newline at end of file +* [자바 코딩 관례(Java Code Conventions)](http://www.oracle.com/technetwork/java/codeconv-138413.html) diff --git a/pt-br/ruby-pt.html.markdown b/pt-br/ruby-pt.html.markdown index 8e8ce6a8..484bb0dd 100644 --- a/pt-br/ruby-pt.html.markdown +++ b/pt-br/ruby-pt.html.markdown @@ -1,5 +1,6 @@ --- language: ruby +lang: br-pt filename: learnruby.rb contributors: - ["Bruno Henrique - Garu", "http://garulab.com"] @@ -98,9 +99,10 @@ caminho_para_a_raiz_do_projeto = '/bom/nome/' caminho = '/nome/ruim/' # Símbolos (são objetos) -# Símbolos são imutáveis, são constantes reutilizáveis representadadas internamente por um -# valor inteiro. Eles são frequentemente usados no lugar de strings para transmitir com eficiência os valores -# específicos e significativos +# Símbolos são imutáveis, são constantes reutilizáveis representadadas +# internamente por um valor inteiro. Eles são frequentemente usados no +# lugar de strings para transmitir com eficiência os valores específicos +# e significativos :pendente.class #=> Symbol diff --git a/ru-ru/clojure-ru.html.markdown b/ru-ru/clojure-ru.html.markdown index 48c16192..e1d68e5a 100644 --- a/ru-ru/clojure-ru.html.markdown +++ b/ru-ru/clojure-ru.html.markdown @@ -4,7 +4,7 @@ filename: learnclojure-ru.clj contributors: - ["Adam Bard", "http://adambard.com/"] - ["Alexey Pirogov", "http://twitter.com/alex_pir"] - +lang: ru-ru --- Clojure, это представитель семейства Lisp-подобных языков, разработанный diff --git a/ru-ru/php-ru.html.markdown b/ru-ru/php-ru.html.markdown index 6c5720e3..9133ecca 100644 --- a/ru-ru/php-ru.html.markdown +++ b/ru-ru/php-ru.html.markdown @@ -88,7 +88,8 @@ $unescaped = 'This just contains a slash and a t: \t'; // Заключайте переменные в фигурные скобки если это необходимо $money = "I have $${number} in the bank."; -// Начиная с PHP 5.3, синтаксис nowdocs может использоваться для неинтерполированного многострочного текста +// Начиная с PHP 5.3, синтаксис nowdocs может использоваться для +// неинтерполированного многострочного текста $nowdoc = <<<'END' Multi line string @@ -210,11 +211,13 @@ echo $integer + $integer; // => 2 $string = '1'; echo $string + $string; // => 2 (строка превращается в число) -// Выводится 0 по той причине, что оператор + не может привести строку 'one' к числовому типу +// Выводится 0 по той причине, что оператор + не может привести строку 'one' к +// числовому типу $string = 'one'; echo $string + $string; // => 0 -// Приведение типов (type casting) может быть использовано для преобразование переменной в другой тип +// Приведение типов (type casting) может быть использовано для преобразование +// переменной в другой тип $boolean = (boolean) 1; // => true $zero = 0; @@ -429,10 +432,11 @@ return 'Anything you like.'; // Эти функции могут также возвращать значения. $value = include 'my-include.php'; -// Имена файлов содержат их путь в файловой системе, или если передано просто имя файла, -// PHP обращается к директиве include_path. Если файл не найден в include_path, предпринимается -// попытка поиска в папке, где выполняется скрипт или в текущей рабочей директории. -// Если не в одном из этих мест файл не найден - выдается ошибка +// Имена файлов содержат их путь в файловой системе, или если передано просто +// имя файла, PHP обращается к директиве include_path. Если файл не найден в +// include_path, предпринимается попытка поиска в папке, где выполняется скрипт +// или в текущей рабочей директории. Если не в одном из этих мест файл не +// найден - выдается ошибка /* */ /******************************** diff --git a/ru-ru/python-ru.html.markdown b/ru-ru/python-ru.html.markdown index 58b0adcc..9163c8aa 100644 --- a/ru-ru/python-ru.html.markdown +++ b/ru-ru/python-ru.html.markdown @@ -1,5 +1,6 @@ --- language: python +lang: ru-ru contributors: - ["Yury Timofeev", "http://twitter.com/gagar1n"] filename: learnpython-ru.py @@ -219,7 +220,8 @@ filled_dict["four"] # KeyError # Чтобы избежать этого, используйте метод get filled_dict.get("one") #=> 1 filled_dict.get("four") #=> None -# Метод get также принимает аргумент default, значение которого будет возвращено при отсутствии указанного ключа +# Метод get также принимает аргумент default, значение которого будет +# возвращено при отсутствии указанного ключа filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 @@ -314,7 +316,9 @@ try: # Для выбора ошибки используется raise raise IndexError("Это IndexError") except IndexError as e: - pass # pass это просто отсутствие оператора. Обычно здесь происходит восстановление от ошибки. + # pass это просто отсутствие оператора. Обычно здесь происходит + # восстановление от ошибки. + pass #################################################### diff --git a/zh-cn/go-zh.html.markdown b/zh-cn/go-zh.html.markdown index 25fd1f03..8f7cb2af 100644 --- a/zh-cn/go-zh.html.markdown +++ b/zh-cn/go-zh.html.markdown @@ -29,7 +29,8 @@ import ( "strconv" // 字符串转换 ) -//函数声明:Main是程序执行的入口。不管你喜欢还是不喜欢,反正G就用了花括号来包住函数体。 +// 函数声明:Main是程序执行的入口。 +// 不管你喜欢还是不喜欢,反正G就用了花括号来包住函数体。 func main() { // 往标准输出打印一行。 // 用包名fmt限制打印函数。 @@ -65,10 +66,10 @@ func learnTypes() { can include line breaks.` // 同样是String类型 // 非ascii字符。Go使用UTF-8编码。 - g := 'Σ' // rune类型,uint32的别名,使用UTF-8编码 + g := 'Σ' // rune类型,uint32的别名,使用UTF-8编码 - f := 3.14195 // float64类型,IEEE-754 64位浮点数 - c := 3 + 4i // complex128类型,内部使用两个float64表示 + f := 3.14195 // float64类型,IEEE-754 64位浮点数 + c := 3 + 4i // complex128类型,内部使用两个float64表示 // Var变量可以直接初始化。 var u uint = 7 // unsigned 无符号变量,但是实现依赖int型变量的长度 @@ -99,9 +100,9 @@ can include line breaks.` // 同样是String类型 // 下划线 _ 可以使你“使用”一个变量,但是丢弃它的值。 _,_,_,_,_,_,_,_,_ = s2, g, f, u, pi, n, a3, s4, bs // 输出变量 - fmt.Println(s, c, a4, s3, d2, m) + fmt.Println(s, c, a4, s3, d2, m) - learnFlowControl() // 回到流程控制 + learnFlowControl() // 回到流程控制 } // Go全面支持垃圾回收。Go有指针,但是不支持指针运算。 @@ -117,155 +118,159 @@ func learnMemory() (p, q *int) { } func expensiveComputation() int { - return 1e6 + return 1e6 } func learnFlowControl() { // If需要花括号,括号就免了 - if true { - fmt.Println("told ya") - } - // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了,也不用容忍被人的代码风格。 - if false { - // pout - } else { - // gloat - } + if true { + fmt.Println("told ya") + } + // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了, + // 也不用容忍被人的代码风格。 + if false { + // pout + } else { + // gloat + } // 如果太多嵌套的if语句,推荐使用switch - x := 1 - switch x { - case 0: - case 1: + x := 1 + switch x { + case 0: + case 1: // 隐式调用break语句,匹配上一个即停止 - case 2: + case 2: // 不会运行 - } + } // 和if一样,for也不用括号 - for x := 0; x < 3; x++ { // ++ 自增 - fmt.Println("iteration", x) - } + for x := 0; x < 3; x++ { // ++ 自增 + fmt.Println("iteration", x) + } // x在这里还是1。为什么? // for 是go里唯一的循环关键字,不过它有很多变种 - for { // 无限循环 - break // 骗你的 - continue // 不会运行的 - } + for { // 无限循环 + break // 骗你的 + continue // 不会运行的 + } // 和for一样,if中的:=先给y赋值,然后再和x作比较。 - if y := expensiveComputation(); y > x { - x = y - } + if y := expensiveComputation(); y > x { + x = y + } // 闭包函数 - xBig := func() bool { - return x > 100 // x是上面声明的变量引用 - } - fmt.Println("xBig:", xBig()) // true (上面把y赋给x了) - x /= 1e5 // x变成10 - fmt.Println("xBig:", xBig()) // 现在是false + xBig := func() bool { + return x > 100 // x是上面声明的变量引用 + } + fmt.Println("xBig:", xBig()) // true (上面把y赋给x了) + x /= 1e5 // x变成10 + fmt.Println("xBig:", xBig()) // 现在是false // 当你需要goto的时候,你会爱死它的! - goto love + goto love love: - learnInterfaces() // 好东西来了! + learnInterfaces() // 好东西来了! } // 定义Stringer为一个接口类型,有一个方法String type Stringer interface { - String() string + String() string } // 定义pair为一个结构体,有x和y两个int型变量。 type pair struct { - x, y int + x, y int } // 定义pair类型的方法,实现Stringer接口。 func (p pair) String() string { // p被叫做“接收器” // Sprintf是fmt包中的另一个公有函数。 // 用 . 调用p中的元素。 - return fmt.Sprintf("(%d, %d)", p.x, p.y) + return fmt.Sprintf("(%d, %d)", p.x, p.y) } func learnInterfaces() { // 花括号用来定义结构体变量,:=在这里将一个结构体变量赋值给p。 - p := pair{3, 4} - fmt.Println(p.String()) // 调用pair类型p的String方法 - var i Stringer // 声明i为Stringer接口类型 - i = p // 有效!因为p实现了Stringer接口(类似java中的塑型) + p := pair{3, 4} + fmt.Println(p.String()) // 调用pair类型p的String方法 + var i Stringer // 声明i为Stringer接口类型 + i = p // 有效!因为p实现了Stringer接口(类似java中的塑型) // 调用i的String方法,输出和上面一样 - fmt.Println(i.String()) + fmt.Println(i.String()) - // fmt包中的Println函数向对象要它们的string输出,实现了String方法就可以这样使用了。(类似java中的序列化) - fmt.Println(p) // 输出和上面一样,自动调用String函数。 - fmt.Println(i) // 输出和上面一样。 + // fmt包中的Println函数向对象要它们的string输出,实现了String方法就可以这样使用了。 + // (类似java中的序列化) + fmt.Println(p) // 输出和上面一样,自动调用String函数。 + fmt.Println(i) // 输出和上面一样。 - learnErrorHandling() + learnErrorHandling() } func learnErrorHandling() { - // ", ok"用来判断有没有正常工作 - m := map[int]string{3: "three", 4: "four"} - if x, ok := m[1]; !ok { // ok 为false,因为m中没有1 - fmt.Println("no one there") - } else { - fmt.Print(x) // 如果x在map中的话,x就是那个值喽。 - } + // ", ok"用来判断有没有正常工作 + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // ok 为false,因为m中没有1 + fmt.Println("no one there") + } else { + fmt.Print(x) // 如果x在map中的话,x就是那个值喽。 + } // 错误可不只是ok,它还可以给出关于问题的更多细节。 - if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value - // 输出"strconv.ParseInt: parsing "non-int": invalid syntax" - fmt.Println(err) - } + if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value + // 输出"strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } // 待会再说接口吧。同时, - learnConcurrency() + learnConcurrency() } // c是channel类型,一个并发安全的通信对象。 func inc(i int, c chan int) { - c <- i + 1 // <-把右边的发送到左边的channel。 + c <- i + 1 // <-把右边的发送到左边的channel。 } // 我们将用inc函数来并发地增加一些数字。 func learnConcurrency() { // 用make来声明一个slice,make会分配和初始化slice,map和channel。 - c := make(chan int) - // 用go关键字开始三个并发的goroutine,如果机器支持的话,还可能是并行执行。三个都被发送到同一个channel。 - go inc(0, c) // go is a statement that starts a new goroutine. - go inc(10, c) - go inc(-805, c) + c := make(chan int) + // 用go关键字开始三个并发的goroutine,如果机器支持的话,还可能是并行执行。 + // 三个都被发送到同一个channel。 + go inc(0, c) // go is a statement that starts a new goroutine. + go inc(10, c) + go inc(-805, c) // 从channel中独处结果并打印。 // 打印出什么东西是不可预知的。 - fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是接收操作。 - - cs := make(chan string) // 操作string的channel - cc := make(chan chan string) // 操作channel的channel - go func() { c <- 84 }() // 开始一个goroutine来发送一个新的数字 - go func() { cs <- "wordy" }() // 发送给cs - // Select类似于switch,但是每个case包括一个channel操作。它随机选择一个准备好通讯的case。 - select { - case i := <-c: // 从channel接收的值可以赋给其他变量 - fmt.Println("it's a", i) - case <-cs: // 或者直接丢弃 - fmt.Println("it's a string") - case <-cc: // 空的,还没作好通讯的准备 - fmt.Println("didn't happen.") - } + fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是接收操作。 + + cs := make(chan string) // 操作string的channel + cc := make(chan chan string) // 操作channel的channel + go func() { c <- 84 }() // 开始一个goroutine来发送一个新的数字 + go func() { cs <- "wordy" }() // 发送给cs + // Select类似于switch,但是每个case包括一个channel操作。 + // 它随机选择一个准备好通讯的case。 + select { + case i := <-c: // 从channel接收的值可以赋给其他变量 + fmt.Println("it's a", i) + case <-cs: // 或者直接丢弃 + fmt.Println("it's a string") + case <-cc: // 空的,还没作好通讯的准备 + fmt.Println("didn't happen.") + } // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个保持阻塞。 - learnWebProgramming() // Go很适合web编程,我知道你也想学! + learnWebProgramming() // Go很适合web编程,我知道你也想学! } // http包中的一个简单的函数就可以开启web服务器。 func learnWebProgramming() { // ListenAndServe第一个参数指定了监听端口,第二个参数是一个接口,特定是http.Handler。 - err := http.ListenAndServe(":8080", pair{}) - fmt.Println(err) // 不要无视错误。 + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // 不要无视错误。 } // 使pair实现http.Handler接口的ServeHTTP方法。 func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 使用http.ResponseWriter返回数据 - w.Write([]byte("You learned Go in Y minutes!")) + w.Write([]byte("You learned Go in Y minutes!")) } ``` -- cgit v1.2.3 From ed2884ca3c60571169bb93f853d307808f319522 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Sat, 17 Aug 2013 10:29:45 +0200 Subject: Split the comment lines to avoid the exceeding of the block of code. --- fr-fr/ruby-fr.html.markdown | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown index 5efb2f3c..ab3ce0ac 100644 --- a/fr-fr/ruby-fr.html.markdown +++ b/fr-fr/ruby-fr.html.markdown @@ -166,7 +166,8 @@ hash['number'] #=> 5 # Recherchez une clé inexistante dans une Hash retourne nil : hash['nothing here'] #=> nil -# Depuis Ruby 1.9, Une syntaxe spécifique est apparue en utilisant les symboles comme clés : +# Depuis Ruby 1.9, Une syntaxe spécifique est apparue +# en utilisant les symboles comme clés : new_hash = { defcon: 3, action: true} @@ -198,10 +199,13 @@ end # CEPENDANT, l'usage de la boucle for est très rare. # À la place, utilisez la méthode "each" # et passez lui un bloc de code. -# Un bloc de code est un ensemble d'instructions que vous pouvez passer à une methode comme "each". -# Les blocs sont similaires aux lambdas, les fonctions anonymes ou les closures dans d'autres langages. +# Un bloc de code est un ensemble d'instructions +# que vous pouvez passer à une methode comme "each". +# Les blocs sont similaires aux lambdas, les fonctions anonymes +# ou les closures dans d'autres langages. # -# La méthode "each" exécute le bloc de code pour chaque élément de l'intervalle d'éléments. +# La méthode "each" exécute le bloc de code +# pour chaque élément de l'intervalle d'éléments. # Le bloc de code passe un paramètre compteur. # Appelez la méthode "each" avec un bloc de code comme ceci : -- cgit v1.2.3 From 6280f879b1395d61f07f223a9e4a4f0973414fdd Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Sat, 17 Aug 2013 10:31:54 +0200 Subject: Fix French syntax. --- fr-fr/ruby-fr.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown index ab3ce0ac..3060bd75 100644 --- a/fr-fr/ruby-fr.html.markdown +++ b/fr-fr/ruby-fr.html.markdown @@ -201,8 +201,8 @@ end # et passez lui un bloc de code. # Un bloc de code est un ensemble d'instructions # que vous pouvez passer à une methode comme "each". -# Les blocs sont similaires aux lambdas, les fonctions anonymes -# ou les closures dans d'autres langages. +# Les blocs sont similaires aux lambdas, aux fonctions anonymes +# ou encore aux closures dans d'autres langages. # # La méthode "each" exécute le bloc de code # pour chaque élément de l'intervalle d'éléments. -- cgit v1.2.3 From e875c3fff10ace21756e22e77ebe1ed1131719f4 Mon Sep 17 00:00:00 2001 From: kultprok Date: Sat, 17 Aug 2013 12:40:17 +0200 Subject: added first draft of german translation for python --- de-de/python-de.html.markdown | 484 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 484 insertions(+) create mode 100644 de-de/python-de.html.markdown diff --git a/de-de/python-de.html.markdown b/de-de/python-de.html.markdown new file mode 100644 index 00000000..0882d5e6 --- /dev/null +++ b/de-de/python-de.html.markdown @@ -0,0 +1,484 @@ +--- +language: python +contributors: + - ["Louie Dinh", "http://ldinh.ca"] + - ["kultprok", "http:/www.kulturproktologie.de"] +filename: learnpython.py +--- + +Anmerkungen des ursprnglichen Autors: +Python wurde in den frhen Neunzigern von Guido van Rossum entworfen. Es ist heute eine der beliebtesten Sprachen. Ich habe mich in Python wegen seiner syntaktischen bersichtlichkeit verliebt. Eigentlich ist es ausfhrbarer Pseudocode. + +Feedback ist herzlich willkommen! Ihr erreicht mich unter [@louiedinh](http://twitter.com/louiedinh) oder louiedinh [at] [google's email service] + +Hinweis: Dieser Beitrag bezieht sich besonders auf Python 2.7, er sollte aber auf Python 2.x anwendbar sein. Haltet Ausschau nach einem Rundgang durch Python 3, der bald erscheinen soll. + +```python +# Einzeilige Kommentare beginnen mit einer Raute (Doppelkreuz) +""" Mehrzeilige Strings werden mit + drei '-Zeichen geschrieben und werden + oft als Kommentare genutzt. +""" + +#################################################### +## 1. Primitive Datentypen und Operatoren +#################################################### + +# Die Zahlen +3 #=> 3 + +# Mathematik ist das, was man erwartet +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Division ist ein wenig kniffliger. Es ist ganzzahlige Division +# und rundet automatisch ab. +5 / 2 #=> 2 + +# Um das zu ndern, mssen wir Gleitkommazahlen kennenlernen. +2.0 # Das ist eine Gleitkommazahl +11.0 / 4.0 #=> 2.75 Ahhh...schon besser + +# Rangfolge wird mit Klammern erzwungen +(1 + 3) * 2 #=> 8 + +# Boolesche Ausdrcke sind primitive Datentypen +True +False + +# Mit not wird negiert +not True #=> False +not False #=> True + +# Gleichheit ist == +1 == 1 #=> True +2 == 1 #=> False + +# Ungleichheit is != +1 != 1 #=> False +2 != 1 #=> True + +# Ein paar weitere Vergleiche +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +# Vergleiche knnen verknpft werden! +1 < 2 < 3 #=> True +2 < 3 < 2 #=> False + +# Strings werden mit " oder ' gebildet +"Das ist ein String." +'Das ist auch ein String.' + +# Strings knnen addiert werden! +"Hello " + "world!" #=> "Hello world!" + +# Ein String kann wie eine Liste von Zeichen verwendet werden +"Das ist ein String"[0] #=> 'D' + +# Mit % knnen Strings formatiert werden, etwa so: +"%s knnen %s werden" % ("Strings", "interpoliert") + +# Ein neuerer Weg, um Strings zu formatieren, ist die format-Methode. +# Diese Methode wird bevorzugt +"{0} knnen {1} werden".format("Strings", "formatiert") +# Wir knnen Schlsselwrter verwenden, wenn wir nicht abzhlen wollen. +"{name} will {food} essen".format(name="Bob", food="Lasagne") + +# None ist ein Objekt +None #=> None + +# Verwendet nicht das Symbol fr Gleichheit `==`, um Objekte mit None zu vergleichen +# Benutzt stattdessen `is` +"etc" is None #=> False +None is None #=> True + +# Der 'is'-Operator testet Objektidentitt. Das ist nicht +# sehr ntzlich, wenn wir mit primitiven Datentypen arbeiten, aber +# sehr ntzlich bei Objekten. + +# None, 0, und leere Strings/Listen werden alle als False bewertet. +# Alle anderen Werte sind True +0 == False #=> True +"" == False #=> True + + +#################################################### +## 2. Variablen und Collections +#################################################### + +# Ausgabe ist sehr einfach +print "Ich bin Python. Schn, dich kennenzulernen!" + + +# Es gibt keinen Grund, Variablen vor der Zuweisung zu deklarieren. +some_var = 5 # kleinschreibung_mit_unterstrichen entspricht der Norm +some_var #=> 5 + +# Eine noch nicht deklarierte Variable anzusprechen, lst eine Exception aus. +# Siehe Kontrollstruktur, um mehr ber Ausnahmebehandlung zu lernen. +some_other_var # Lst einen NameError aus + +# if kann als Ausdruck verwendet werden +"yahoo!" if 3 > 2 else 2 #=> "yahoo!" + +# Listen speichern Sequenzen +li = [] +# Wir knnen mit einer bereits gefllten Liste anfangen +other_li = [4, 5, 6] + +# append fgt Daten am Ende der Liste ein +li.append(1) #li ist jetzt [1] +li.append(2) #li ist jetzt [1, 2] +li.append(4) #li ist jetzt [1, 2, 4] +li.append(3) #li ist jetzt [1, 2, 4, 3] +# Vom Ende der Liste mit pop entfernen +li.pop() #=> 3 und li ist jetzt [1, 2, 4] +# Fgen wir es wieder hinzu +li.append(3) # li ist jetzt wieder [1, 2, 4, 3]. + +# Greife auf Listen wie auf Arrays zu +li[0] #=> 1 +# Das letzte Element ansehen +li[-1] #=> 3 + +# Auerhalb der Liste ist es ein IndexError +li[4] # Raises an IndexError + +# Wir knnen uns Ranges mit Slice-Syntax ansehen +li[1:3] #=> [2, 4] +# Den Anfang auslassen +li[2:] #=> [4, 3] +# Das Ende auslassen +li[:3] #=> [1, 2, 4] + +# Ein bestimmtes Element mit del aus der Liste entfernen +del li[2] # li ist jetzt [1, 2, 3] + +# Listen knnen addiert werden +li + other_li #=> [1, 2, 3, 4, 5, 6] - Hinweis: li und other_li werden in Ruhe gelassen + +# Listen mit extend verknpfen +li.extend(other_li) # Jetzt ist li [1, 2, 3, 4, 5, 6] + +# Mit in auf Existenz eines Elements prfen +1 in li #=> True + +# Die Lnge der Liste mit len ermitteln +len(li) #=> 6 + + +# Tupel sind wie Listen, nur unvernderlich. +tup = (1, 2, 3) +tup[0] #=> 1 +tup[0] = 3 # Lst einen TypeError aus + +# Wir knnen all diese Listen-Dinge auch mit Tupeln anstellen +len(tup) #=> 3 +tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) +tup[:2] #=> (1, 2) +2 in tup #=> True + +# Wir knnen Tupel (oder Listen) in Variablen entpacken +a, b, c = (1, 2, 3) # a ist jetzt 1, b ist jetzt 2 und c ist jetzt 3 +# Tuple werden standardmig erstellt, wenn wir uns die Klammern sparen +d, e, f = 4, 5, 6 +# Es ist kinderleicht zwei Werte zu tauschen +e, d = d, e # d is now 5 and e is now 4 + + +# Dictionarys (Wrterbucher) speichern Key-Value-Paare +empty_dict = {} +# Hier ein geflltes Wrterbuch +filled_dict = {"one": 1, "two": 2, "three": 3} + +# Wir knnen Eintrge mit [] nachschlagen +filled_dict["one"] #=> 1 + +# So holen wir alle Keys (Schlssel) als Liste +filled_dict.keys() #=> ["three", "two", "one"] +# Hinweis - Die Reihenfolge von Schlsseln in der Liste ist nicht garantiert. +# Einzelne Resultate knnen anders angeordnet sein. + +# Alle Values (Werte) als Liste +filled_dict.values() #=> [3, 2, 1] +# Hinweis - Hier gelten dieselben Einschrnkungen fr die Reihenfolge wie bei Schlsseln. + +# Das Vorhandensein eines Schlssels im Wrterbuch mit in prfen +"one" in filled_dict #=> True +1 in filled_dict #=> False + +# Einen nicht vorhandenenen Schlssel zu suchen, lst einen KeyError aus +filled_dict["four"] # KeyError + +# Mit der get-Methode verhindern wir das +filled_dict.get("one") #=> 1 +filled_dict.get("four") #=> None +# Die get-Methode untersttzt auch ein Standardargument, falls der Wert fehlt +filled_dict.get("one", 4) #=> 1 +filled_dict.get("four", 4) #=> 4 + +# Die setdefault-Methode ist ein sicherer Weg, ein neues Schlssel-Wert-Paar anzulegen +filled_dict.setdefault("five", 5) #filled_dict["five"] wird auf 5 gesetzt +filled_dict.setdefault("five", 6) #filled_dict["five"] ist noch immer 5 + + +# Sets speichern Mengen +empty_set = set() +# Initialisieren wir ein Set mit ein paar Werten +some_set = set([1,2,2,3,4]) # some_set ist jetzt set([1, 2, 3, 4]) + +# Seit Python 2.7 kann {} benutzt werden, um ein Set zu erstellen +filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} + +# Mehr Elemente hinzufgen +filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} + +# Schnittmengen werden mit & gebildet +other_set = {3, 4, 5, 6} +filled_set & other_set #=> {3, 4, 5} + +# Mengen werden mit | vereinigt +filled_set | other_set #=> {1, 2, 3, 4, 5, 6} + +# Die Differenz einer Menge mit - bilden +{1,2,3,4} - {2,3,5} #=> {1, 4} + +# Auf Vorhandensein mit in prfen +2 in filled_set #=> True +10 in filled_set #=> False + + +#################################################### +## 3. Kontrollstruktur +#################################################### + +# Erstellen wir mal eine Variable +some_var = 5 + +# Hier eine if-Anweisung. Die Einrckung ist in Python wichtig! +# gibt "some_var ist kleiner als 10" aus +if some_var > 10: + print "some_var ist viel grer als 10." +elif some_var < 10: # Dieser elif-Absatz ist optional. + print "some_var ist kleiner als 10." +else: # Das hier ist auch optional. + print "some_var ist tatschlich 10." + + +""" +For-Schleifen iterieren ber Listen +Ausgabe: + hund ist ein Sugetier + katze ist ein Sugetier + maus ist ein Sugetier +""" +for animal in ["hund", "katze", "maus"]: + # Wir knnen Strings mit % formatieren + print "%s ist ein Sugetier" % animal + +""" +`range(Zahl)` gibt eine null-basierte Liste bis zur angegebenen Zahl wieder +Ausgabe: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i + +""" +While-Schleifen laufen, bis eine Bedingung erfllt ist. +Ausgabe: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print x + x += 1 # Kurzform fr x = x + 1 + +# Ausnahmebehandlung mit einem try/except-Block + +# Funktioniert in Python 2.6 und hher: +try: + # Mit raise wird ein Fehler ausgegeben + raise IndexError("Das hier ist ein Index-Fehler") +except IndexError as e: + pass # Pass ist nur eine no-op. Normalerweise wrden wir hier den Fehler klren. + + +#################################################### +## 4. Funktionen +#################################################### + +# Mit def neue Funktionen erstellen +def add(x, y): + print "x ist %s und y ist %s" % (x, y) + return x + y # Werte werden mit return zurckgegeben + +# Funktionen mit Parametern aufrufen +add(5, 6) #=> Ausgabe ist "x ist 5 und y ist 6" und gibt 11 zurck + +# Ein anderer Weg des Funktionsaufrufs sind Schlsselwort-Argumente +add(y=6, x=5) # Schlsselwrter knnen in beliebiger Reihenfolge bergeben werden. + +# Wir knnen Funktionen mit beliebiger Anzahl von # Positionsargumenten definieren +def varargs(*args): + return args + +varargs(1, 2, 3) #=> (1,2,3) + + +# Wir knnen auch Funktionen mit beliebiger Anzahl +# Schlsselwort-Argumenten definieren +def keyword_args(**kwargs): + return kwargs + +# Rufen wir es mal auf, um zu sehen, was passiert +keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} + +# Wir knnen beides gleichzeitig machem, wenn wir wollen +def all_the_args(*args, **kwargs): + print args + print kwargs +""" +all_the_args(1, 2, a=3, b=4) Ausgabe: + (1, 2) + {"a": 3, "b": 4} +""" + +# Beim Aufruf von Funktionen knnen wir das Gegenteil von varargs/kwargs machen! +# Wir benutzen dann *, um Tupel auszuweiten, und ** fr kwargs. +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +all_the_args(*args) # quivalent zu foo(1, 2, 3, 4) +all_the_args(**kwargs) # quivalent zu foo(a=3, b=4) +all_the_args(*args, **kwargs) # quivalent zu foo(1, 2, 3, 4, a=3, b=4) + +# Python hat First-Class-Funktionen +def create_adder(x): + def adder(y): + return x + y + return adder + +add_10 = create_adder(10) +add_10(3) #=> 13 + +# Es gibt auch anonyme Funktionen +(lambda x: x > 2)(3) #=> True + +# Es gibt auch Funktionen hherer Ordnung als Built-Ins +map(add_10, [1,2,3]) #=> [11, 12, 13] +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] + +# Wir knnen bei map- und filter-Funktionen auch List Comprehensions einsetzen +[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] +[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] + +#################################################### +## 5. Klassen +#################################################### + +# Wir bilden die Unterklasse eines Objekts, um Klassen zu erhalten. +class Human(object): + + # Ein Klassenattribut. Es wird von allen Instanzen einer Klasse geteilt + species = "H. sapiens" + + # Ein simpler Konstruktor + def __init__(self, name): + # Wir weisen das Argument name dem name-Attribut der Instanz zu + self.name = name + + # Eine Instanzmethode. Alle Methoden erhalten self als erstes Argument. + def say(self, msg): + return "%s: %s" % (self.name, msg) + + # Eine Klassenmethode wird von allen Instanzen geteilt. + # Sie werden mit der aufrufenden Klasse als erstem Argument aufgerufen + @classmethod + def get_species(cls): + return cls.species + + # Eine statische Methode wird ohne Klasse oder Instanz aufgerufen + @staticmethod + def grunt(): + return "*grunt*" + + +# Eine Instanz einer Klasse erstellen +i = Human(name="Ian") +print i.say("hi") # gitbt "Ian: hi" aus + +j = Human("Joel") +print j.say("hello") #gibt "Joel: hello" aus + +# Rufen wir mal unsere Klassenmethode auf +i.get_species() #=> "H. sapiens" + +# ndern wir mal das gemeinsame Attribut +Human.species = "H. neanderthalensis" +i.get_species() #=> "H. neanderthalensis" +j.get_species() #=> "H. neanderthalensis" + +# Aufruf der statischen Methode +Human.grunt() #=> "*grunt*" + + +#################################################### +## 6. Module +#################################################### + +# Wir knnen Module importieren +import math +print math.sqrt(16) #=> 4 + +# Wir knnen auch nur spezielle Funktionen eines Moduls importieren +from math import ceil, floor +print ceil(3.7) #=> 4.0 +print floor(3.7) #=> 3.0 + +# Wir knnen auch alle Funktionen eines Moduls importieren +# Warnung: Dies wird nicht empfohlen +from math import * + +# Wir knnen Modulnamen abkrzen +import math as m +math.sqrt(16) == m.sqrt(16) #=> True + +# Module sind in Python nur gewhnliche Dateien. Wir +# knnen unsere eigenen schreiben und importieren. Der Name des +# Moduls ist der Dateiname. + +# Wir knnen auch die Funktionen und Attribute eines +# Moduls herausfinden. +import math +dir(math) + + +``` + +## Lust auf mehr? + +### Kostenlos online (Englisch) + +* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +* [Dive Into Python](http://www.diveintopython.net/) +* [The Official Docs](http://docs.python.org/2.6/) +* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) +* [Python Module of the Week](http://pymotw.com/2/) + +### Totholz (Englisch) + +* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) +* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) +* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) + -- cgit v1.2.3 From 4997a2af3333dd74074d88943a36569a038175ad Mon Sep 17 00:00:00 2001 From: marcuse Date: Sat, 17 Aug 2013 13:17:21 +0200 Subject: Spelling, capatability -> compatibility And added a link to the RubySpec section. --- ruby-ecosystem.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index 54c1d178..c2a2087b 100644 --- a/ruby-ecosystem.html.markdown +++ b/ruby-ecosystem.html.markdown @@ -63,7 +63,7 @@ Very mature/compatible: * MRI - Written in C, this is the reference implementation of ruby. By definition it is 100% compatible (with itself). All other rubies -maintain capatability with MRI (see RubySpec below). +maintain compatibility with MRI (see [RubySpec](#rubyspec) below). * JRuby - Written in Java and ruby, this robust implementation is quite fast. Most importantly, JRuby's strength is JVM/Java interop, leveraging existing JVM tools, projects, and languages. -- cgit v1.2.3 From 2173dd419a1daa06efee295e3fe2cdbc40224433 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 15:57:52 +0200 Subject: Accessing nullable's value in C# --- csharp.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index c254b5a9..c9df6db9 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -144,6 +144,10 @@ namespace Learning int? nullable = null; Console.WriteLine("Nullable variable: " + nullable); + // In order to use nullable's value, you have to use Value property or to explicitly cast it + string? nullableString = "not null"; + Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString ); + // ?? is syntactic sugar for specifying default value // in case variable is null int notNullable = nullable ?? 0; -- cgit v1.2.3 From 541fd3fb06bf6e97974123934ad89cb07a7ef289 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:01:08 +0200 Subject: C# Coding style: property names start with capitals --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index c9df6db9..490c34bb 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -474,7 +474,7 @@ namespace Learning // when only data needs to be accessed, consider using properties. // properties may have either get or set, or both private bool _hasTassles; // private variable - public bool hasTassles // public accessor + public bool HasTassles // public accessor { get { return _hasTassles; } set { _hasTassles = value; } -- cgit v1.2.3 From 4295e85d6097e2d331ff75a958a3a5a064795410 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:03:35 +0200 Subject: C#: Auto-implemented properties --- csharp.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 490c34bb..951958b6 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -480,13 +480,13 @@ namespace Learning set { _hasTassles = value; } } - private int _frameSize; + // Properties can be auto-implemented public int FrameSize { - get { return _frameSize; } + get; // you are able to specify access modifiers for either get or set // this means only Bicycle class can call set on Framesize - private set { _frameSize = value; } + private set; } //Method to display the attribute values of this Object. -- cgit v1.2.3 From c3df13db567b82fc0802e46c23e4e4486618b207 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:06:04 +0200 Subject: C#: comments about "this" keyword --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 951958b6..0cef8f05 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -422,10 +422,10 @@ namespace Learning public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes) { - this.gear = startGear; + this.gear = startGear; // "this" keyword denotes the current object this.cadence = startCadence; this._speed = startSpeed; - this.name = name; + this.name = name; // it can be useful when there's a name conflict this.hasCardsInSpokes = hasCardsInSpokes; } -- cgit v1.2.3 From c903f5a73dc0b0eb5fe84e20182767dc310bd30a Mon Sep 17 00:00:00 2001 From: Roberto Perez Date: Sat, 17 Aug 2013 10:12:25 -0500 Subject: created groovy markdown --- groovy.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 groovy.html.markdown diff --git a/groovy.html.markdown b/groovy.html.markdown new file mode 100644 index 00000000..d015e5d2 --- /dev/null +++ b/groovy.html.markdown @@ -0,0 +1,9 @@ +--- +language: Groovy +filename: learngroovy.groovy +contributors: + - ["Roberto Perez Alcolea", "http://github.com/rpalcolea"] +filename: learngroovy.groovy +--- + +Groovy - A dynamic language for the Java platform -- cgit v1.2.3 From 767fb174f8e5981dec603c2ed6782cef5574c7b4 Mon Sep 17 00:00:00 2001 From: Roberto Perez Date: Sat, 17 Aug 2013 10:19:47 -0500 Subject: Groovy: Added references --- groovy.html.markdown | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/groovy.html.markdown b/groovy.html.markdown index d015e5d2..94906ffc 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -6,4 +6,30 @@ contributors: filename: learngroovy.groovy --- -Groovy - A dynamic language for the Java platform +Groovy - A dynamic language for the Java platform [Read more here.](http://groovy.codehaus.org) + +```cpp + + +``` + +## Further resources + +[Groovy documentation](http://groovy.codehaus.org/Documentation) + +[Groovy web console](http://groovyconsole.appspot.com/) + +Join a [Groovy user group](http://groovy.codehaus.org/User+Groups) + +## Books + +* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook) + +* [Groovy in Action] (http://manning.com/koenig2/) + +* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do) + + + + + -- cgit v1.2.3 From 12b39c739f0f39a753ef30c70f1d57c6876c0902 Mon Sep 17 00:00:00 2001 From: Roberto Perez Date: Sat, 17 Aug 2013 11:23:36 -0500 Subject: Groovy: added installation, colletions, maps, beans, logical branching and loops --- groovy.html.markdown | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/groovy.html.markdown b/groovy.html.markdown index 94906ffc..3a928787 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -10,6 +10,177 @@ Groovy - A dynamic language for the Java platform [Read more here.](http://groov ```cpp +/* + Set yourself up: + + 1) Install GVM - http://gvmtool.net/ + 2) Install Groovy: gvm install groovy + 3) Start the groovy console by typing: groovyConsole + +*/ + +// Single line comments start with two forward slashes +/* +Multi line comments look like this. +*/ + +// Hello World +println "Hello world!" + +/* + Variables: + + You can assign values to variables for later use +*/ + +def x = 1 +println x + +x = new java.util.Date() +println x + +x = -3.1499392 +println x + +x = false +println x + +x = "Groovy!" +println x + +/* + Collections and maps +*/ +//Creating an empty list +def technologies = [] + +//Add an element to the list +technologies << "Groovy" +technologies.add("Grails") +technologies.addAll(["Gradle","Griffon"]) + +//Remove an element from the list +technologies.remove("Griffon") + +//Iterate over elements of a list +technologies.each { println "Technology: $it"} +technologies.eachWithIndex { it, i -> println "$i: $it"} + +//Evaluate if a list contains element(s) (boolean) +technologies.contains('Groovy') +technologies.containsAll(['Groovy','Grails']) + +//Sort a list +technologies.sort() + +//Replace all elements in the list +Collections.replaceAll(technologies, 'Gradle', 'gradle') + +//Shuffle a list +Collections.shuffle(technologies, new Random()) + +//Clear a list +technologies.clear() + +//Creating an empty map +def devMap = [:] + +//Add values +devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +devMap.put('lastName','Perez') + +//Iterate over elements of a map +devMap.each { println "$it.key: $it.value" } +devMap.eachWithIndex { it, i -> println "$i: $it"} + +//Evaluate if a map contains a key +assert devMap.containsKey('name') + +//Evaluate if a map contains a value +assert devMap.containsValue('Roberto') + +//Get the keys of a map +println devMap.keySet() + +//Get the values of a map +println devMap.values() + +/* + Groovy Beans + + GroovyBeans are JavaBeans but using a much simpler syntax + + When Groovy is compiled to bytecode, the following rules are used. + + * If the name is declared with an access modifier (public, private or protected) then a field is generated. + * A name declared with no access modifier generates a private field with public getter and setter (i.e. a property). + * If a property is declared final the private field is created final and no setter is generated. + * You can declare a property and also declare your own getter or setter. + * You can declare a property and a field of the same name, the property will use that field then. + * If you want a private or protected property you have to provide your own getter and setter which must be declared private or protected. + * If you access a property from within the class the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going though the getter and setter. + * If you access a property that does not exist using the explicit or implicit foo, then Groovy will access the property through the meta class, which may fail at runtime. + +*/ + +class Foo { + // read only property + final String name = "Roberto" + + // read only property with public getter and protected setter + String language + protected void setLanguage(String language) { this.language = language } + + // dynamically typed property + def lastName +} + +/* + Logical Branching and Looping +*/ + +//Groovy supports the usual if - else syntax +def x = 3 + +if(x==1) { + println "One" +} else if(x==2) { + println "Two" +} else { + println "X greater than Two" +} + +//Groovy also supports the ternary operator: +def y = 10 +def x = (y > 1) ? "worked" : "failed" +assert x == "worked" + +//For loop +//Iterate over a range +def x = 0 +for (i in 0 .. 30) { + x += i +} + +//Iterate over a list +x = 0 +for( i in [5,3,2,1] ) { + x += i +} + +//Iterate over an array +array = (0..20).toArray() +x = 0 +for (i in array) { + x += i +} + +//Iterate over a map +def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +x = 0 +for ( e in map ) { + x += e.value +} ``` -- cgit v1.2.3 From f07376a26830eb9c2ade7933ca45191776e965da Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Sat, 17 Aug 2013 18:33:18 +0200 Subject: Add french coffeescript translation. --- fr-fr/coffeescript-fr.html.markdown | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 fr-fr/coffeescript-fr.html.markdown diff --git a/fr-fr/coffeescript-fr.html.markdown b/fr-fr/coffeescript-fr.html.markdown new file mode 100644 index 00000000..c66b7be0 --- /dev/null +++ b/fr-fr/coffeescript-fr.html.markdown @@ -0,0 +1,58 @@ +--- +language: coffeescript +contributors: + - ["Tenor Biel", "http://github.com/L8D"] +translators: + - ["Geoffrey Roguelon", "https://github.com/GRoguelon"] +lang: fr-fr +filename: coffeescript-fr.coffee +--- + +``` coffeescript +# CoffeeScript est un langage préprocesseur, il permet de générer du Javascript. +# Il suit les tendances de certains langages récents. +# Par exemple, les commentaires se définissent comme en Ruby ou en Python. + +### +Ceci est un bloc de commentaires +il est converti directement avec '/ *' et '* /' +pour correspondre aux commentaires Javascript + +Vous devez comprendre la syntaxe du langage JavaScript pour continuer. +### + +# Affectation : +number = 42 #=> var number = 42; +opposite = true #=> var opposite = true; + +# Structures de contrôle : +number = -42 if opposite #=> if(opposite) { number = -42; } + +# Fonctions : +square = (x) -> x * x #=> var square = function(x) { return x * x; } + +# Intervals : +list = [1..5] #=> var list = [1, 2, 3, 4, 5]; + +# Objets : +math = + root: Math.sqrt + square: square + cube: (x) -> x * square x +#=> var math = { +# "root": Math.sqrt, +# "square": square, +# "cube": function(x) { return x * square(x); } +#} + +# Liste d'arguments variables : +race = (winner, runners...) -> + print winner, runners + +# Existance : +alert "I knew it!" if elvis? +#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); } + +# Lecture d'un tableau : +cubes = (math.cube num for num in list) #=> ... +``` -- cgit v1.2.3 From abf527143adf98564206f9253270e52d08045a49 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 17 Aug 2013 09:44:05 -0700 Subject: Changed python-de to utf-8 --- de-de/python-de.html.markdown | 162 +++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/de-de/python-de.html.markdown b/de-de/python-de.html.markdown index 0882d5e6..7298c62c 100644 --- a/de-de/python-de.html.markdown +++ b/de-de/python-de.html.markdown @@ -6,8 +6,8 @@ contributors: filename: learnpython.py --- -Anmerkungen des ursprnglichen Autors: -Python wurde in den frhen Neunzigern von Guido van Rossum entworfen. Es ist heute eine der beliebtesten Sprachen. Ich habe mich in Python wegen seiner syntaktischen bersichtlichkeit verliebt. Eigentlich ist es ausfhrbarer Pseudocode. +Anmerkungen des ursprünglichen Autors: +Python wurde in den frühen Neunzigern von Guido van Rossum entworfen. Es ist heute eine der beliebtesten Sprachen. Ich habe mich in Python wegen seiner syntaktischen Übersichtlichkeit verliebt. Eigentlich ist es ausführbarer Pseudocode. Feedback ist herzlich willkommen! Ihr erreicht mich unter [@louiedinh](http://twitter.com/louiedinh) oder louiedinh [at] [google's email service] @@ -37,14 +37,14 @@ Hinweis: Dieser Beitrag bezieht sich besonders auf Python 2.7, er sollte aber au # und rundet automatisch ab. 5 / 2 #=> 2 -# Um das zu ndern, mssen wir Gleitkommazahlen kennenlernen. +# Um das zu ändern, müssen wir Gleitkommazahlen kennenlernen. 2.0 # Das ist eine Gleitkommazahl 11.0 / 4.0 #=> 2.75 Ahhh...schon besser # Rangfolge wird mit Klammern erzwungen (1 + 3) * 2 #=> 8 -# Boolesche Ausdrcke sind primitive Datentypen +# Boolesche Ausdrücke sind primitive Datentypen True False @@ -66,7 +66,7 @@ not False #=> True 2 <= 2 #=> True 2 >= 2 #=> True -# Vergleiche knnen verknpft werden! +# Vergleiche können verknüpft werden! 1 < 2 < 3 #=> True 2 < 3 < 2 #=> False @@ -74,32 +74,32 @@ not False #=> True "Das ist ein String." 'Das ist auch ein String.' -# Strings knnen addiert werden! +# Strings können addiert werden! "Hello " + "world!" #=> "Hello world!" # Ein String kann wie eine Liste von Zeichen verwendet werden "Das ist ein String"[0] #=> 'D' -# Mit % knnen Strings formatiert werden, etwa so: -"%s knnen %s werden" % ("Strings", "interpoliert") +# Mit % können Strings formatiert werden, etwa so: +"%s können %s werden" % ("Strings", "interpoliert") # Ein neuerer Weg, um Strings zu formatieren, ist die format-Methode. # Diese Methode wird bevorzugt -"{0} knnen {1} werden".format("Strings", "formatiert") -# Wir knnen Schlsselwrter verwenden, wenn wir nicht abzhlen wollen. +"{0} können {1} werden".format("Strings", "formatiert") +# Wir können Schlüsselwörter verwenden, wenn wir nicht abzählen wollen. "{name} will {food} essen".format(name="Bob", food="Lasagne") # None ist ein Objekt None #=> None -# Verwendet nicht das Symbol fr Gleichheit `==`, um Objekte mit None zu vergleichen +# Verwendet nicht das Symbol für Gleichheit `==`, um Objekte mit None zu vergleichen # Benutzt stattdessen `is` "etc" is None #=> False None is None #=> True -# Der 'is'-Operator testet Objektidentitt. Das ist nicht -# sehr ntzlich, wenn wir mit primitiven Datentypen arbeiten, aber -# sehr ntzlich bei Objekten. +# Der 'is'-Operator testet Objektidentität. Das ist nicht +# sehr nützlich, wenn wir mit primitiven Datentypen arbeiten, aber +# sehr nützlich bei Objekten. # None, 0, und leere Strings/Listen werden alle als False bewertet. # Alle anderen Werte sind True @@ -112,33 +112,33 @@ None is None #=> True #################################################### # Ausgabe ist sehr einfach -print "Ich bin Python. Schn, dich kennenzulernen!" +print "Ich bin Python. Schön, dich kennenzulernen!" # Es gibt keinen Grund, Variablen vor der Zuweisung zu deklarieren. some_var = 5 # kleinschreibung_mit_unterstrichen entspricht der Norm some_var #=> 5 -# Eine noch nicht deklarierte Variable anzusprechen, lst eine Exception aus. -# Siehe Kontrollstruktur, um mehr ber Ausnahmebehandlung zu lernen. -some_other_var # Lst einen NameError aus +# Eine noch nicht deklarierte Variable anzusprechen, löst eine Exception aus. +# Siehe Kontrollstruktur, um mehr über Ausnahmebehandlung zu lernen. +some_other_var # Löst einen NameError aus # if kann als Ausdruck verwendet werden "yahoo!" if 3 > 2 else 2 #=> "yahoo!" # Listen speichern Sequenzen li = [] -# Wir knnen mit einer bereits gefllten Liste anfangen +# Wir können mit einer bereits gefüllten Liste anfangen other_li = [4, 5, 6] -# append fgt Daten am Ende der Liste ein +# append fügt Daten am Ende der Liste ein li.append(1) #li ist jetzt [1] li.append(2) #li ist jetzt [1, 2] li.append(4) #li ist jetzt [1, 2, 4] li.append(3) #li ist jetzt [1, 2, 4, 3] # Vom Ende der Liste mit pop entfernen li.pop() #=> 3 und li ist jetzt [1, 2, 4] -# Fgen wir es wieder hinzu +# Fügen wir es wieder hinzu li.append(3) # li ist jetzt wieder [1, 2, 4, 3]. # Greife auf Listen wie auf Arrays zu @@ -146,10 +146,10 @@ li[0] #=> 1 # Das letzte Element ansehen li[-1] #=> 3 -# Auerhalb der Liste ist es ein IndexError +# Außerhalb der Liste ist es ein IndexError li[4] # Raises an IndexError -# Wir knnen uns Ranges mit Slice-Syntax ansehen +# Wir können uns Ranges mit Slice-Syntax ansehen li[1:3] #=> [2, 4] # Den Anfang auslassen li[2:] #=> [4, 3] @@ -159,70 +159,70 @@ li[:3] #=> [1, 2, 4] # Ein bestimmtes Element mit del aus der Liste entfernen del li[2] # li ist jetzt [1, 2, 3] -# Listen knnen addiert werden +# Listen können addiert werden li + other_li #=> [1, 2, 3, 4, 5, 6] - Hinweis: li und other_li werden in Ruhe gelassen -# Listen mit extend verknpfen +# Listen mit extend verknüpfen li.extend(other_li) # Jetzt ist li [1, 2, 3, 4, 5, 6] -# Mit in auf Existenz eines Elements prfen +# Mit in auf Existenz eines Elements prüfen 1 in li #=> True -# Die Lnge der Liste mit len ermitteln +# Die Länge der Liste mit len ermitteln len(li) #=> 6 -# Tupel sind wie Listen, nur unvernderlich. +# Tupel sind wie Listen, nur unveränderlich. tup = (1, 2, 3) tup[0] #=> 1 -tup[0] = 3 # Lst einen TypeError aus +tup[0] = 3 # Löst einen TypeError aus -# Wir knnen all diese Listen-Dinge auch mit Tupeln anstellen +# Wir können all diese Listen-Dinge auch mit Tupeln anstellen len(tup) #=> 3 tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) 2 in tup #=> True -# Wir knnen Tupel (oder Listen) in Variablen entpacken +# Wir können Tupel (oder Listen) in Variablen entpacken a, b, c = (1, 2, 3) # a ist jetzt 1, b ist jetzt 2 und c ist jetzt 3 -# Tuple werden standardmig erstellt, wenn wir uns die Klammern sparen +# Tuple werden standardmäßig erstellt, wenn wir uns die Klammern sparen d, e, f = 4, 5, 6 # Es ist kinderleicht zwei Werte zu tauschen e, d = d, e # d is now 5 and e is now 4 -# Dictionarys (Wrterbucher) speichern Key-Value-Paare +# Dictionarys (Wörterbucher) speichern Key-Value-Paare empty_dict = {} -# Hier ein geflltes Wrterbuch +# Hier ein gefülltes Wörterbuch filled_dict = {"one": 1, "two": 2, "three": 3} -# Wir knnen Eintrge mit [] nachschlagen +# Wir können Einträge mit [] nachschlagen filled_dict["one"] #=> 1 -# So holen wir alle Keys (Schlssel) als Liste +# So holen wir alle Keys (Schlüssel) als Liste filled_dict.keys() #=> ["three", "two", "one"] -# Hinweis - Die Reihenfolge von Schlsseln in der Liste ist nicht garantiert. -# Einzelne Resultate knnen anders angeordnet sein. +# Hinweis - Die Reihenfolge von Schlüsseln in der Liste ist nicht garantiert. +# Einzelne Resultate können anders angeordnet sein. # Alle Values (Werte) als Liste filled_dict.values() #=> [3, 2, 1] -# Hinweis - Hier gelten dieselben Einschrnkungen fr die Reihenfolge wie bei Schlsseln. +# Hinweis - Hier gelten dieselben Einschränkungen für die Reihenfolge wie bei Schlüsseln. -# Das Vorhandensein eines Schlssels im Wrterbuch mit in prfen +# Das Vorhandensein eines Schlüssels im Wörterbuch mit in prüfen "one" in filled_dict #=> True 1 in filled_dict #=> False -# Einen nicht vorhandenenen Schlssel zu suchen, lst einen KeyError aus +# Einen nicht vorhandenenen Schlüssel zu suchen, löst einen KeyError aus filled_dict["four"] # KeyError # Mit der get-Methode verhindern wir das filled_dict.get("one") #=> 1 filled_dict.get("four") #=> None -# Die get-Methode untersttzt auch ein Standardargument, falls der Wert fehlt +# Die get-Methode unterstützt auch ein Standardargument, falls der Wert fehlt filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 -# Die setdefault-Methode ist ein sicherer Weg, ein neues Schlssel-Wert-Paar anzulegen +# Die setdefault-Methode ist ein sicherer Weg, ein neues Schlüssel-Wert-Paar anzulegen filled_dict.setdefault("five", 5) #filled_dict["five"] wird auf 5 gesetzt filled_dict.setdefault("five", 6) #filled_dict["five"] ist noch immer 5 @@ -235,7 +235,7 @@ some_set = set([1,2,2,3,4]) # some_set ist jetzt set([1, 2, 3, 4]) # Seit Python 2.7 kann {} benutzt werden, um ein Set zu erstellen filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} -# Mehr Elemente hinzufgen +# Mehr Elemente hinzufügen filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} # Schnittmengen werden mit & gebildet @@ -248,7 +248,7 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} # Die Differenz einer Menge mit - bilden {1,2,3,4} - {2,3,5} #=> {1, 4} -# Auf Vorhandensein mit in prfen +# Auf Vorhandensein mit in prüfen 2 in filled_set #=> True 10 in filled_set #=> False @@ -260,26 +260,26 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} # Erstellen wir mal eine Variable some_var = 5 -# Hier eine if-Anweisung. Die Einrckung ist in Python wichtig! +# Hier eine if-Anweisung. Die Einrückung ist in Python wichtig! # gibt "some_var ist kleiner als 10" aus if some_var > 10: - print "some_var ist viel grer als 10." + print "some_var ist viel größer als 10." elif some_var < 10: # Dieser elif-Absatz ist optional. print "some_var ist kleiner als 10." else: # Das hier ist auch optional. - print "some_var ist tatschlich 10." + print "some_var ist tatsächlich 10." """ -For-Schleifen iterieren ber Listen +For-Schleifen iterieren über Listen Ausgabe: - hund ist ein Sugetier - katze ist ein Sugetier - maus ist ein Sugetier + hund ist ein Säugetier + katze ist ein Säugetier + maus ist ein Säugetier """ for animal in ["hund", "katze", "maus"]: - # Wir knnen Strings mit % formatieren - print "%s ist ein Sugetier" % animal + # Wir können Strings mit % formatieren + print "%s ist ein Säugetier" % animal """ `range(Zahl)` gibt eine null-basierte Liste bis zur angegebenen Zahl wieder @@ -293,7 +293,7 @@ for i in range(4): print i """ -While-Schleifen laufen, bis eine Bedingung erfllt ist. +While-Schleifen laufen, bis eine Bedingung erfüllt ist. Ausgabe: 0 1 @@ -303,16 +303,16 @@ Ausgabe: x = 0 while x < 4: print x - x += 1 # Kurzform fr x = x + 1 + x += 1 # Kurzform für x = x + 1 # Ausnahmebehandlung mit einem try/except-Block -# Funktioniert in Python 2.6 und hher: +# Funktioniert in Python 2.6 und höher: try: # Mit raise wird ein Fehler ausgegeben raise IndexError("Das hier ist ein Index-Fehler") except IndexError as e: - pass # Pass ist nur eine no-op. Normalerweise wrden wir hier den Fehler klren. + pass # Pass ist nur eine no-op. Normalerweise würden wir hier den Fehler klären. #################################################### @@ -322,30 +322,30 @@ except IndexError as e: # Mit def neue Funktionen erstellen def add(x, y): print "x ist %s und y ist %s" % (x, y) - return x + y # Werte werden mit return zurckgegeben + return x + y # Werte werden mit return zurückgegeben # Funktionen mit Parametern aufrufen -add(5, 6) #=> Ausgabe ist "x ist 5 und y ist 6" und gibt 11 zurck +add(5, 6) #=> Ausgabe ist "x ist 5 und y ist 6" und gibt 11 zurück -# Ein anderer Weg des Funktionsaufrufs sind Schlsselwort-Argumente -add(y=6, x=5) # Schlsselwrter knnen in beliebiger Reihenfolge bergeben werden. +# Ein anderer Weg des Funktionsaufrufs sind Schlüsselwort-Argumente +add(y=6, x=5) # Schlüsselwörter können in beliebiger Reihenfolge übergeben werden. -# Wir knnen Funktionen mit beliebiger Anzahl von # Positionsargumenten definieren +# Wir können Funktionen mit beliebiger Anzahl von # Positionsargumenten definieren def varargs(*args): return args varargs(1, 2, 3) #=> (1,2,3) -# Wir knnen auch Funktionen mit beliebiger Anzahl -# Schlsselwort-Argumenten definieren +# Wir können auch Funktionen mit beliebiger Anzahl +# Schlüsselwort-Argumenten definieren def keyword_args(**kwargs): return kwargs # Rufen wir es mal auf, um zu sehen, was passiert keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} -# Wir knnen beides gleichzeitig machem, wenn wir wollen +# Wir können beides gleichzeitig machem, wenn wir wollen def all_the_args(*args, **kwargs): print args print kwargs @@ -355,13 +355,13 @@ all_the_args(1, 2, a=3, b=4) Ausgabe: {"a": 3, "b": 4} """ -# Beim Aufruf von Funktionen knnen wir das Gegenteil von varargs/kwargs machen! -# Wir benutzen dann *, um Tupel auszuweiten, und ** fr kwargs. +# Beim Aufruf von Funktionen können wir das Gegenteil von varargs/kwargs machen! +# Wir benutzen dann *, um Tupel auszuweiten, und ** für kwargs. args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -all_the_args(*args) # quivalent zu foo(1, 2, 3, 4) -all_the_args(**kwargs) # quivalent zu foo(a=3, b=4) -all_the_args(*args, **kwargs) # quivalent zu foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # äquivalent zu foo(1, 2, 3, 4) +all_the_args(**kwargs) # äquivalent zu foo(a=3, b=4) +all_the_args(*args, **kwargs) # äquivalent zu foo(1, 2, 3, 4, a=3, b=4) # Python hat First-Class-Funktionen def create_adder(x): @@ -375,11 +375,11 @@ add_10(3) #=> 13 # Es gibt auch anonyme Funktionen (lambda x: x > 2)(3) #=> True -# Es gibt auch Funktionen hherer Ordnung als Built-Ins +# Es gibt auch Funktionen höherer Ordnung als Built-Ins map(add_10, [1,2,3]) #=> [11, 12, 13] filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] -# Wir knnen bei map- und filter-Funktionen auch List Comprehensions einsetzen +# Wir können bei map- und filter-Funktionen auch List Comprehensions einsetzen [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] @@ -424,7 +424,7 @@ print j.say("hello") #gibt "Joel: hello" aus # Rufen wir mal unsere Klassenmethode auf i.get_species() #=> "H. sapiens" -# ndern wir mal das gemeinsame Attribut +# Ändern wir mal das gemeinsame Attribut Human.species = "H. neanderthalensis" i.get_species() #=> "H. neanderthalensis" j.get_species() #=> "H. neanderthalensis" @@ -437,28 +437,28 @@ Human.grunt() #=> "*grunt*" ## 6. Module #################################################### -# Wir knnen Module importieren +# Wir können Module importieren import math print math.sqrt(16) #=> 4 -# Wir knnen auch nur spezielle Funktionen eines Moduls importieren +# Wir können auch nur spezielle Funktionen eines Moduls importieren from math import ceil, floor print ceil(3.7) #=> 4.0 print floor(3.7) #=> 3.0 -# Wir knnen auch alle Funktionen eines Moduls importieren +# Wir können auch alle Funktionen eines Moduls importieren # Warnung: Dies wird nicht empfohlen from math import * -# Wir knnen Modulnamen abkrzen +# Wir können Modulnamen abkürzen import math as m math.sqrt(16) == m.sqrt(16) #=> True -# Module sind in Python nur gewhnliche Dateien. Wir -# knnen unsere eigenen schreiben und importieren. Der Name des +# Module sind in Python nur gewöhnliche Dateien. Wir +# können unsere eigenen schreiben und importieren. Der Name des # Moduls ist der Dateiname. -# Wir knnen auch die Funktionen und Attribute eines +# Wir können auch die Funktionen und Attribute eines # Moduls herausfinden. import math dir(math) -- cgit v1.2.3 From f1692b1576e7f5a8193b1973020fce05c7b0f531 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 17 Aug 2013 09:44:28 -0700 Subject: Updated header matter in python-de --- de-de/python-de.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/de-de/python-de.html.markdown b/de-de/python-de.html.markdown index 7298c62c..6803f98b 100644 --- a/de-de/python-de.html.markdown +++ b/de-de/python-de.html.markdown @@ -2,8 +2,9 @@ language: python contributors: - ["Louie Dinh", "http://ldinh.ca"] - - ["kultprok", "http:/www.kulturproktologie.de"] -filename: learnpython.py + - ["kultprok", "http:/www.kulturproktologie.de"] +filename: learnpython-de.py +lang: de-de --- Anmerkungen des ursprünglichen Autors: -- cgit v1.2.3 From 655ef1d66fc564e394cdb2a370f5a8dad723c3c4 Mon Sep 17 00:00:00 2001 From: Roberto Perez Date: Sat, 17 Aug 2013 12:44:19 -0500 Subject: Groovy: v 1.0 --- groovy.html.markdown | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/groovy.html.markdown b/groovy.html.markdown index 3a928787..5333935c 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -2,7 +2,7 @@ language: Groovy filename: learngroovy.groovy contributors: - - ["Roberto Perez Alcolea", "http://github.com/rpalcolea"] + - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"] filename: learngroovy.groovy --- @@ -182,6 +182,179 @@ for ( e in map ) { x += e.value } +/* + Operators + + Operator Overloading for a list of the common operators that Groovy supports: http://groovy.codehaus.org/Operator+Overloading + + Helpful groovy operators +*/ +//Spread operator: invoke an action on all items of an aggregate object. +def technologies = ['Groovy','Grails','Gradle'] +technologies*.toUpperCase() //equivalent to: technologies.collect { it?.toUpperCase() } + +//Safe navigation operator: used to avoid a NullPointerException. +def user = User.get(1) +def username = user?.username + + +/* + Closures + A Groovy Closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point. + + More info at: http://groovy.codehaus.org/Closures+-+Formal+Definition +*/ +//Example: +def clos = { println "Hello World!" } + +println "Executing the Closure:" +clos() + +//Passing parameters to a closure +def sum = { a, b -> println a+b } +sum(2,4) + +//Closures may refer to variables not listed in their parameter list. +def x = 5 +def multiplyBy = { num -> num * x } +println multiplyBy(10) + +//If you have a Closure that takes a single argument, you may omit the parameter definition of the Closure +def clos = { print it } +clos( "hi" ) + +/* + Groovy can memorize closure results: + More info at: + http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ + http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize + http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html +/* +def cl = {a, b -> + sleep(3000) // simulate some time consuming processing + a + b +} + +mem = cl.memoize() + +def callClosure(a, b) { + def start = System.currentTimeMillis() + mem(a, b) + println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs." +} + +callClosure(1, 2) +callClosure(1, 2) +callClosure(2, 3) +callClosure(2, 3) +callClosure(3, 4) +callClosure(3, 4) +callClosure(1, 2) +callClosure(2, 3) +callClosure(3, 4) + +/* + Expando + + The Expando class is a dynamic bean so we can add properties and we can add closures as methods to an instance of this class + + Reference: http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html +*/ + def user = new Expando(name:"Roberto") + assert 'Roberto' == user.name + + user.lastName = 'Pérez' + assert 'Pérez' == user.lastName + + user.showInfo = { out -> + out << "Name: $name" + out << ", Last name: $lastName" + } + + def sw = new StringWriter() + println user.showInfo(sw) + + +/* + Metaprogramming (MOP) +*/ + +//Using ExpandoMetaClass to add behaviour +String.metaClass.testAdd = { + println "we added this" +} + +String x = "test" +x?.testAdd() + +//Intercepting method calls +class Test implements GroovyInterceptable { + def sum(Integer x, Integer y) { x + y } + + def invokeMethod(String name, args) { + System.out.println "Invoke method $name with args: $args" + } +} + +def test = new Test() +test?.sum(2,3) +test?.multiply(2,3) + +//Groovy supports propertyMissing for dealing with property resolution attempts. +class Foo { + def propertyMissing(String name) { name } +} +def f = new Foo() + +assertEquals "boo", f.boo + +/* + TypeChecked and CompileStatic + Groovy, by nature, is and will always be a dynamic language but it supports typechecked and compilestatic + + More info: http://www.infoq.com/articles/new-groovy-20 +*/ +//TypeChecked +import groovy.transform.TypeChecked + +void testMethod() {} + +@TypeChecked +void test() { + testMeethod() + + def name = "Roberto" + + println naameee + +} + +//Another example: +import groovy.transform.TypeChecked + +@TypeChecked +Integer test() { + Integer num = "1" + + Integer[] numbers = [1,2,3,4] + + Date date = numbers[1] + + return "Test" + +} + +//CompileStatic example: +import groovy.transform.CompileStatic + +@CompileStatic +int sum(int x, int y) { + x + y +} + +assert sum(2,5) == 7 + + ``` ## Further resources -- cgit v1.2.3 From 77cb25bf9b35b5a8bae6bf5e3a3ff1a4fa9ec438 Mon Sep 17 00:00:00 2001 From: Roberto Perez Date: Sat, 17 Aug 2013 12:45:13 -0500 Subject: Groovy: v 1.0 --- groovy.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groovy.html.markdown b/groovy.html.markdown index 5333935c..e4c2180b 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -229,7 +229,7 @@ clos( "hi" ) http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html -/* +*/ def cl = {a, b -> sleep(3000) // simulate some time consuming processing a + b -- cgit v1.2.3 From 0312c061b292985a5d849253480b0a8e5eafb332 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:35:59 +0200 Subject: C#: Added myself to contributors list --- csharp.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0cef8f05..b113b6d3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -3,6 +3,7 @@ language: c# contributors: - ["Irfan Charania", "https://github.com/irfancharania"] + - ["Max Yankov", "https://github.com/golergka"] filename: LearnCSharp.cs --- -- cgit v1.2.3 From b488745fc646b16df4ef87ddb746af596ea6588f Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:45:01 +0200 Subject: Static field --- csharp.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index b113b6d3..3de1eb66 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -403,7 +403,12 @@ namespace Learning private int _speed; // Private: Only accessible from within the class protected int gear; // Protected: Accessible from the class and subclasses internal int wheels; // Internal: Accessible from within the assembly - string name; // default: Only accessible from within this class + string name; // Everything is private by default: Only accessible from within this class + + // Static members belong to the type itself rather then specific object. + static public int bicyclesCreated = 0; + // You can access them without a reference to any object: + // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); // readonly values are set at run time // they can only be assigned upon declaration or in a constructor @@ -417,6 +422,7 @@ namespace Learning cadence = 50; _speed = 5; name = "Bontrager"; + bicyclesCreated++; } // This is a specified constructor (it contains arguments) -- cgit v1.2.3 From 964476a6e859959a3c1ae6a005ba82ba0d6b234e Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:45:58 +0200 Subject: C#: Lacking static members --- csharp.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 3de1eb66..584d40fd 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -535,6 +535,7 @@ namespace Learning * Enums, Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions + * Static properties, methods and classes * Exceptions, Interfaces, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3 From bb1f4eb93a70882f959acf6e849a9663ad74deca Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:49:36 +0200 Subject: C#: edited example code according to Microsoft's naming conventions --- csharp.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 584d40fd..0740c3ed 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -450,29 +450,29 @@ namespace Learning // Method declaration syntax: // () - public int getCadence() + public int GetCadence() { return cadence; } // void methods require no return statement - public void setCadence(int newValue) + public void SetCadence(int newValue) { cadence = newValue; } // virtual keyword indicates this method can be overridden - public virtual void setGear(int newValue) + public virtual void SetGear(int newValue) { gear = newValue; } - public void speedUp(int increment) + public void SpeedUp(int increment) { _speed += increment; } - public void slowDown(int decrement) + public void SlowDown(int decrement) { _speed -= decrement; } @@ -521,7 +521,7 @@ namespace Learning { } - public override void setGear(int gear) + public override void SetGear(int gear) { gear = 0; } -- cgit v1.2.3 From ccdc21900d090a88ca02e25007f4e3c197b8f50e Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:08:24 +0200 Subject: C#: enums --- csharp.html.markdown | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0740c3ed..fefcb0bb 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -405,6 +405,19 @@ namespace Learning internal int wheels; // Internal: Accessible from within the assembly string name; // Everything is private by default: Only accessible from within this class + // Enum is a value type that consists of a set of named constants + public enum Brand + { + AIST, + BMC, + Electra, + Gitane + } + // We defined this type inside a Bicycle class, so it is a nested type + // Code outside of this class should reference this type as Bicycle.Brand + + public Brand brand; // After declaing an enum type, we can declare the field of this type + // Static members belong to the type itself rather then specific object. static public int bicyclesCreated = 0; // You can access them without a reference to any object: @@ -416,28 +429,30 @@ namespace Learning // Constructors are a way of creating classes // This is a default constructor - public Bicycle() + private Bicycle() { gear = 1; cadence = 50; _speed = 5; name = "Bontrager"; + brand = Brand.AIST; bicyclesCreated++; } // This is a specified constructor (it contains arguments) public Bicycle(int startCadence, int startSpeed, int startGear, - string name, bool hasCardsInSpokes) + string name, bool hasCardsInSpokes, Brand brand) { this.gear = startGear; // "this" keyword denotes the current object this.cadence = startCadence; this._speed = startSpeed; this.name = name; // it can be useful when there's a name conflict this.hasCardsInSpokes = hasCardsInSpokes; + this.brand = brand; } // Constructors can be chained - public Bicycle(int startCadence, int startSpeed) : + public Bicycle(int startCadence, int startSpeed, Brand brand) : this(startCadence, startSpeed, 0, "big wheels", true) { } @@ -532,7 +547,7 @@ namespace Learning ## Topics Not Covered - * Enums, Flags + * Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions * Static properties, methods and classes -- cgit v1.2.3 From dd1a6193605909f3d6abf7795cd18261b27696b9 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:09:07 +0200 Subject: C#: default parameter values --- csharp.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index fefcb0bb..599aaca3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -482,12 +482,13 @@ namespace Learning gear = newValue; } - public void SpeedUp(int increment) + // Method parameters can have defaut values. In this case, methods can be called with these parameters omitted + public void SpeedUp(int increment = 1) { _speed += increment; } - public void SlowDown(int decrement) + public void SlowDown(int decrement = 1) { _speed -= decrement; } -- cgit v1.2.3 From 41ec7af8e5ac7e4a7ab8b8fc008466be98b0cdc4 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:09:21 +0200 Subject: Static methods --- csharp.html.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 599aaca3..22fb58e1 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -523,6 +523,14 @@ namespace Learning "\n------------------------------\n" ; } + + // Methods can also be static. It can be useful for helper methods + public static bool DidWeCreateEnoughBycles() + { + // Within a static method, we only can reference static class memebers + return bicyclesCreated > 9000; + } // If your class only needs static members, consider marking the class itself as static. + } // end class Bicycle // PennyFarthing is a subclass of Bicycle @@ -551,7 +559,7 @@ namespace Learning * Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions - * Static properties, methods and classes + * Static properties * Exceptions, Interfaces, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3 From cae70d430e40478e2c916489e53ffcb24f8fe024 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:28:21 +0200 Subject: C#: calling base method --- csharp.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 22fb58e1..0012fcb3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -549,6 +549,13 @@ namespace Learning { gear = 0; } + + public override string ToString() + { + string result = "PennyFarthing bicycle "; + result += base.ToString(); // Calling the base version of the method + return reuslt; + } } } // End Namespace -- cgit v1.2.3 From a225349acb165084a9da95f2da631a7525c513d3 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:28:55 +0200 Subject: C#: interfaces --- csharp.html.markdown | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0012fcb3..55de415d 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -557,6 +557,36 @@ namespace Learning return reuslt; } } + + // Interfaces only contain signatures of the members, without the implementation. + interface IJumpable + { + void Jump(int meters); // all interface members are implicitly public + } + + interface IBreakable + { + bool Broken { get; } // interfaces can contain properties as well as methods, fields & events + } + + // Class can inherit only one other class, but can implement any amount of interfaces + class MountainBike : Bicycle, IJumpable, IBreakable + { + int damage = 0; + + public void Jump(int meters) + { + damage += meters; + } + + public void Broken + { + get + { + return damage > 100; + } + } + } } // End Namespace ``` @@ -567,7 +597,7 @@ namespace Learning * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions * Static properties - * Exceptions, Interfaces, Abstraction + * Exceptions, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) * Winforms -- cgit v1.2.3 From dcdfd9114f3d606ede2a3d4967e7699579427e46 Mon Sep 17 00:00:00 2001 From: JakeHurlbut Date: Sat, 17 Aug 2013 18:06:23 -0400 Subject: Corrected Array Object NSLog Call --- objective-c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index b92e3218..9e9f43e7 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -90,7 +90,7 @@ int main (int argc, const char * argv[]) // Array object NSArray *anArray = @[@1, @2, @3, @4]; NSNumber *thirdNumber = anArray[2]; - NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3" + NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3" // Dictionary object NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; -- cgit v1.2.3 From 17733c473c700b0ecd9ecce481c985caf1ddffba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Szab=C3=B3?= Date: Sun, 18 Aug 2013 09:44:10 +0200 Subject: Added hungarian translation of Golang --- hu-hu/go.html.markdown | 305 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 hu-hu/go.html.markdown diff --git a/hu-hu/go.html.markdown b/hu-hu/go.html.markdown new file mode 100644 index 00000000..b3e8c1ca --- /dev/null +++ b/hu-hu/go.html.markdown @@ -0,0 +1,305 @@ +--- +name: Go +category: language +language: Go +filename: learngo.go +contributors: + - ["Sonia Keys", "https://github.com/soniakeys"] +translators: + - ["Szabó Krisztián", "https://github.com/thenonameguy/"] +--- + +A Go programozási nyelv az életszerű feladatok könnyebb elvégzése miatt született. +A mai legújabb programozási trendeket elkerülve, +praktikus megoldást nyújt a valós, üzleti problémákra. + +C-szerű szintaktikával és statikus típuskezeléssel rendelkezik. +A fordító szempillantás alatt végez és egy gyorsan futó,statikus futtatható állományt hoz létre. +A nyelv könnyen érthető, üzenet-alapú konkurenciát tesz lehetővé, így könnyen ki lehet használni +a mai számítógépek több magos processzorait, ez nagy rendszerek építéséhez ideális. + +A Go alap könyvtára mindenre területre kiterjed, ennek köszönhetően a nyelvnek egyre növekvő tábora van. + +```Go +// Egy soros komment +/* Több + soros komment */ + +// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python csomagkezelésére +// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz létre egy könyvtár helyett. +package main + +// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a forrásfájlban +import ( + "fmt" // A Go alap könyvtárának része + "net/http" // Beépített webszerver! + "strconv" // Stringek átalakítására szolgáló csomag +) + +// Funkció deklarás, a main nevű funkció a program kezdőpontja. +func main() { + // Println kiírja a beadott paramétereket a standard kimenetre. + // Ha más csomagot funkcióját akarjuk használni, akkor azt jelezni kell a csomag nevével + fmt.Println("Hello world!") + + // Meghívunk egy másik funkciót ebből a csomagból + beyondHello() +} + +// A függvények paraméterei zárójelek között vannak. +// Ha nincsenek paraméterek, akkor is kötelező a zárójel-pár. +func beyondHello() { + var x int // Változó deklaráció, használat előtt muszáj ezt megtenni. + x = 3 // Változó értékadás + // "Rövid" deklaráció is létezik, ez az érték alapján deklarálja, definiálja és értéket is ad a változónak + y := 4 + sum, prod := learnMultiple(x, y) // a függvényeknek több visszatérési értéke is lehet + fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás + learnTypes() +} + +// A funkcióknak elnevezett visszatérési értékük is lehet +func learnMultiple(x, y int) (sum, prod int) { + return x + y, x * y // visszatérünk két értékkel + /* + sum = x + y + prod = x * y + return + Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor. + Üres return esetén, az elnevezett visszatérési változók + aktuális értékeikkel térnek vissza. */ +} + +// Beépített típusok +func learnTypes() { + // Rövid deklarás az esetek többségében elég lesz a változókhoz + s := "Tanulj Go-t!" // string típus + + s2 := `A "nyers" stringekben lehetnek + újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön típusa + + // nem ASCII karakterek. Minden Go forrás UTF-8 és a stringek is azok. + g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert tárol + + f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites lebegőpontos szám + c := 3 + 4i // complex128, belsőleg két float64-el tárolva + + // Var szintaxis változó típus definiálással + var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az int-nél + var pi float32 = 22. / 7 + + // Rövid deklarásnál átalakítás is lehetséges + n := byte('\n') // byte típus, ami megegyezik az uint8-al + + // A tömböknek fordítás-időben fixált méretük van + var a4 [4]int // egy tömb 4 int-tel, mind 0-ra inicializálva + a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi értékekre + + // Szeleteknek dinamikus a méretük. A szeleteknek és a tömböknek is meg vannak az előnyeik + // de a szeleteket sokkal gyakrabban használjuk. + s3 := []int{4, 5, 9} // vesd össze a3-al, nincsenek pontok. + s4 := make([]int, 4) // allokál 4 int-et, mind 0-ra inicializálva + var d2 [][]float64 // ez csak deklaráció, semmi sincs még allokálva + bs := []byte("a slice") // típus konverzió szintaxisa + + p, q := learnMemory() // deklarál két mutatót (p,q), két int-re + fmt.Println(*p, *q) // * követi a mutatót. Ez a sor kiírja a két int értékét. + + // A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít + // a hash és dictionary típusokra más nyelvekben. + m := map[string]int{"three": 3, "four": 4} + m["one"] = 1 + + // A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban. + // Az aláhúzással "használod" a változókat, de eldobod az értéküket. + _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs + // Kiíratás is természetesen használatnak minősül + fmt.Println(s, c, a4, s3, d2, m) + + learnFlowControl() +} + +// A Go nyelv teljesen szemétgyűjtött (garbage-collected). Megtalálhatók benne mutatók, de nincs mutató aritmetika. +// Ez azt jelenti, hogy üres mutatóval még mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet. +func learnMemory() (p, q *int) { + // Elnevezett visszatérési változóknak int-re mutató a típusa + p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát allokál, és visszaad rá egy mutatót. + // Az allokált int nullázva van, p többé nem üres mutató. + s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen. + s[3] = 7 // adjunk értéket az egyiknek + r := -2 // hozzánk létre egy lokális változót + return &s[3], &r // A & megadja a memóriacímét a változónak +} + +func expensiveComputation() int { + return 1e6 +} + +func learnFlowControl() { + // Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges. + if true { + fmt.Println("megmondtam") + } + // A kód formátumát a nyelvvel járó "go" parancssori program "go fmt" parancsa szabványosítja + if false { + // így lehet + } else { + // if/else-t csinálni + } + // Használjunk switchet a hosszabb elágazások alkalmazása helyett. + x := 1 + switch x { + case 0: + case 1: + // Az "esetek" nem "esnek át", tehát + case 2: + // ez nem fog lefutni, nincs szükség break-ekre. + } + // A for ciklus sem használ zárójeleket + for x := 0; x < 3; x++ { + fmt.Println("iteráció", x) + } + // itt az x == 1. + + // A for az egyetlen ciklus fajta a Go-ban, de több formája van. + for { // végtelen ciklus + break // csak vicceltem + continue // soha nem fut le + } + // Akárcsak a for-nál, az if-ben is lehet rövid deklarással egy lokális változót létrehozni + // ami az blokk összes if/else-n keresztül érvényes marad. + if y := expensiveComputation(); y > x { + x = y + } + // Függvényeket használhatjuk closure-ként is. + xBig := func() bool { + return x > 100 // a switch felett deklarált x-et használjuk itt + } + fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek) + x /= 1e5 // így most már x == 10 + fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis + + // Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t. + goto love +love: + + learnInterfaces() // Itt kezdődnek az érdekes dolgok! +} + +// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a String, ami visszatér egy stringgel. +type Stringer interface { + String() string +} + +// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van, x és y. +type pair struct { + x, y int +} + +// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer interf +func (p pair) String() string { // p lesz a "vevő" + // Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s megfelelőjével. + // A pontokkal érjük el a mindenkori p struktúra elemeit + return fmt.Sprintf("(%d, %d)", p.x, p.y) +} + +func learnInterfaces() { + // A kapcsos zárójellel jelezzük, hogy egyből inicializálni + // szeretnénk a struktúra változóit a sorrendnek megfelelően. + p := pair{3, 4} + fmt.Println(p.String()) // meghívjuk a p String metódusát. + var i Stringer // deklaráljuk i-t Stringer típusú interfésznek + i = p // lehetséges, mert a pair struktúra eleget tesz a Stringer interfésznek + // Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb. + fmt.Println(i.String()) + + // Az fmt csomag funckciói automatikusan meghívják a String funkciót + // hogy megtudják egy objektum szöveges reprezentációját. + fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja a String metódust. + fmt.Println(i) // dettó + + learnErrorHandling() +} + +func learnErrorHandling() { + // ", ok" szokásos megoldás arra, hogy jól működött-e a függvény. + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban. + fmt.Println("nincs meg") + } else { + fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban. + } + // A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden "ok" volt-e + if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket, úgy se lesz jó jelen esetben + // kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } + // Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás! + learnConcurrency() +} + +// c egy csatorna, egy konkurens-biztos kommunikációs objektum. +func inc(i int, c chan int) { + c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így i+1-et küld be a csatornába +} + +// Az inc-et fogjuk arra használni, hogy konkurensen megnöveljünk számokat +func learnConcurrency() { + // Ugyan az a make funkció amivel korábban szeleteket hoztunk létre. + // Make allokál mapokat, szeleteket és csatornákat. + c := make(chan int) + // Indítsunk három konkurens goroutine-t. A számok konkurensen lesznek + // megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig paralellizálva/egymás mellett. + // Mind a 3 ugyanabba a csatornába küldi az eredményeket. + go inc(0, c) // A go utasítás indít el goroutine-okat. + go inc(10, c) + go inc(-805, c) + // Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre. + // Nem lehet tudni milyen sorrendben fognak érkezni az eredmények! + fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a "<-" a beolvasó/kapó operátor + + cs := make(chan string) // még egy csatorna, ez stringekkel kommunikál + cc := make(chan chan string) // egy csatorna csatornával + go func() { c <- 84 }() // indítsunk egy új goroutine-t, csak azért hogy küldjünk egy számot + go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet küldünk + // A select olyan mint a switch, csak feltételek helyett csatorna műveletek vannak. + // Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet kommunikáció. + select { + case i := <-c: // a megkapott értéket el lehet tárolni egy változóban + fmt.Println("ez egy", i) + case <-cs: // vagy el lehet dobni az értékét + fmt.Println("ez egy string volt") + case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni + fmt.Println("sose futok le :'( ") + } + // Ezen a ponton vagy c vagy a cs goroutineja lefutott. + // Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik blokkolva vár. + + learnWebProgramming() // Go képes rá. Te is képes akarsz rá lenni. +} + +// Egy funkció a http csomagból elindít egy webszervert. +func learnWebProgramming() { + // A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd. + // Második paramétere egy interfész, pontosabban a http.Handler interfész. + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // nem felejtük el kiírni az esetleges hibákat! +} + +// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az egyetlen metódusát a ServeHTTP-t. +func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // Minden kapcsolatra elküldjük ezt a http.ResponseWriter-el + w.Write([]byte("Megtanultad a Go-t Y perc alatt!")) +} +``` + +## További olvasmányok + +Minden Go-val kapcsolatos megtaláható a [hivatalos Go weboldalon](http://golang.org/). +Ott követhetsz egy tutorialt, játszhatsz a nyelvvel az interneten és sok érdekességet olvashatsz. + +A nyelv specifikációját kifejezetten érdemes olvasni, viszonylag rövid és sokat tanul belőle az ember. + +Ha pedig jobban bele akarod vetni magad a Go-ba, akkor a standard könyvtárban a legjobb praktikákat kilesheted. +TIPP: a dokumentációban kattints egy funkció nevére és rögtön megmutatja a hozzá tartozó kódot! + -- cgit v1.2.3 From 2e465d0e7cce9cfb3d54d2a76a023610e29c2755 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 18 Aug 2013 15:44:31 +0700 Subject: Added Russian Translation of Ruby --- ru-ru/ruby-ru.html.markdown | 390 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 390 insertions(+) create mode 100644 ru-ru/ruby-ru.html.markdown diff --git a/ru-ru/ruby-ru.html.markdown b/ru-ru/ruby-ru.html.markdown new file mode 100644 index 00000000..7d48d0e3 --- /dev/null +++ b/ru-ru/ruby-ru.html.markdown @@ -0,0 +1,390 @@ +--- +language: ruby +filename: learnruby.rb +contributors: + - ["Alexey Makarov", "https://github.com/Anakros"] +--- + +```ruby +# Это комментарий + +=begin +Это многострочный комментарий +Никто их не использует +И они не рекомендуются к использованию +=end + +# Первое и самое главное: Всё является объектом. + +# Числа это объекты + +3.class #=> Fixnum + +3.to_s #=> "3" + + +# Немного простой арифметики +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Арифметика -- это синтаксический сахар +# над вызовом метода для объекта +1.+(3) #=> 4 +10.* 5 #=> 50 + +# Логические величины -- это объекты +nil # Здесь ничего нет +true # правда +false # ложь + +nil.class #=> NilClass +true.class #=> TrueClass +false.class #=> FalseClass + +# Операция равенства +1 == 1 #=> true +2 == 1 #=> false + +# Операция неравенства +1 != 1 #=> false +2 != 1 #=> true +!true #=> false +!false #=> true + +# nil -- имеет такое же логическое значение, как и false + +!nil #=> true +!false #=> true +!0 #=> false + +# Больше операций сравнения +1 < 10 #=> true +1 > 10 #=> false +2 <= 2 #=> true +2 >= 2 #=> true + +# Строки -- это объекты + +'Я строка'.class #=> String +"Я тоже строка".class #=> String + +placeholder = "использовать интерполяцию строк" +"Я могу #{placeholder}, когда создаю строку с двойными кавычками" +#=> "Я могу использовать интерполяцию строк, когда создаю строку с двойными кавычками" + + +# печатать в стандартный вывод +puts "Я печатаюсь!" + +# Переменные +x = 25 #=> 25 +x #=> 25 + +# Присваивание значения возвращает присвоенное значение +# Это позволяет делать множественные присваивания: + +x = y = 10 #=> 10 +x #=> 10 +y #=> 10 + +# По соглашению, используйте snake_case для имён переменных +snake_case = true + +# Используйте подробные имена для переменных +# Но не переборщите! +path_to_project_root = '/good/name/' +path = '/bad/name/' + +# Идентификаторы (тоже объекты) + +# Идентификаторы -- это неизменяемые, многоразовые константы. +# Для каждого идентификатора (кроме текста) сохраняется цифровой хэш. При последующем +# использовании идентификатора, заместо создания нового объекта, будет найден уже +# существующий по цифровому хэшу. Они часто используются вместо строк +# для ускорения работы приложений + +:pending.class #=> Symbol + +status = :pending + +status == :pending #=> true + +status == 'pending' #=> false + +status == :approved #=> false + +# Массивы + +# Это массив +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] + +# Массив может содержать различные типы значений + +[1, "hello", false] #=> [1, "hello", false] + +# Значение в массиве можно получить по индексу с левой границы +array[0] #=> 1 +array[12] #=> nil + +# Как и арифметика, доступ к значению в массиве +# это синтаксический сахар над вызовом метода для объекта +array.[] 0 #=> 1 +array.[] 12 #=> nil + +# Также, можно получить по индексу с правой границы +array[-1] #=> 5 + +# С заданными левой и правой границами индексов +array[2, 4] #=> [3, 4, 5] + +# Или с использованием диапазона значений +array[1..3] #=> [2, 3, 4] + +# Вот так можно добавить значение в массив +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# Хэши -- это массив пар ключ => значение. +# Хэши объявляются с использованием фигурных скобок: +hash = {'color' => 'green', 'number' => 5} + +hash.keys #=> ['color', 'number'] + +# Значение в хэше легко может быть найдено по ключу: +hash['color'] #=> 'green' +hash['number'] #=> 5 + +# Поиск по ключу, которого в хэше нет, вернёт nil: +hash['nothing here'] #=> nil + +# начиная с Ruby 1.9, существует специальный синтаксис +# при использовании идентификаторов как ключей хэша: + +new_hash = { defcon: 3, action: true} + +new_hash.keys #=> [:defcon, :action] + +# Массивы и Хэши -- перечисляемые типы данных +# У них есть много полезных методов, например: each, map, count и другие + +# Управление ходом выполнения (Управляющие структуры) + +if true + "if условие" +elsif false + "else if, условие" +else + "else, условие" +end + +for counter in 1..5 + puts "#итерация {counter}" +end +#=> итерация 1 +#=> итерация 2 +#=> итерация 3 +#=> итерация 4 +#=> итерация 5 + +# Однако, никто не использует "for" для циклов. +# Вместо него Вы должны использовать метод "each" вместе с блоком кода. +# +# Блок кода -- это один из вариантов создания замыканий (лямбды, анонимные функции). +# Блок может только передаваться методу, сам по себе он существовать не может. +# "for" не имеет своей области видимости и все переменные, объявленные в нём +# будут доступны отовсюду. "each" вместе с блоком создаёт свою область видимости. + +# Метод "each" для диапазона значений запускает блок кода один раз для каждого из значений диапазона +# Блок передаёт счётчик (counter) в качестве параметра. +# Вызов метода "each" с блоком выглядит следующим образом: + +(1..5).each do |counter| + puts "итерация #{counter}" +end +#=> итерация 1 +#=> итерация 2 +#=> итерация 3 +#=> итерация 4 +#=> итерация 5 + +# Вы также можете ограничивать блоки фигурными скобками: +(1..5).each {|counter| puts "итерация #{counter}"} + +# Содержимое управляющих структур также можно перебирать используя "each": +array.each do |element| + puts "#{element} -- часть массива" +end +hash.each do |key, value| + puts "#{key} -- это #{value}" +end + +counter = 1 +while counter <= 5 do + puts "итерация #{counter}" + counter += 1 +end +#=> итерация 1 +#=> итерация 2 +#=> итерация 3 +#=> итерация 4 +#=> итерация 5 + +grade = 'B' + +case grade +when 'A' + puts "Так держать, детка!" +when 'B' + puts "Тебе повезёт в следующий раз" +when 'C' + puts "Ты можешь сделать лучше" +when 'D' + puts "Выскоблил последнее" +when 'F' + puts "Ты провалился!" +else + puts "Альтернативная система оценок, да?" +end + +# Функции + +def double(x) + x * 2 +end + +# Функции (и все блоки) неявно возвращают значение последней операции +double(2) #=> 4 + +# Скобки необязательны, если возвращаемый результат однозначен +double 3 #=> 6 + +double double 3 #=> 12 + +def sum(x,y) + x + y +end + +# Аргументы метода разделены запятой +sum 3, 4 #=> 7 + +sum sum(3,4), 5 #=> 12 + +# yield +# Все методы имеют неявный, опциональный параметр, +# который может быть вызван с помощью инструкции "yield" + +def surround + puts "{" + yield + puts "}" +end + +surround { puts 'hello world' } + +# { +# hello world +# } + + +# Определение класса с помощью ключевого слова "class" +class Human + + # Переменная класса, она является общей для всех экземпляров класса + @@species = "H. sapiens" + + # Базовый метод-конструктор + def initialize(name, age=0) + # Присвоить аргумент "name" переменной "name" экземпляра класса + @name = name + # Если аргумент "age" не задан, мы используем значение по умолчанию из списка аргументов + @age = age + end + + # Базовый метод установки значения для переменной (setter) + def name=(name) + @name = name + end + + # Базовый метод получения значения переменной (getter) + def name + @name + end + + # Метод класса определяется с ключевым словом "self", + # чтобы можно было отличить его от метода экземпляра класса. + # Он может быть вызван только на уровне класса, не экземпляра. + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end + +end + + +# Создание экземпляра класса +jim = Human.new("Jim Halpert") + +dwight = Human.new("Dwight K. Schrute") + +# Давайте вызовем несколько методов этого класса +jim.species #=> "H. sapiens" +jim.name #=> "Jim Halpert" +jim.name = "Jim Halpert II" #=> "Jim Halpert II" +jim.name #=> "Jim Halpert II" +dwight.species #=> "H. sapiens" +dwight.name #=> "Dwight K. Schrute" + +# Вызов метода класса +Human.say("Hi") #=> "Hi" + +# Класс тоже объект в Ruby. Потому класс может иметь переменные экземпляра. +# Переменная класса доступна в классе, его экземплярах и его потомках. + +# Базовый класс +class Human + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + +# Производный класс (класс-потомок) +class Worker < Human +end + +Human.foo # 0 +Worker.foo # 0 + +Human.foo = 2 # 2 +Worker.foo # 2 + +# Переменная экземпляра класса недоступна в потомках этого класса. + +class Human + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Doctor < Human +end + +Human.bar # 0 +Doctor.bar # nil + +``` -- cgit v1.2.3 From 27a73fe1759dc049646b2a3e5feedf47d541b6e2 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 18 Aug 2013 15:52:30 +0700 Subject: Fixed filename, added previous contributors --- ru-ru/ruby-ru.html.markdown | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ru-ru/ruby-ru.html.markdown b/ru-ru/ruby-ru.html.markdown index 7d48d0e3..baa5e8b5 100644 --- a/ru-ru/ruby-ru.html.markdown +++ b/ru-ru/ruby-ru.html.markdown @@ -1,8 +1,13 @@ --- language: ruby -filename: learnruby.rb +filename: learnruby-ru.rb contributors: - - ["Alexey Makarov", "https://github.com/Anakros"] + - ["David Underwood", "http://theflyingdeveloper.com"] + - ["Joel Walden", "http://joelwalden.net"] + - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] + - ["Alexey Makarov", "https://github.com/Anakros"] --- ```ruby -- cgit v1.2.3 From e5f626f7c4acb6a1a87b1cb3f766de87b7bdc4ae Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 18 Aug 2013 17:59:29 +0700 Subject: - added russian "lang" in description - removed tabs - minor changes in the text --- ru-ru/ruby-ru.html.markdown | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/ru-ru/ruby-ru.html.markdown b/ru-ru/ruby-ru.html.markdown index baa5e8b5..0a8fbb09 100644 --- a/ru-ru/ruby-ru.html.markdown +++ b/ru-ru/ruby-ru.html.markdown @@ -1,13 +1,14 @@ --- language: ruby +lang: ru-ru filename: learnruby-ru.rb contributors: - - ["David Underwood", "http://theflyingdeveloper.com"] - - ["Joel Walden", "http://joelwalden.net"] - - ["Luke Holder", "http://twitter.com/lukeholder"] - - ["Tristan Hume", "http://thume.ca/"] - - ["Nick LaMuro", "https://github.com/NickLaMuro"] - - ["Alexey Makarov", "https://github.com/Anakros"] + - ["David Underwood", "http://theflyingdeveloper.com"] + - ["Joel Walden", "http://joelwalden.net"] + - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] + - ["Alexey Makarov", "https://github.com/Anakros"] --- ```ruby @@ -87,7 +88,7 @@ puts "Я печатаюсь!" x = 25 #=> 25 x #=> 25 -# Присваивание значения возвращает присвоенное значение +# Присваивание значения возвращает то самое присвоенное значение. # Это позволяет делать множественные присваивания: x = y = 10 #=> 10 @@ -150,7 +151,7 @@ array[1..3] #=> [2, 3, 4] # Вот так можно добавить значение в массив array << 6 #=> [1, 2, 3, 4, 5, 6] -# Хэши -- это массив пар ключ => значение. +# Хэши -- это массив пар "ключ => значение". # Хэши объявляются с использованием фигурных скобок: hash = {'color' => 'green', 'number' => 5} @@ -160,7 +161,7 @@ hash.keys #=> ['color', 'number'] hash['color'] #=> 'green' hash['number'] #=> 5 -# Поиск по ключу, которого в хэше нет, вернёт nil: +# Поиск по ключу, которого в хэше нет вернёт nil: hash['nothing here'] #=> nil # начиная с Ruby 1.9, существует специальный синтаксис @@ -200,7 +201,8 @@ end # "for" не имеет своей области видимости и все переменные, объявленные в нём # будут доступны отовсюду. "each" вместе с блоком создаёт свою область видимости. -# Метод "each" для диапазона значений запускает блок кода один раз для каждого из значений диапазона +# Метод "each" для диапазона значений запускает блок кода один раз +# для каждого из значений диапазона # Блок передаёт счётчик (counter) в качестве параметра. # Вызов метода "each" с блоком выглядит следующим образом: @@ -295,18 +297,19 @@ surround { puts 'hello world' } # Определение класса с помощью ключевого слова "class" class Human - # Переменная класса, она является общей для всех экземпляров класса + # Переменная класса, она является общей для всех экземпляров класса @@species = "H. sapiens" # Базовый метод-конструктор def initialize(name, age=0) - # Присвоить аргумент "name" переменной "name" экземпляра класса + # Присвоить аргумент "name" переменной "name" экземпляра класса @name = name - # Если аргумент "age" не задан, мы используем значение по умолчанию из списка аргументов + # Если аргумент "age" не задан, + # мы используем значение по умолчанию из списка аргументов @age = age end - # Базовый метод установки значения для переменной (setter) + # Базовый метод установки значения для переменной (setter) def name=(name) @name = name end @@ -316,9 +319,9 @@ class Human @name end - # Метод класса определяется с ключевым словом "self", - # чтобы можно было отличить его от метода экземпляра класса. - # Он может быть вызван только на уровне класса, не экземпляра. + # Метод класса определяется с ключевым словом "self", + # чтобы можно было отличить его от метода экземпляра класса. + # Он может быть вызван только на уровне класса, но не экземпляра. def self.say(msg) puts "#{msg}" end @@ -335,7 +338,7 @@ jim = Human.new("Jim Halpert") dwight = Human.new("Dwight K. Schrute") -# Давайте вызовем несколько методов этого класса +# Давайте вызовем несколько методов jim.species #=> "H. sapiens" jim.name #=> "Jim Halpert" jim.name = "Jim Halpert II" #=> "Jim Halpert II" -- cgit v1.2.3 From 0a7d50c097f36336adf92fd79144f370fd66caa2 Mon Sep 17 00:00:00 2001 From: kultprok Date: Sun, 18 Aug 2013 14:04:10 +0200 Subject: Added german translation for git. --- de-de/git-de.html.markdown | 374 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 374 insertions(+) create mode 100644 de-de/git-de.html.markdown diff --git a/de-de/git-de.html.markdown b/de-de/git-de.html.markdown new file mode 100644 index 00000000..471c7641 --- /dev/null +++ b/de-de/git-de.html.markdown @@ -0,0 +1,374 @@ +--- +category: tool +tool: git +contributors: + - ["Jake Prather", "http:#github.com/JakeHP"] + - ["kultprok", "http://www.kulturproktologie.de"] +filename: LearnGit.txt +lang: de-de +--- + +Git ist eine verteilte Versions- und Quellcodeverwaltung. + +Es nimmt Schnappschüsse der Projekte, um mit diesen Schnappschüssen verschiedene Versionen unterscheiden und den Quellcode verwalten zu können. + +Anmerkung des Übersetzers: Einige englische Begriffe wie *Repository*, *Commit* oder *Head* sind idiomatische Bestandteile im Umgang mit Git. Sie wurden nicht übersetzt. + +## Konzepte der Versionsverwaltung + +### Was ist Versionsverwaltung? + +Eine Versionskontrolle erfasst die Änderungen einer Datei oder eines Verzeichnisses im Verlauf der Zeit. + +### Zentrale im Vergleich mit verteilter Versionverwaltung + +* Zentrale Versionskontrolle konzentriert sich auf das Synchronisieren, Verfolgen und Sichern von Dateien. +* Verteilte Versionskontrolle konzentriert sich auf das Teilen der Änderungen. Jede Änderung hat eine eindeutige ID. +* Verteilte Systeme haben keine vorbestimmte Struktur. Ein SVN-ähnliches, zentrales System wäre mit Git ebenso umsetzbar. + +[Weiterführende Informationen](http://git-scm.com/book/en/Getting-Started-About-Version-Control) + +### Warum Git? + +* Ist offline einsetzbar. +* Einfache Kollaboration! +* Branching ist einfach! +* Merging ist einfach! +* Git ist schnell. +* Git ist flexibel. + +## Die Architektur von Git + + +### Repository (Repo) + +Ein Satz von Dateien, Verzeichnisen, Historieneinträgen, Commits und Heads. Stell es dir wie eine Quellcode-Datenstruktur vor, unter anderem mit der Eigenschaft, dass alle *Elemente* dir Zugriff auf die Revisionshistorie geben. + +Ein Repository besteht in Git aus dem .git-Verzeichnis und dem Arbeitsverzeichnis. + +### .git-Verzeichnis (Teil des Repositorys) + +Das .git-Verzeichnis enth? alle Einstellung, Logs, Branches, den HEAD und mehr. +[Ausführliche Übersicht](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html) + +### Arbeitsverzeichnis (Teil des Repositorys) + +Dies sind die Verzeichnisse und Dateien in deinem Repository. + +### Index (Teil des .git-Verzeichnisses) + +Der Index ist die die Staging-Area von Git. Es ist im Grunde eine Ebene, die Arbeitsverzeichnis vom Repository trennt. Sie gibt Entwicklern mehr Einfluss darüber, was ins Git-Repository eingeht. + +### Commit + +Ein Commit ist ein Schnappschuss von Uderungen in deinem Arbeitsverzeichnis. Wenn du zum Beispiel 5 Dateien hinzugefügt und 2 andere entfernt hast, werden diese Änderungen im Commit (Schnappschuss) enthalten sein. Dieser Commit kann dann in andere Repositorys gepusht werden. Oder nicht! + +### Branch + +Ein Branch, ein Ast oder Zweig, ist im Kern ein Pointer auf den letzten Commit, den du gemacht hast. Während des Commits wird der Pointer automatisch auf Stand gebracht und zeigt dann auf den neuen letzten Commit. + +### HEAD und head (Teil des .git-Verzeichnisses) + +HEAD ist ein Pointer auf den aktuellen Branch. Ein Repository hat nur einen *aktiven* HEAD. Ein head ist ein Pointer, der auf ein beliebige Zahl von heads zeigt. + +### Konzeptionelle Hintergründe + +* [Git For Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/) +* [Git For Designers](http://hoth.entp.com/output/git_for_designers.html) + + +## Befehle + + +### init + +Erstelle ein leeres Git-Repository. Die Einstellungen, gespeicherte Informationen und mehr zu diesem Git-Repository werden in einem Verzeichnis namens *.git* angelegt. + +```bash +$ git init +``` + +### config + +Hiermit werden Einstellungen vorgenommen. Dies kann das Repository, das System selbst oder globale Einstellungen betreffen. + +```bash +# Grundlegende Config-Variablen ausgeben und setzen +$ git config --global user.email +$ git config --global user.name + +$ git config --global user.email "MyEmail@Zoho.com" +$ git config --global user.name "My Name" +``` + +[Mehr über git config](http://git-scm.com/docs/git-config) + +### help + +Schnellzugriff auf extrem detaillierte Anleitungen zu allen Befehlen. Oder als Erinnerung zu semantischen Eigenheiten. + +```bash +# Übersicht gängiger Befehle +$ git help + +# Übersicht aller verfügbaren Befehle +$ git help -a + +# Befehlspezifische Hilfe - Bedienungsanleitung +# git help +$ git help add +$ git help commit +$ git help init +``` + +### status + +Zeigt die Unterschiede zwischen Index (im Grunde dein Arbeitsverzeichnis/-repository) und dem aktuellen HEAD an. + + +```bash +# Zeigt den Branch, nicht-verfolgte Dateien, Uderungen und andere Unterschiede an +$ git status + +# Anderes Wissenswertes über git status anzeigen +$ git help status +``` + +### add + +Hinzufügen von Dateien zum Arbeitsverzeichnis/-repository. Wenn du neue Dateien nicht mit *git add* zum Arbeitsverzeichnis hinzufügst, werden sie nicht in den Commit aufgenommen! + +```bash +# Fügt eine Datei deinem aktuellen Arbeitsverzeichnis hinzu +$ git add HelloWorld.java + +# Fügt eine Datei aus einem verschachtelten Verzeichnis hinzu +$ git add /path/to/file/HelloWorld.c + +# Reguläre Ausdrücke werden unterstützt! +$ git add ./*.java +``` + +### branch + +Verwalte alle Branches. Du kannst sie mit diesem Befehl ansehen, bearbeiten, neue erschaffen oder löschen. + +```bash +# Liste alle bestehenden Branches und Remotes auf +$ git branch -a + +# Erstelle einen neuen Branch +$ git branch myNewBranch + +# Lösche einen Branch +$ git branch -d myBranch + +# Benenne einen Branch um +# git branch -m +$ git branch -m myBranchName myNewBranchName + +# Ändere die Beschreibung eines Branchs +$ git branch myBranchName --edit-description +``` + +### checkout + +Bringt alle Dateien im Arbeitsverzeichnis auf den Stand des Index oder des angegebenen Branches. + +```bash +# Ein Repo auschecken - wenn nicht anders angegeben ist das der master +$ git checkout +# Einen bestimmten Branch auschecken +$ git checkout branchName +# Erstelle einen neuen Branch und wechsle zu ihm. Wie: "git branch ; git checkout " +$ git checkout -b newBranch +``` + +### clone + +Ein bestehendes Repository in ein neues Verzeichnis klonen oder kopieren. Es fügt außerdem für hedes geklonte Repo remote-tracking Branches hinzu. Du kannst auf diese Remote-Branches pushen. + +```bash +# Klone learnxinyminutes-docs +$ git clone https://github.com/adambard/learnxinyminutes-docs.git +``` + +### commit + +Speichert die aktuellen Inhalte des Index in einen neuen *Commit*. Dieser Commit enthält alle Änderungen und eine vom Benutzer erstellte Beschreibung der Änderungen. + +```bash +# Commit mit Beschreibung erstellen. +$ git commit -m "Added multiplyNumbers() function to HelloWorld.c" +``` + +### diff + +Zeigt die Unterschiede zwischen Dateien von Arbeitsverzeichnisse, dem Index und Commits an. + +```bash +# Unterschiede zwischen deinem Arbeitsverzeichnis und dem Index anzeigen +$ git diff + +# Unterschiede zwischen dem Index und dem aktuellsten Commit anzeigen +$ git diff --cached + +# Unterschiede zwischen deinem Arbeitsverzeichnis und dem aktuellsten Commit anzeigen +$ git diff HEAD +``` + +### grep + +Schnell ein Repository durchsuchen. + +Optionale Einstellungen: + +```bash +# Vielen Dank an Travis Jeffery für die Hinweise. +# Zeilennummerierung in grep-Suchergebnissen +$ git config --global grep.lineNumber true + +# Suchergebnisse lesbarer gestalten, auch Gruppierungen sind möglich +$ git config --global alias.g "grep --break --heading --line-number" +``` + +```bash +# Suche nach "variableName" in allen java-Dateien +$ git grep 'variableName' -- '*.java' + +# Suche nach eine Zeile, die "arrayListName" und "add" oder "remove" enthält +$ git grep -e 'arrayListName' --and \( -e add -e remove \) +``` + +Google ist dein Freund; für mehr Beispiele: +[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja) + +### log + +Zeige Commits für das Repository an. + +```bash +# Zeige alle Commits +$ git log + +# Zeige die Anzahl n an Commits +$ git log -n 10 + +# Zeige nur Merges an +$ git log --merges +``` + +### merge + +*Merge*, also verschmelze, alle Änderungen von externen Commits in den aktuellen Branch. + +```bash +# Merge den angegebenen Branch in den aktuellen. +$ git merge branchName + +# Erstelle immer einen Merge-Commit. +$ git merge --no-ff branchName +``` + +### mv + +Eine Datei umbenennen oder verschieben. + +```bash +# Umbenennen +$ git mv HelloWorld.c HelloNewWorld.c + +# Verschieben +$ git mv HelloWorld.c ./new/path/HelloWorld.c + +# Umbenennung oder Verschieben erzwingen +# "existingFile" besteht schon im Verzeichnis, wird überschrieben mit "myFile" +$ git mv -f myFile existingFile +``` + +### pull + +Führe einen Pull, zieht alle Daten, eines Repositorys und f?? einen Merge mit einem anderen Branch durch. + +```bash +# Update deines lokalen Repos, indem ein Merge der neuen Uderungen +# von den remote-liegenden "origin"- und "master"-Branches durchgef?? wird. +# git pull +# git pull => impliziter Verweis auf origin und master +$ git pull origin master + +# F?? einen Merge von Uderungen eines remote-Branch und ein Rebase +# des Branch-Commits im lokalen Repo durch. Wie: pull , git rebase " +$ git pull origin master --rebase +``` + +### push + +Führe einen Push, ein Hochladen, und einen Merge von Änderungen eines remote-Branch mit einem Branch aus. + +```bash +# Führe Push und Merge von Änderungen des lokalen Repo zu einem +# remote-Branch namens "origin" und dem "master"-Branch aus. +# git push +# git push => impliziter Verweis auf => git push origin master +$ git push origin master +``` + +### rebase (mit Vorsicht einsetzen) + +Nimm alle Änderungen, die in einem Branch durch Commits vorgenommen wurden, und übertrage sie auf einen anderen Branch. Achtung: Führe keinen Rebase von Commits durch, die auf ein öffentliches Repo gepusht wurden. + +```bash +# Rebase "experimentBranch" in den "master"-Branch +# git rebase +$ git rebase master experimentBranch +``` + +[Weiterführende Informationen](http://git-scm.com/book/en/Git-Branching-Rebasing) + +### reset (mit Vorsicht einsetzen) + +Setze den aktuellen HEAD auf den angegebenen Zustand zurück. So können Merges, Pulls, Commits, Hinzufügungen und andere Änderungen rückgängig gemacht werden. Es ist ein hervorragender Befehl, aber auch sehr gefährlich, wenn du nicht weißt, was du tust. + +```bash +# Setze die Staging-Area zurück, um dem letzten Commit zu entsprechen (das Verzeichnis bleibt unberührt) +$ git reset + +# Setze die Staging-Area zurück, um dem letzten Commit zu entsprechen und überschreibe das Arbeitsverzeichnis +$ git reset --hard + +# Bewegt die Spitze des Branches zu dem angegebenen Commit (das Verzeichnis bleibt unber??) +# Alle Uderungen bleiben im Verzeichnis erhalten +$ git reset 31f2bb1 + +# Bewegt die Spitze des Branches zurück zu dem angegebenen Commit +# und gleicht die Arbeitsverzeichnisse ab (löscht nicht vom Commit erfasste Änderungen und alle Commits, +# die dem angegebenen Commit folgen). +$ git reset --hard 31f2bb1 +``` + +### rm + +Das Gegenteil von *git add*. *git rm* löscht Dateien vom Arbeitsverzeichnis. + +```bash +# Entferne HelloWorld.c +$ git rm HelloWorld.c + +# Entferne eine Datei aus einem verschachtelten Verzeichnis +$ git rm /pather/to/the/file/HelloWorld.c +``` + +## Weiterführende Informationen + +* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1) + +* [git-scm - Video Tutorials](http://git-scm.com/videos) + +* [git-scm - Documentation](http://git-scm.com/docs) + +* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/) + +* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf) + +* [GitGuys](http://www.gitguys.com/) -- cgit v1.2.3 From 3a88d4f2443c91dba5a968e38ede2b4f145f152a Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 00:59:35 +0200 Subject: Stub bash file --- bash.html.markdown | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bash.html.markdown diff --git a/bash.html.markdown b/bash.html.markdown new file mode 100644 index 00000000..ea0a28da --- /dev/null +++ b/bash.html.markdown @@ -0,0 +1,44 @@ +--- + +language: bash +contributors: + - ["Max Yankov", "https://github.com/golergka"] +filename: LearnBash.sh + +--- + +Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as default shell on Linux and Mac OS X. +Nearly all examples below can be a part of a shell script or executed directly in the shell. + +[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html) + +```bash +#!/bin/sh +# First line of the script is shebang which tells the system how to execute the script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# As you already figured, comments start with #. Shebang is also a comment. + +# Simple hello world example: +echo 'Hello, world!' + +# Each command starts on a new line, or after semicolon: +echo 'This is the first line'; echo 'This is the second line' + +# Declaring a variable looks like this: +VARIABLE="Some string" + +# But not like this: +VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must execute and give an error because it couldn't be found. + +# Using the variable: +echo $VARIABLE +echo "$VARIABLE" + +# We have the usual if structure: +if true +then + echo "This is expected" +else + echo "And is was not" +fi + +``` \ No newline at end of file -- cgit v1.2.3 From 01f1419dd7d0fba8735cbafb4cff871e52604b07 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 01:14:00 +0200 Subject: Bash: user input and expressions --- bash.html.markdown | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bash.html.markdown b/bash.html.markdown index ea0a28da..1ddacc33 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -18,7 +18,7 @@ Nearly all examples below can be a part of a shell script or executed directly i # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: -echo 'Hello, world!' +echo Hello, world! # Each command starts on a new line, or after semicolon: echo 'This is the first line'; echo 'This is the second line' @@ -32,6 +32,12 @@ VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must e # Using the variable: echo $VARIABLE echo "$VARIABLE" +# When you use the variable itself — assign it, export it, or else — you write it's name without $. If you want to use variable's value, you should use $. + +# Reading a value from input: +echo "What's your name?" +read NAME # Note that we didn't need to declare new variable +echo Hello, $NAME! # We have the usual if structure: if true @@ -41,4 +47,7 @@ else echo "And is was not" fi +# Expressions are denoted with the following format: +echo $(( 10 + 5 )) + ``` \ No newline at end of file -- cgit v1.2.3 From a538c52fb444fc14782ceb8353f69da04d232e60 Mon Sep 17 00:00:00 2001 From: Darren Lin Date: Sat, 17 Aug 2013 17:33:32 -0700 Subject: expanded the bash tutorial --- bash.html.markdown | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/bash.html.markdown b/bash.html.markdown index 1ddacc33..4e1eff9e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -2,7 +2,7 @@ language: bash contributors: - - ["Max Yankov", "https://github.com/golergka"] + - ["Max Yankov", "https://github.com/golergka" - "Darren Lin", "https://github.com/CogBear"] filename: LearnBash.sh --- @@ -50,4 +50,24 @@ fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -``` \ No newline at end of file +# Commands can be substitued within other commands using $( ): +# The following command displays the number of files and directories in the current directory. +echo "There are $(ls | wc -l) items here." + +#Bash uses a case statement that works similarily to switch in Java and C++: +case "$VARIABLE" +in + #List patterns for the conditions you want to meet + 0) echo "There is a zero." + 1) echo "There is a one." + *) echo "It is not null." +esac + +#For loops iterate for as many arguments given: +#The contents of var $VARIABLE is printed three times. +for $VARIABLE in x y z +do + echo "$VARIABLE" +done + +``` -- cgit v1.2.3 From 3e8c292a10eabd9816f7b0ccb9249661fbb4c3be Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 14:25:20 +0200 Subject: Bash: commands, attributes, ls, grep & pipe --- bash.html.markdown | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bash.html.markdown b/bash.html.markdown index 4e1eff9e..8cf7be18 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -44,12 +44,23 @@ if true then echo "This is expected" else - echo "And is was not" + echo "And this is not" fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) +# Unlike other programming languages, bash is a shell — so it works in a context of current directory. +# You can list files and directories in the current directories with ls command: +ls + +# These commands have options that control their execution: +ls -l # Lists every file and directory on a separate line + +# Results of the previous command can be passed to the next command as input. +# grep command filters the input with provided patterns. That's how we can list txt files in the current directory: +ls -l | grep "\.txt" + # Commands can be substitued within other commands using $( ): # The following command displays the number of files and directories in the current directory. echo "There are $(ls | wc -l) items here." -- cgit v1.2.3 From 0ff5090a5d60a91bbd8612ca4db186732bf4da72 Mon Sep 17 00:00:00 2001 From: Tenor Biel Date: Sun, 18 Aug 2013 10:10:40 -0500 Subject: Updated documentation for new language behaivour --- whip.html.markdown | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/whip.html.markdown b/whip.html.markdown index b8852ecb..3429ec24 100644 --- a/whip.html.markdown +++ b/whip.html.markdown @@ -109,18 +109,18 @@ undefined ; user to indicate a value that hasn't been set ; Dictionaries are Whip's equivalent to JavaScript 'objects' or Python 'dicts' ; or Ruby 'hashes': an unordered collection of key-value pairs. -{"key1":"value1" "key2":2 3:3} +{"key1" "value1" "key2" 2 3 3} ; Keys are just values, either identifier, number, or string. -(def my_dict {my_key:"my_value" "my other key":4}) -; But in Whip, dictionaries get parsed like: value, colon, value; -; with whitespace between each. So that means -{"key": "value" +(def my_dict {my_key "my_value" "my other key" 4}) +; But in Whip, dictionaries get parsed like: value, whitespace, value; +; with more whitespace between each. So that means +{"key" "value" "another key" -: 1234 +1234 } ; is evaluated to the same as -{"key":"value" "another key":1234} +{"key" "value" "another key" 1234} ; Dictionary definitions can be accessed used the `at` function ; (like strings and lists.) @@ -220,8 +220,8 @@ undefined ; user to indicate a value that hasn't been set (max (1 2 3 4)) ; 4 ; If value is in list or object (elem 1 (1 2 3)) ; true -(elem "foo" {"foo":"bar"}) ; true -(elem "bar" {"foo":"bar"}) ; false +(elem "foo" {"foo" "bar"}) ; true +(elem "bar" {"foo" "bar"}) ; false ; Reverse list order (reverse (1 2 3 4)) ; => (4 3 2 1) ; If value is even or odd -- cgit v1.2.3 From 081564a08af8b048e0b6f61e92d14b19d0918b0c Mon Sep 17 00:00:00 2001 From: rduerig Date: Sun, 18 Aug 2013 23:39:09 +0200 Subject: improved python german a little bit --- de-de/python-de.html.markdown | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/de-de/python-de.html.markdown b/de-de/python-de.html.markdown index 6803f98b..7462d5f6 100644 --- a/de-de/python-de.html.markdown +++ b/de-de/python-de.html.markdown @@ -28,17 +28,17 @@ Hinweis: Dieser Beitrag bezieht sich besonders auf Python 2.7, er sollte aber au # Die Zahlen 3 #=> 3 -# Mathematik ist das, was man erwartet +# Mathematik funktioniert so, wie man das erwartet 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -# Division ist ein wenig kniffliger. Es ist ganzzahlige Division -# und rundet automatisch ab. +# Division ist ein wenig kniffliger. Ganze Zahlen werden ohne Rest dividiert +# und das Ergebnis wird automatisch abgerundet. 5 / 2 #=> 2 -# Um das zu ändern, müssen wir Gleitkommazahlen kennenlernen. +# Um das zu ändern, müssen wir Gleitkommazahlen einführen und benutzen 2.0 # Das ist eine Gleitkommazahl 11.0 / 4.0 #=> 2.75 Ahhh...schon besser @@ -57,7 +57,7 @@ not False #=> True 1 == 1 #=> True 2 == 1 #=> False -# Ungleichheit is != +# Ungleichheit ist != 1 != 1 #=> False 2 != 1 #=> True @@ -84,7 +84,7 @@ not False #=> True # Mit % können Strings formatiert werden, etwa so: "%s können %s werden" % ("Strings", "interpoliert") -# Ein neuerer Weg, um Strings zu formatieren, ist die format-Methode. +# Ein modernerer Weg, um Strings zu formatieren, ist die format-Methode. # Diese Methode wird bevorzugt "{0} können {1} werden".format("Strings", "formatiert") # Wir können Schlüsselwörter verwenden, wenn wir nicht abzählen wollen. @@ -112,7 +112,7 @@ None is None #=> True ## 2. Variablen und Collections #################################################### -# Ausgabe ist sehr einfach +# Textausgabe ist sehr einfach print "Ich bin Python. Schön, dich kennenzulernen!" @@ -120,8 +120,9 @@ print "Ich bin Python. Schön, dich kennenzulernen!" some_var = 5 # kleinschreibung_mit_unterstrichen entspricht der Norm some_var #=> 5 -# Eine noch nicht deklarierte Variable anzusprechen, löst eine Exception aus. -# Siehe Kontrollstruktur, um mehr über Ausnahmebehandlung zu lernen. +# Das Ansprechen einer noch nicht deklarierte Variable löst eine Exception aus. +# Unter "Kontrollstruktur" kann noch mehr über +# Ausnahmebehandlung erfahren werden. some_other_var # Löst einen NameError aus # if kann als Ausdruck verwendet werden @@ -139,7 +140,7 @@ li.append(4) #li ist jetzt [1, 2, 4] li.append(3) #li ist jetzt [1, 2, 4, 3] # Vom Ende der Liste mit pop entfernen li.pop() #=> 3 und li ist jetzt [1, 2, 4] -# Fügen wir es wieder hinzu +# und dann wieder hinzufügen li.append(3) # li ist jetzt wieder [1, 2, 4, 3]. # Greife auf Listen wie auf Arrays zu @@ -147,7 +148,7 @@ li[0] #=> 1 # Das letzte Element ansehen li[-1] #=> 3 -# Außerhalb der Liste ist es ein IndexError +# Bei Zugriffen außerhal der Liste kommt es jedoch zu einem IndexError li[4] # Raises an IndexError # Wir können uns Ranges mit Slice-Syntax ansehen @@ -249,7 +250,7 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} # Die Differenz einer Menge mit - bilden {1,2,3,4} - {2,3,5} #=> {1, 4} -# Auf Vorhandensein mit in prüfen +# Auf Vorhandensein von Elementen mit in prüfen 2 in filled_set #=> True 10 in filled_set #=> False @@ -417,7 +418,7 @@ class Human(object): # Eine Instanz einer Klasse erstellen i = Human(name="Ian") -print i.say("hi") # gitbt "Ian: hi" aus +print i.say("hi") # gibt "Ian: hi" aus j = Human("Joel") print j.say("hello") #gibt "Joel: hello" aus -- cgit v1.2.3 From dbf05283fc39c9c0f192bb0bd63d9945a6d140d2 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Mon, 19 Aug 2013 14:14:05 +0300 Subject: turkish translation for c language --- tr-tr/c-tr.html.markdown | 417 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 417 insertions(+) create mode 100644 tr-tr/c-tr.html.markdown diff --git a/tr-tr/c-tr.html.markdown b/tr-tr/c-tr.html.markdown new file mode 100644 index 00000000..1f9db274 --- /dev/null +++ b/tr-tr/c-tr.html.markdown @@ -0,0 +1,417 @@ +--- +name: c +category: language +language: c +filename: learnc-tr.c +contributors: + - ["Adam Bard", "http://adambard.com/"] +translators: + - ["Haydar KULEKCI", "http://scanf.info/"] +lang: tr-tr + +--- +/* +C halen modern yüksek performans bilgisayarların dili. + +C bir çok programcının kullandığı en düşük seviye dillerdendir, ama +salt hız ile daha fazlasını karşılar. C'nin bellek yönetiminden iyi +anlarsanız sizi isteğiniz yere götürecektir. + +```c +// Tek satır yorum // karakterleri ile başlar + +/* +Çoklu satırlı yorumlar bu şekilde görünür. +*/ + +// #include ile header dosyalarınız dahil edin. +#include +#include +#include + +// Fonksiyonlarınızı bir .h dosyasında ya da c dosyanızın üst tarafta +// tanımlayın. + +void function_1(); +void function_2(); + +// Programınızın giriş noktası main isimli bir fonksiyondur ve +// integer değer döner +int main() { + +// çıktıları yazdırmak için printf kullanılır, "print formatted" +// %d bir sayı tipidir, \n yeni satır karakteridir +printf("%d\n", 0); // => 0 karakteri yazdırılır. +// Tüm ifadeler noktalı virgül ile bitmelidir. + +/////////////////////////////////////// +// Tipler +/////////////////////////////////////// + +// Değişkenleri kullanmadan önce tanımlamalısınız. Bir değişken tanımlarken +// tipini belirtmelisiniz; bu tip onun byte olarak boyutunu belirler. + +// int değişken tipi 4 byte boyutundadır. +int x_int = 0; + +// short değişken tipi genellikle 2 byte boyutundadır. +short x_short = 0; + +// char tipi 1 byte boyutunu garanti eder. +char x_char = 0; +char y_char = 'y'; // Karakterler '' işaretleri arasına yazılır. + +// long tipi 4-8 byte olur; long long tipi en azından 64 bit garantiler. +long x_long = 0; +long long x_long_long = 0; + +// float tipi 32-bit kayan noktalı sayı boyutundadır. +float x_float = 0.0; + +// double değişken tipi 64-bit kayan noktalı yazı tipindedir. +double x_double = 0.0; + +// Integral türleri işaretsiz olabilir. Bunun anlamı, onlar eksi değer +// olamaz demektir, ama aynı boyuttaki işaretsiz bir sayının maksimum +// değeri işaretli bir sayının maksimum değeriden büyük olur. +unsigned char ux_char; +unsigned short ux_short; +unsigned int ux_int; +unsigned long long ux_long_long; + +// Diğer taraftan char, ki her zaman bir byte boyutundadır, bu tipler +// makinenize göre boyut değiştirir. sizeof(T) size bir değişkenin byte +// cinsinden boyutunu verir öyle ki bu tipin boyutunu taşınabilir bir +// şekilde ifade edilebilir +// Örneğin, +printf("%lu\n", sizeof(int)); // => 4 (on machines with 4-byte words) + +// Diziler somut bir boyut ile oluşturulmalıdır. +char my_char_array[20]; // Bu dizi 1 * 20 = 20 byte alan kaplar +int my_int_array[20]; // Bu dizi 4 * 20 = 80 byte alan kaplar + // (4-byte bir word varsayılır) + +// Şu şekilde bir diziyi 0 ile oluşturabilirsiniz: +char my_array[20] = {0}; + +// Dizinin elemanlarını indexlemek diğer diller gibidir, veya +// diğer diller C gibi. +my_array[0]; // => 0 + +// Diziler değişebilirdir (mutable); O sadece memory! +my_array[1] = 2; +printf("%d\n", my_array[1]); // => 2 + +// String'ler bir NUL (0x00) byte ile sonlandırılmış karakter dizileridir, +// bu string içerisinde özel bir karakter olan '\0' ile gösterilir. +// (Biz Nul byte'i string karakterleri arasında bulundurmamıza gerek +// yoktur; derleyici onu bizim için dizinin sonuna ekler.) +char a_string[20] = "This is a string"; +printf("%s\n", a_string); // %s bir string formatıdır. + +/* +a_string 16 karakter uzunluğundadır. +17. karakter NUL karakteridir. +18., 19. ve 20. karakterler tanımsızdır.(undefined) +*/ + +printf("%d\n", a_string[16]); // => 0 + +/////////////////////////////////////// +// Operatörler +/////////////////////////////////////// + +int i1 = 1, i2 = 2; // Çoklu tanımlama için kısayol. +float f1 = 1.0, f2 = 2.0; + +// Aritmatik basittir. +i1 + i2; // => 3 +i2 - i1; // => 1 +i2 * i1; // => 2 +i1 / i2; // => 0 (0.5'dir ama 0 a yuvarlanmıştır.) + +f1 / f2; // => 0.5, artı veya eksi epsilon + +// Modüler aritmetikte vardır. +11 % 3; // => 2 + +// Karşılaştırma operatörleri muhtemelen tanıdıktır, ama +// C'de boolean tipi yoktur. Bunun yerine sayı(int) kullanırız. +// 0 false yerine ve diğer herşey true yerine geçmektedir. +// (Karşılaştırma operatörleri her zaman 0 veya 1 dönmektedir.) +3 == 2; // => 0 (false) +3 != 2; // => 1 (true) +3 > 2; // => 1 +3 < 2; // => 0 +2 <= 2; // => 1 +2 >= 2; // => 1 + +// Sayılar üzerinde mantık işlemleri +!3; // => 0 (Logical not) +!0; // => 1 +1 && 1; // => 1 (Logical and) +0 && 1; // => 0 +0 || 1; // => 1 (Logical or) +0 || 0; // => 0 + +// Bit boyutunda işlem yapmak için operatörler +~0x0F; // => 0xF0 (bitwise negation) +0x0F & 0xF0; // => 0x00 (bitwise AND) +0x0F | 0xF0; // => 0xFF (bitwise OR) +0x04 ^ 0x0F; // => 0x0B (bitwise XOR) +0x01 << 1; // => 0x02 (bitwise left shift (by 1)) +0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) + +/////////////////////////////////////// +// Kontrol Yapıları +/////////////////////////////////////// + +if (0) { + printf("I am never run\n"); +} else if (0) { + printf("I am also never run\n"); +} else { + printf("I print\n"); +} + +// While Döngüsü +int ii = 0; +while (ii < 10) { + printf("%d, ", ii++); // ii++, ii değişkenini değerini kullandıktan sonra artırır. +} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +int kk = 0; +do { + printf("%d, ", kk); +} while (++kk < 10); // ++kk, kk değişkeninin değerini kullanmadan önce artırır. +// => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +// For Döngüsü +int jj; +for (jj=0; jj < 10; jj++) { + printf("%d, ", jj); +} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +printf("\n"); + +/////////////////////////////////////// +// Tip Dönüşümleri +/////////////////////////////////////// + +// C'de her değer bir tipe sahiptir, ama siz bir değeri bir başka tipe +// dönüştürebilirsiniz. + +int x_hex = 0x01; // Hex literatüründe değer atayabilirsiniz. + +// Türler arasındaki dönüşümde kendi değerini korumak için çalışacaktır. +printf("%d\n", x_hex); // => Prints 1 +printf("%d\n", (short) x_hex); // => Prints 1 +printf("%d\n", (char) x_hex); // => Prints 1 + +// Tip hiçbir hata vermeden taşacaktır(overflow). +printf("%d\n", (char) 257); // => 1 (Max char = 255) + +// Integral tipi kayan noktalı yazı tipine dönüştürülecektir ve tam tersi. +printf("%f\n", (float)100); // %f formats a float +printf("%lf\n", (double)100); // %lf formats a double +printf("%d\n", (char)100.0); + +/////////////////////////////////////// +// İşaretçiler (Pointers) +/////////////////////////////////////// + +// Bir işaretci bellek adresini barındıran bir değişkendir. Tanımlaması ile işaret +// edeceği verinin tipi de belirtilecektir. Değişkenlerininzi bellek adreslerini +// getirerek bellek ile ilgili karışıklığı ortadan kaldırabilirsiniz. + +int x = 0; +printf("%p\n", &x); // & işareti bir değişkenin bellek adresini getirmek için kullanılır. +// (%p işaretçilerin formatıdır) +// => Bazı bellek adresleri yazdırılacaktır. + + +// İşaretçiler tanımlanırken * ile başlar +int *px, not_a_pointer; // px sayı tipinde bir işaretçidir. +px = &x; // X değişkeninin bellek adresi px değişkeninde tutulmaktadır. +printf("%p\n", px); // => x değişkeninin bellek adresi yazdırılacaktır. +printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer)); +// => 64-bit sistemde "8, 4" yazdırılacaktır. + +// İşaretçinin adres olarak gösterdiği yerdeki değeri almak için +// değişkenin önüne * işareti ekleyiniz. +printf("%d\n", *px); // => 0 bastıracaktır, x in değeridir, + // çünkü px değişkeni x in adresini göstermektedir. + +// Ayrıca siz işaretçinin gösterdiği yerin değerini +// değiştirebilirsiniz. Burada referansı parantez içerisinde göstereceğiz +// çünkü ++ işleminin önceliği * işleminden yüksektir. +(*px)++; // px'in işaret ettiği değeri 1 artır. +printf("%d\n", *px); // => 1 yazdırılır. +printf("%d\n", x); // => 1 yazdırılır. + +int x_array[20]; // Diziler(arrays) bellekten yan yana bellek bloklarını + // tahsis etmek için iyi bir yöntemdir. +int xx; +for (xx=0; xx<20; xx++) { + x_array[xx] = 20 - xx; +} // x_array dizisi 20, 19, 18,... 2, 1 değerleri ile oluşturuluyor. + +// Bir sayı tipinde işaretçi tanımlanıyor ve x_array'i işaret ediyor. +int* x_ptr = x_array; +// x_ptr artık dizinin ilk elemanını işaret etmektedir (the integer 20). +// Bu çalışacaktır çünkü diziler(arrays) aslında sadece onların ilk +// elemanlarını gösteren birer işaretçidir. + +// Diziler ilk elemanlarını gösteren birer işaretçidirler. +printf("%d\n", *(x_ptr)); // => 20 yazılacaktır. +printf("%d\n", x_array[0]); // => 20 yazılacaktır. + +// İşaretçiler kendi tiplerinde artırılır ve azaltılır. +printf("%d\n", *(x_ptr + 1)); // => 19 yazılacaktır. +printf("%d\n", x_array[1]); // => 19 yazılacaktır. + +// Ayrıca dinamik olarak bir bellek bloğunu standart kütüphanede bulunan +// malloc fonksiyonu ile uygulamanız için ayırabilirsiniz. Bu fonksiyon +// byte türünden ayırmak istediğiniz bloğun boyutunu parametre olarak alır. +int* my_ptr = (int*) malloc(sizeof(int) * 20); +for (xx=0; xx<20; xx++) { + *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx 'de aynı zamanda çalışabilir +} // Bellekte 20, 19, 18, 17... 2, 1 (as ints) şeklinde oluşturulmuş olacaktır. + +// Eğer ayrımadığınız bir bellek adresini çağırırsanız +// öngörülmeyen bir değer dönecektir. +printf("%d\n", *(my_ptr + 21)); // => kim-bilir-ne-yazacak? + +// Malloc fonksiyonu ile ayrıdığınız bellek kısmı ile işiniz bittiğinde +// onu free fonksiyonu ile boşaltmalısınız, aksi durumda uygulamanız +// kapatılana kadar belleğin o kısmını kimse kullanamaz. +free(my_ptr); + +// Metin Dizileri(String) birer karakter dizisidir(char array), ama +// genelde karakter işaretçisi olarak kullanılır. +char* my_str = "This is my very own string"; + +printf("%c\n", *my_str); // => 'T' + +function_1(); +} // main fonksiyon sonu + +/////////////////////////////////////// +// Fonksiyonlar +/////////////////////////////////////// + +// Fonksiyon tanımlama sözdizimi: +// () + +int add_two_ints(int x1, int x2){ + return x1 + x2; // bir değer geri döndürmek için return kullanılır. +} + +/* +Fonksiyonlar pass-by-value'dür, ama isterseniz işaretçi referanslarını +kullanarak fonksiyona gönderilen parametrenin değerini değiştirebilirsiniz. + +Example: Bir metni tersine çevirme +*/ + +// Bir void konksiyonu hiç bir değer dönmez +void str_reverse(char* str_in){ + char tmp; + int ii=0, len = strlen(str_in); // Strlen C'nin standart kütüphanesinin bir fonksiyonu + for(ii=0; ii ".tset a si sihT" +*/ + +/////////////////////////////////////// +// Kullanıcı Tanımlı Tipler ve Yapılar +/////////////////////////////////////// + +// Typedef'ler bir tip takma adı oluşturur. +typedef int my_type; +my_type my_type_var = 0; + +// Struct'lar bir veri koleksiyonudur. +struct rectangle { + int width; + int height; +}; + + +void function_1(){ + + struct rectangle my_rec; + + // "." ile yapı üyelerine ulaşılabilir. + my_rec.width = 10; + my_rec.height = 20; + + // Bir yapının adresini işaretçiye atayabilirsiniz. + struct rectangle* my_rec_ptr = &my_rec; + + // İşaretçi üzerinden bir yapıya ulaşabilirsiniz. + (*my_rec_ptr).width = 30; + + // ya da -> işareti ile yapının elemanlarına ulaşabilirsiniz. + my_rec_ptr->height = 10; // (*my_rec_ptr).height = 10; ile aynıdır. +} + +// Kolaylık sağlamak için bir yapıya typedef tanımlayabilirsiniz. +typedef struct rectangle rect; + +int area(rect r){ + return r.width * r.height; +} + +/////////////////////////////////////// +// Fonksiyon İşaretçiler +/////////////////////////////////////// + +/* +Çalışma zamanında, fonksiyonların bilinen bir bellek adresleri vardır. Fonksiyon +işaretçileri fonksiyonları direk olarak çağırmayı sağlayan veya geri bildirim(callback) +için parametre gönderirken kullanılan başka birer işaretçidirler. Ama, syntax tanımı +başlangıçta biraz karışık gelebilir. + +Örnek: bir işaretçiden str_reverse kullanımı +*/ +void str_reverse_through_pointer(char * str_in) { + // f adında bir fonksiyon işaretçisi tanımlanır. + void (*f)(char *); // Signature should exactly match the target function. + f = &str_reverse; // Assign the address for the actual function (determined at runtime) + (*f)(str_in); // Just calling the function through the pointer + // f(str_in); // That's an alternative but equally valid syntax for calling it. +} + +/* +As long as function signatures match, you can assign any function to the same pointer. +Function pointers are usually typedef'd for simplicity and readability, as follows: +*/ + +typedef void (*my_fnp_type)(char *); + +// Gerçek bir işaretçi tanımlandığı zaman ki kullanımı: +// ... +// my_fnp_type f; + +``` + +## Daha Fazla Okuma Listesi + +[K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language)'in bir kopyasını bulundurmak mükemmel olabilir + +Diğer bir iyi kaynak ise [Learn C the hard way](http://c.learncodethehardway.org/book/) + +Diğer taraftan google sizin için bir arkadaş olabilir. -- cgit v1.2.3 From f6b7d0b2c02de143df5992fa10e5284bf72132d3 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Mon, 19 Aug 2013 14:41:44 +0300 Subject: some updates turkish translation for latest updates of forked repo --- tr-tr/c-tr.html.markdown | 585 ++++++++++++++++++++++++++--------------------- 1 file changed, 326 insertions(+), 259 deletions(-) diff --git a/tr-tr/c-tr.html.markdown b/tr-tr/c-tr.html.markdown index 1f9db274..50bca246 100644 --- a/tr-tr/c-tr.html.markdown +++ b/tr-tr/c-tr.html.markdown @@ -24,11 +24,16 @@ anlarsanız sizi isteğiniz yere götürecektir. Çoklu satırlı yorumlar bu şekilde görünür. */ -// #include ile header dosyalarınız dahil edin. +// C Standart kütüphanelerini uygulamanıza #include ile +// dahil edebilirsiniz. #include #include #include +// Kendi başlık(header) dosyalarınız dahil etmek için "çift tırnak" +// kullanmalısınız. +#include "my_header.h" + // Fonksiyonlarınızı bir .h dosyasında ya da c dosyanızın üst tarafta // tanımlayın. @@ -39,265 +44,317 @@ void function_2(); // integer değer döner int main() { -// çıktıları yazdırmak için printf kullanılır, "print formatted" -// %d bir sayı tipidir, \n yeni satır karakteridir -printf("%d\n", 0); // => 0 karakteri yazdırılır. -// Tüm ifadeler noktalı virgül ile bitmelidir. - -/////////////////////////////////////// -// Tipler -/////////////////////////////////////// - -// Değişkenleri kullanmadan önce tanımlamalısınız. Bir değişken tanımlarken -// tipini belirtmelisiniz; bu tip onun byte olarak boyutunu belirler. - -// int değişken tipi 4 byte boyutundadır. -int x_int = 0; - -// short değişken tipi genellikle 2 byte boyutundadır. -short x_short = 0; - -// char tipi 1 byte boyutunu garanti eder. -char x_char = 0; -char y_char = 'y'; // Karakterler '' işaretleri arasına yazılır. - -// long tipi 4-8 byte olur; long long tipi en azından 64 bit garantiler. -long x_long = 0; -long long x_long_long = 0; - -// float tipi 32-bit kayan noktalı sayı boyutundadır. -float x_float = 0.0; - -// double değişken tipi 64-bit kayan noktalı yazı tipindedir. -double x_double = 0.0; - -// Integral türleri işaretsiz olabilir. Bunun anlamı, onlar eksi değer -// olamaz demektir, ama aynı boyuttaki işaretsiz bir sayının maksimum -// değeri işaretli bir sayının maksimum değeriden büyük olur. -unsigned char ux_char; -unsigned short ux_short; -unsigned int ux_int; -unsigned long long ux_long_long; - -// Diğer taraftan char, ki her zaman bir byte boyutundadır, bu tipler -// makinenize göre boyut değiştirir. sizeof(T) size bir değişkenin byte -// cinsinden boyutunu verir öyle ki bu tipin boyutunu taşınabilir bir -// şekilde ifade edilebilir -// Örneğin, -printf("%lu\n", sizeof(int)); // => 4 (on machines with 4-byte words) - -// Diziler somut bir boyut ile oluşturulmalıdır. -char my_char_array[20]; // Bu dizi 1 * 20 = 20 byte alan kaplar -int my_int_array[20]; // Bu dizi 4 * 20 = 80 byte alan kaplar - // (4-byte bir word varsayılır) - -// Şu şekilde bir diziyi 0 ile oluşturabilirsiniz: -char my_array[20] = {0}; - -// Dizinin elemanlarını indexlemek diğer diller gibidir, veya -// diğer diller C gibi. -my_array[0]; // => 0 - -// Diziler değişebilirdir (mutable); O sadece memory! -my_array[1] = 2; -printf("%d\n", my_array[1]); // => 2 - -// String'ler bir NUL (0x00) byte ile sonlandırılmış karakter dizileridir, -// bu string içerisinde özel bir karakter olan '\0' ile gösterilir. -// (Biz Nul byte'i string karakterleri arasında bulundurmamıza gerek -// yoktur; derleyici onu bizim için dizinin sonuna ekler.) -char a_string[20] = "This is a string"; -printf("%s\n", a_string); // %s bir string formatıdır. - -/* -a_string 16 karakter uzunluğundadır. -17. karakter NUL karakteridir. -18., 19. ve 20. karakterler tanımsızdır.(undefined) -*/ - -printf("%d\n", a_string[16]); // => 0 - -/////////////////////////////////////// -// Operatörler -/////////////////////////////////////// - -int i1 = 1, i2 = 2; // Çoklu tanımlama için kısayol. -float f1 = 1.0, f2 = 2.0; - -// Aritmatik basittir. -i1 + i2; // => 3 -i2 - i1; // => 1 -i2 * i1; // => 2 -i1 / i2; // => 0 (0.5'dir ama 0 a yuvarlanmıştır.) - -f1 / f2; // => 0.5, artı veya eksi epsilon - -// Modüler aritmetikte vardır. -11 % 3; // => 2 - -// Karşılaştırma operatörleri muhtemelen tanıdıktır, ama -// C'de boolean tipi yoktur. Bunun yerine sayı(int) kullanırız. -// 0 false yerine ve diğer herşey true yerine geçmektedir. -// (Karşılaştırma operatörleri her zaman 0 veya 1 dönmektedir.) -3 == 2; // => 0 (false) -3 != 2; // => 1 (true) -3 > 2; // => 1 -3 < 2; // => 0 -2 <= 2; // => 1 -2 >= 2; // => 1 - -// Sayılar üzerinde mantık işlemleri -!3; // => 0 (Logical not) -!0; // => 1 -1 && 1; // => 1 (Logical and) -0 && 1; // => 0 -0 || 1; // => 1 (Logical or) -0 || 0; // => 0 - -// Bit boyutunda işlem yapmak için operatörler -~0x0F; // => 0xF0 (bitwise negation) -0x0F & 0xF0; // => 0x00 (bitwise AND) -0x0F | 0xF0; // => 0xFF (bitwise OR) -0x04 ^ 0x0F; // => 0x0B (bitwise XOR) -0x01 << 1; // => 0x02 (bitwise left shift (by 1)) -0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) - -/////////////////////////////////////// -// Kontrol Yapıları -/////////////////////////////////////// - -if (0) { - printf("I am never run\n"); -} else if (0) { - printf("I am also never run\n"); -} else { - printf("I print\n"); -} - -// While Döngüsü -int ii = 0; -while (ii < 10) { - printf("%d, ", ii++); // ii++, ii değişkenini değerini kullandıktan sonra artırır. -} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -printf("\n"); - -int kk = 0; -do { - printf("%d, ", kk); -} while (++kk < 10); // ++kk, kk değişkeninin değerini kullanmadan önce artırır. -// => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -printf("\n"); - -// For Döngüsü -int jj; -for (jj=0; jj < 10; jj++) { - printf("%d, ", jj); -} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -printf("\n"); - -/////////////////////////////////////// -// Tip Dönüşümleri -/////////////////////////////////////// - -// C'de her değer bir tipe sahiptir, ama siz bir değeri bir başka tipe -// dönüştürebilirsiniz. - -int x_hex = 0x01; // Hex literatüründe değer atayabilirsiniz. - -// Türler arasındaki dönüşümde kendi değerini korumak için çalışacaktır. -printf("%d\n", x_hex); // => Prints 1 -printf("%d\n", (short) x_hex); // => Prints 1 -printf("%d\n", (char) x_hex); // => Prints 1 - -// Tip hiçbir hata vermeden taşacaktır(overflow). -printf("%d\n", (char) 257); // => 1 (Max char = 255) - -// Integral tipi kayan noktalı yazı tipine dönüştürülecektir ve tam tersi. -printf("%f\n", (float)100); // %f formats a float -printf("%lf\n", (double)100); // %lf formats a double -printf("%d\n", (char)100.0); - -/////////////////////////////////////// -// İşaretçiler (Pointers) -/////////////////////////////////////// + // çıktıları yazdırmak için printf kullanılır, "print formatted" + // %d bir sayı tipidir, \n yeni satır karakteridir + printf("%d\n", 0); // => 0 karakteri yazdırılır. + // Tüm ifadeler noktalı virgül ile bitmelidir. + + /////////////////////////////////////// + // Tipler + /////////////////////////////////////// + + // Değişkenleri kullanmadan önce tanımlamalısınız. Bir değişken tanımlarken + // tipini belirtmelisiniz; bu tip onun byte olarak boyutunu belirler. + + // int değişken tipi 4 byte boyutundadır. + int x_int = 0; + + // short değişken tipi genellikle 2 byte boyutundadır. + short x_short = 0; + + // char tipi 1 byte boyutunu garanti eder. + char x_char = 0; + char y_char = 'y'; // Karakterler '' işaretleri arasına yazılır. + + // long tipi 4-8 byte olur; long long tipi en azından 64 bit garantiler. + long x_long = 0; + long long x_long_long = 0; + + // float tipi 32-bit kayan noktalı sayı boyutundadır. + float x_float = 0.0; + + // double değişken tipi 64-bit kayan noktalı yazı tipindedir. + double x_double = 0.0; + + // Integral türleri işaretsiz olabilir. Bunun anlamı, onlar eksi değer + // olamaz demektir, ama aynı boyuttaki işaretsiz bir sayının maksimum + // değeri işaretli bir sayının maksimum değeriden büyük olur. + unsigned char ux_char; + unsigned short ux_short; + unsigned int ux_int; + unsigned long long ux_long_long; + + // Diğer taraftan char, ki her zaman bir byte boyutundadır, bu tipler + // makinenize göre boyut değiştirir. sizeof(T) size bir değişkenin byte + // cinsinden boyutunu verir öyle ki bu tipin boyutunu taşınabilir bir + // şekilde ifade edilebilir + // Örneğin, + printf("%lu\n", sizeof(int)); // => 4 (bir çok makinede 4-byte words) + + // If the argument of the `sizeof` operator an expression, then its argument + // is not evaluated (except VLAs (see below)). + // The value it yields in this case is a compile-time constant. + int a = 1; + size_t size = sizeof(a++); // a++ is not evaluated + printf("sizeof(a++) = %zu where a = %d\n", size, a); + // prints "sizeof(a++) = 4 where a = 1" (on a 32-bit architecture) + + // Diziler somut bir boyut ile oluşturulmalıdır. + char my_char_array[20]; // Bu dizi 1 * 20 = 20 byte alan kaplar + int my_int_array[20]; // Bu dizi 4 * 20 = 80 byte alan kaplar + // (4-byte bir word varsayılır) + + // Şu şekilde bir diziyi 0 ile oluşturabilirsiniz: + char my_array[20] = {0}; + + // Dizinin elemanlarını indexlemek diğer diller gibidir, veya + // diğer diller C gibi. + my_array[0]; // => 0 + + // Diziler değişebilirdir (mutable); O sadece memory! + my_array[1] = 2; + printf("%d\n", my_array[1]); // => 2 + + // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) + // can be declared as well. The size of such an array need not be a compile + // time constant: + printf("Enter the array size: "); // ask the user for an array size + char buf[0x100]; + fgets(buf, sizeof buf, stdin); + + // strtoul parses a string to an unsigned integer + size_t size = strtoul(buf, NULL, 10); + int var_length_array[size]; // declare the VLA + printf("sizeof array = %zu\n", sizeof var_length_array); + + // A possible outcome of this program may be: + // > Enter the array size: 10 + // > sizeof array = 40 + + // String'ler bir NUL (0x00) byte ile sonlandırılmış karakter dizileridir, + // bu string içerisinde özel bir karakter olan '\0' ile gösterilir. + // (Biz Nul byte'i string karakterleri arasında bulundurmamıza gerek + // yoktur; derleyici onu bizim için dizinin sonuna ekler.) + char a_string[20] = "This is a string"; + printf("%s\n", a_string); // %s bir string formatıdır. + + /* + a_string 16 karakter uzunluğundadır. + 17. karakter NUL karakteridir. + 18., 19. ve 20. karakterler tanımsızdır.(undefined) + */ + + printf("%d\n", a_string[16]); // => 0 + // i.e., byte #17 is 0 (as are 18, 19, and 20) + + // If we have characters between single quotes, that's a character literal. + // It's of type `int`, and *not* `char` (for historical reasons). + int cha = 'a'; // fine + char chb = 'a'; // fine too (implicit conversion from int to char) + + /////////////////////////////////////// + // Operatörler + /////////////////////////////////////// + + int i1 = 1, i2 = 2; // Çoklu tanımlama için kısayol. + float f1 = 1.0, f2 = 2.0; + + // Aritmatik basittir. + i1 + i2; // => 3 + i2 - i1; // => 1 + i2 * i1; // => 2 + i1 / i2; // => 0 (0.5'dir ama 0 a yuvarlanmıştır.) + + f1 / f2; // => 0.5, artı veya eksi epsilon + + // Modüler aritmetikte vardır. + 11 % 3; // => 2 + + // Karşılaştırma operatörleri muhtemelen tanıdıktır, ama + // C'de boolean tipi yoktur. Bunun yerine sayı(int) kullanırız. + // 0 false yerine ve diğer herşey true yerine geçmektedir. + // (Karşılaştırma operatörleri her zaman 0 veya 1 dönmektedir.) + 3 == 2; // => 0 (false) + 3 != 2; // => 1 (true) + 3 > 2; // => 1 + 3 < 2; // => 0 + 2 <= 2; // => 1 + 2 >= 2; // => 1 + + // Sayılar üzerinde mantık işlemleri + !3; // => 0 (Logical not) + !0; // => 1 + 1 && 1; // => 1 (Logical and) + 0 && 1; // => 0 + 0 || 1; // => 1 (Logical or) + 0 || 0; // => 0 + + // Bit boyutunda işlem yapmak için operatörler + ~0x0F; // => 0xF0 (bitwise negation) + 0x0F & 0xF0; // => 0x00 (bitwise AND) + 0x0F | 0xF0; // => 0xFF (bitwise OR) + 0x04 ^ 0x0F; // => 0x0B (bitwise XOR) + 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) + 0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) + + // Be careful when shifting signed integers - the following are undefined: + // - shifting into the sign bit of a signed integer (int a = 1 << 32) + // - left-shifting a negative number (int a = -1 << 2) + // - shifting by an offset which is >= the width of the type of the LHS: + // int a = 1 << 32; // UB if int is 32 bits wide + + /////////////////////////////////////// + // Kontrol Yapıları + /////////////////////////////////////// + + if (0) { + printf("I am never run\n"); + } else if (0) { + printf("I am also never run\n"); + } else { + printf("I print\n"); + } -// Bir işaretci bellek adresini barındıran bir değişkendir. Tanımlaması ile işaret -// edeceği verinin tipi de belirtilecektir. Değişkenlerininzi bellek adreslerini -// getirerek bellek ile ilgili karışıklığı ortadan kaldırabilirsiniz. - -int x = 0; -printf("%p\n", &x); // & işareti bir değişkenin bellek adresini getirmek için kullanılır. -// (%p işaretçilerin formatıdır) -// => Bazı bellek adresleri yazdırılacaktır. - - -// İşaretçiler tanımlanırken * ile başlar -int *px, not_a_pointer; // px sayı tipinde bir işaretçidir. -px = &x; // X değişkeninin bellek adresi px değişkeninde tutulmaktadır. -printf("%p\n", px); // => x değişkeninin bellek adresi yazdırılacaktır. -printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer)); -// => 64-bit sistemde "8, 4" yazdırılacaktır. - -// İşaretçinin adres olarak gösterdiği yerdeki değeri almak için -// değişkenin önüne * işareti ekleyiniz. -printf("%d\n", *px); // => 0 bastıracaktır, x in değeridir, - // çünkü px değişkeni x in adresini göstermektedir. - -// Ayrıca siz işaretçinin gösterdiği yerin değerini -// değiştirebilirsiniz. Burada referansı parantez içerisinde göstereceğiz -// çünkü ++ işleminin önceliği * işleminden yüksektir. -(*px)++; // px'in işaret ettiği değeri 1 artır. -printf("%d\n", *px); // => 1 yazdırılır. -printf("%d\n", x); // => 1 yazdırılır. - -int x_array[20]; // Diziler(arrays) bellekten yan yana bellek bloklarını - // tahsis etmek için iyi bir yöntemdir. -int xx; -for (xx=0; xx<20; xx++) { - x_array[xx] = 20 - xx; -} // x_array dizisi 20, 19, 18,... 2, 1 değerleri ile oluşturuluyor. - -// Bir sayı tipinde işaretçi tanımlanıyor ve x_array'i işaret ediyor. -int* x_ptr = x_array; -// x_ptr artık dizinin ilk elemanını işaret etmektedir (the integer 20). -// Bu çalışacaktır çünkü diziler(arrays) aslında sadece onların ilk -// elemanlarını gösteren birer işaretçidir. - -// Diziler ilk elemanlarını gösteren birer işaretçidirler. -printf("%d\n", *(x_ptr)); // => 20 yazılacaktır. -printf("%d\n", x_array[0]); // => 20 yazılacaktır. - -// İşaretçiler kendi tiplerinde artırılır ve azaltılır. -printf("%d\n", *(x_ptr + 1)); // => 19 yazılacaktır. -printf("%d\n", x_array[1]); // => 19 yazılacaktır. - -// Ayrıca dinamik olarak bir bellek bloğunu standart kütüphanede bulunan -// malloc fonksiyonu ile uygulamanız için ayırabilirsiniz. Bu fonksiyon -// byte türünden ayırmak istediğiniz bloğun boyutunu parametre olarak alır. -int* my_ptr = (int*) malloc(sizeof(int) * 20); -for (xx=0; xx<20; xx++) { - *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx 'de aynı zamanda çalışabilir -} // Bellekte 20, 19, 18, 17... 2, 1 (as ints) şeklinde oluşturulmuş olacaktır. - -// Eğer ayrımadığınız bir bellek adresini çağırırsanız -// öngörülmeyen bir değer dönecektir. -printf("%d\n", *(my_ptr + 21)); // => kim-bilir-ne-yazacak? - -// Malloc fonksiyonu ile ayrıdığınız bellek kısmı ile işiniz bittiğinde -// onu free fonksiyonu ile boşaltmalısınız, aksi durumda uygulamanız -// kapatılana kadar belleğin o kısmını kimse kullanamaz. -free(my_ptr); - -// Metin Dizileri(String) birer karakter dizisidir(char array), ama -// genelde karakter işaretçisi olarak kullanılır. -char* my_str = "This is my very own string"; - -printf("%c\n", *my_str); // => 'T' - -function_1(); + // While Döngüsü + int ii = 0; + while (ii < 10) { + printf("%d, ", ii++); // ii++, ii değişkenini değerini kullandıktan sonra artırır. + } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + + printf("\n"); + + int kk = 0; + do { + printf("%d, ", kk); + } while (++kk < 10); // ++kk, kk değişkeninin değerini kullanmadan önce artırır. + // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + + printf("\n"); + + // For Döngüsü + int jj; + for (jj=0; jj < 10; jj++) { + printf("%d, ", jj); + } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + + printf("\n"); + + /////////////////////////////////////// + // Tip Dönüşümleri + /////////////////////////////////////// + + // C'de her değer bir tipe sahiptir, ama siz bir değeri bir başka tipe + // dönüştürebilirsiniz. + + int x_hex = 0x01; // Hex literatüründe değer atayabilirsiniz. + + // Türler arasındaki dönüşümde kendi değerini korumak için çalışacaktır. + printf("%d\n", x_hex); // => Prints 1 + printf("%d\n", (short) x_hex); // => Prints 1 + printf("%d\n", (char) x_hex); // => Prints 1 + + // Tip hiçbir hata vermeden taşacaktır(overflow). + printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 eğer karakter 8 bit uzunluğunda ise) + + // `char`, `signed char` ve `unsigned char` karakter tiplerinin maksimum uzunluğunu + // belirlemek için kütüphanesindeki CHAR_MAX, SCHAR_MAX ve UCHAR_MAX + // macrolarını kullanınız. + + // Integral tipi kayan noktalı yazı tipine dönüştürülecektir ve tam tersi. + printf("%f\n", (float)100); // %f formats a float + printf("%lf\n", (double)100); // %lf formats a double + printf("%d\n", (char)100.0); + + /////////////////////////////////////// + // İşaretçiler (Pointers) + /////////////////////////////////////// + + // Bir işaretci bellek adresini barındıran bir değişkendir. Tanımlaması ile işaret + // edeceği verinin tipi de belirtilecektir. Değişkenlerininzi bellek adreslerini + // getirerek bellek ile ilgili karışıklığı ortadan kaldırabilirsiniz. + + int x = 0; + printf("%p\n", &x); // & işareti bir değişkenin bellek adresini getirmek için kullanılır. + // (%p işaretçilerin formatıdır) + // => Bazı bellek adresleri yazdırılacaktır. + + + // İşaretçiler tanımlanırken * ile başlar + int *px, not_a_pointer; // px sayı tipinde bir işaretçidir. + px = &x; // X değişkeninin bellek adresi px değişkeninde tutulmaktadır. + printf("%p\n", px); // => x değişkeninin bellek adresi yazdırılacaktır. + printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer)); + // => 64-bit sistemde "8, 4" yazdırılacaktır. + + // İşaretçinin adres olarak gösterdiği yerdeki değeri almak için + // değişkenin önüne * işareti ekleyiniz. + printf("%d\n", *px); // => 0 bastıracaktır, x in değeridir, + // çünkü px değişkeni x in adresini göstermektedir. + + // Ayrıca siz işaretçinin gösterdiği yerin değerini + // değiştirebilirsiniz. Burada referansı parantez içerisinde göstereceğiz + // çünkü ++ işleminin önceliği * işleminden yüksektir. + (*px)++; // px'in işaret ettiği değeri 1 artır. + printf("%d\n", *px); // => 1 yazdırılır. + printf("%d\n", x); // => 1 yazdırılır. + + int x_array[20]; // Diziler(arrays) bellekten yan yana bellek bloklarını + // tahsis etmek için iyi bir yöntemdir. + int xx; + for (xx=0; xx<20; xx++) { + x_array[xx] = 20 - xx; + } // x_array dizisi 20, 19, 18,... 2, 1 değerleri ile oluşturuluyor. + + // Bir sayı tipinde işaretçi tanımlanıyor ve x_array'i işaret ediyor. + int* x_ptr = x_array; + // x_ptr artık dizinin ilk elemanını işaret etmektedir (the integer 20). + // Bu çalışacaktır çünkü diziler(arrays) aslında sadece onların ilk + // elemanlarını gösteren birer işaretçidir. + // For example, when an array is passed to a function or is assigned to a pointer, + // it decays into (implicitly converted to) a pointer. + // Exceptions: when the array is the argument of the `&` (address-od) operator: + int arr[10]; + int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! + // It's of type "pointer to array" (of ten `int`s). + // or when the array is a string literal used for initializing a char array: + char arr[] = "foobarbazquirk"; + // or when it's the argument of the `sizeof` or `alignof` operator: + int arr[10]; + int *ptr = arr; // equivalent with int *ptr = &arr[0]; + printf("%zu %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8" + + // Diziler ilk elemanlarını gösteren birer işaretçidirler. + printf("%d\n", *(x_ptr)); // => 20 yazılacaktır. + printf("%d\n", x_array[0]); // => 20 yazılacaktır. + + // İşaretçiler kendi tiplerinde artırılır ve azaltılır. + printf("%d\n", *(x_ptr + 1)); // => 19 yazılacaktır. + printf("%d\n", x_array[1]); // => 19 yazılacaktır. + + // Ayrıca dinamik olarak bir bellek bloğunu standart kütüphanede bulunan + // malloc fonksiyonu ile uygulamanız için ayırabilirsiniz. Bu fonksiyon + // byte türünden ayırmak istediğiniz bloğun boyutunu parametre olarak alır. + int* my_ptr = (int*) malloc(sizeof(int) * 20); + for (xx=0; xx<20; xx++) { + *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx 'de aynı zamanda çalışabilir + } // Bellekte 20, 19, 18, 17... 2, 1 (as ints) şeklinde oluşturulmuş olacaktır. + + // Eğer ayrımadığınız bir bellek adresini çağırırsanız + // öngörülmeyen bir değer dönecektir. + printf("%d\n", *(my_ptr + 21)); // => kim-bilir-ne-yazacak? + + // Malloc fonksiyonu ile ayrıdığınız bellek kısmı ile işiniz bittiğinde + // onu free fonksiyonu ile boşaltmalısınız, aksi durumda uygulamanız + // kapatılana kadar belleğin o kısmını kimse kullanamaz. + free(my_ptr); + + // Metin Dizileri(String) birer karakter dizisidir(char array), ama + // genelde karakter işaretçisi olarak kullanılır. + char* my_str = "This is my very own string"; + + printf("%c\n", *my_str); // => 'T' + + function_1(); } // main fonksiyon sonu /////////////////////////////////////// @@ -349,6 +406,10 @@ struct rectangle { int height; }; +// It's not generally true that +// sizeof(struct rectangle) == sizeof(int) + sizeof(int) +// due to potential padding between the structure members (this is for alignment +// reasons). [1] void function_1(){ @@ -414,4 +475,10 @@ typedef void (*my_fnp_type)(char *); Diğer bir iyi kaynak ise [Learn C the hard way](http://c.learncodethehardway.org/book/) +It's very important to use proper spacing, indentation and to be consistent with your coding style in general. +Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the +[Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). + Diğer taraftan google sizin için bir arkadaş olabilir. + +[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member \ No newline at end of file -- cgit v1.2.3 From f33dea8b83bf64ecde36337a5e02cae77f5210de Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 19 Aug 2013 09:14:02 -0700 Subject: Updates --- README.markdown | 41 ++++- bash.html.markdown | 43 +++-- groovy.html.markdown | 60 ++++--- hu-hu/go.html.markdown | 444 ++++++++++++++++++++++++++----------------------- 4 files changed, 338 insertions(+), 250 deletions(-) diff --git a/README.markdown b/README.markdown index efc2fa07..701b12d7 100644 --- a/README.markdown +++ b/README.markdown @@ -9,18 +9,47 @@ commented code and explained as they go. ... to write more inline code tutorials. Just grab an existing file from this repo and copy the formatting (don't worry, it's all very simple). Make a new file, send a pull request, and if it passes muster I'll get it up pronto. -Remember to fill in the author and author\_url fields so you get credited +Remember to fill in the "contributors" fields so you get credited properly! ### Requests -The most requested languages are: +We've had a ton of interest, b -* Go -* ~~Scala~~ -* ~~Javascript~~ +### Contributing -... but there are many more requests to do "every language", so don't let that stop you. +All contributions welcome, from the tiniest typo to a brand new article. Translations +in all languages are welcome (or, for that matter, original articles in any language). + +#### Style Guidelines + +* Try to keep **line length in code blocks to 80 characters or fewer**, or they'll overflow + and look odd. + +* Try to use as few words as possible. Code examples are preferred over exposition in all cases. + +* We welcome newcomers, but the target audience for this site is programmers with some experience. + So, try to avoid explaining basic concepts except for those specific to the language in question, + to keep articles succinct and scannable. We all know how to use google here. + +* For translations (or english articles with non-ASCII characters), please make sure your file is + utf-8 encoded. + +#### Header configuration + +The actual site uses Middleman to generate HTML files from these markdown ones. Middleman, or at least +the custom scripts underpinning the site, required that some key information be defined in the header. + +The following fields are necessary for english articles about programming languages: + +* **language** The *programming language* in question +* **contributors** A list of [author, url] lists to credit + +Other fields: + +* **filename**: The filename for this article's code. It will be fetched, mashed together, and made downloadable. + For non-english articles, *filename* should have a language-specific suffix. +* **lang**: For translations, the human language this article is in. For categorization, mostly. ## License diff --git a/bash.html.markdown b/bash.html.markdown index 8cf7be18..7421f880 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -1,8 +1,10 @@ --- -language: bash +category: tool +tool: bash contributors: - - ["Max Yankov", "https://github.com/golergka" - "Darren Lin", "https://github.com/CogBear"] + - ["Max Yankov", "https://github.com/golergka"] + - ["Darren Lin", "https://github.com/CogBear"] filename: LearnBash.sh --- @@ -14,7 +16,8 @@ Nearly all examples below can be a part of a shell script or executed directly i ```bash #!/bin/sh -# First line of the script is shebang which tells the system how to execute the script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# First line of the script is shebang which tells the system how to execute +# the script: http://en.wikipedia.org/wiki/Shebang_(Unix) # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: @@ -27,12 +30,15 @@ echo 'This is the first line'; echo 'This is the second line' VARIABLE="Some string" # But not like this: -VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must execute and give an error because it couldn't be found. +VARIABLE = "Some string" +# Bash will decide that VARIABLE is a command it must execute and give an error +# because it couldn't be found. # Using the variable: echo $VARIABLE echo "$VARIABLE" -# When you use the variable itself — assign it, export it, or else — you write it's name without $. If you want to use variable's value, you should use $. +# When you use the variable itself — assign it, export it, or else — you write +# its name without $. If you want to use variable's value, you should use $. # Reading a value from input: echo "What's your name?" @@ -42,43 +48,46 @@ echo Hello, $NAME! # We have the usual if structure: if true then - echo "This is expected" + echo "This is expected" else - echo "And this is not" + echo "And this is not" fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -# Unlike other programming languages, bash is a shell — so it works in a context of current directory. -# You can list files and directories in the current directories with ls command: +# Unlike other programming languages, bash is a shell — so it works in a context +# of current directory. You can list files and directories in the current +# directories with ls command: ls # These commands have options that control their execution: ls -l # Lists every file and directory on a separate line # Results of the previous command can be passed to the next command as input. -# grep command filters the input with provided patterns. That's how we can list txt files in the current directory: +# grep command filters the input with provided patterns. That's how we can list +# txt files in the current directory: ls -l | grep "\.txt" # Commands can be substitued within other commands using $( ): -# The following command displays the number of files and directories in the current directory. +# The following command displays the number of files and directories in the +# current directory. echo "There are $(ls | wc -l) items here." -#Bash uses a case statement that works similarily to switch in Java and C++: +# Bash uses a case statement that works similarily to switch in Java and C++: case "$VARIABLE" in - #List patterns for the conditions you want to meet - 0) echo "There is a zero." - 1) echo "There is a one." - *) echo "It is not null." + #List patterns for the conditions you want to meet + 0) echo "There is a zero." + 1) echo "There is a one." + *) echo "It is not null." esac #For loops iterate for as many arguments given: #The contents of var $VARIABLE is printed three times. for $VARIABLE in x y z do - echo "$VARIABLE" + echo "$VARIABLE" done ``` diff --git a/groovy.html.markdown b/groovy.html.markdown index e4c2180b..1a635e59 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -112,14 +112,31 @@ println devMap.values() When Groovy is compiled to bytecode, the following rules are used. - * If the name is declared with an access modifier (public, private or protected) then a field is generated. - * A name declared with no access modifier generates a private field with public getter and setter (i.e. a property). - * If a property is declared final the private field is created final and no setter is generated. + * If the name is declared with an access modifier (public, private or + protected) then a field is generated. + + * A name declared with no access modifier generates a private field with + public getter and setter (i.e. a property). + + * If a property is declared final the private field is created final and no + setter is generated. + * You can declare a property and also declare your own getter or setter. - * You can declare a property and a field of the same name, the property will use that field then. - * If you want a private or protected property you have to provide your own getter and setter which must be declared private or protected. - * If you access a property from within the class the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going though the getter and setter. - * If you access a property that does not exist using the explicit or implicit foo, then Groovy will access the property through the meta class, which may fail at runtime. + + * You can declare a property and a field of the same name, the property will + use that field then. + + * If you want a private or protected property you have to provide your own + getter and setter which must be declared private or protected. + + * If you access a property from within the class the property is defined in + at compile time with implicit or explicit this (for example this.foo, or + simply foo), Groovy will access the field directly instead of going though + the getter and setter. + + * If you access a property that does not exist using the explicit or + implicit foo, then Groovy will access the property through the meta class, + which may fail at runtime. */ @@ -185,13 +202,14 @@ for ( e in map ) { /* Operators - Operator Overloading for a list of the common operators that Groovy supports: http://groovy.codehaus.org/Operator+Overloading + Operator Overloading for a list of the common operators that Groovy supports: + http://groovy.codehaus.org/Operator+Overloading Helpful groovy operators */ //Spread operator: invoke an action on all items of an aggregate object. def technologies = ['Groovy','Grails','Gradle'] -technologies*.toUpperCase() //equivalent to: technologies.collect { it?.toUpperCase() } +technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() } //Safe navigation operator: used to avoid a NullPointerException. def user = User.get(1) @@ -200,7 +218,8 @@ def username = user?.username /* Closures - A Groovy Closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point. + A Groovy Closure is like a "code block" or a method pointer. It is a piece of + code that is defined and then executed at a later point. More info at: http://groovy.codehaus.org/Closures+-+Formal+Definition */ @@ -219,16 +238,13 @@ def x = 5 def multiplyBy = { num -> num * x } println multiplyBy(10) -//If you have a Closure that takes a single argument, you may omit the parameter definition of the Closure +// If you have a Closure that takes a single argument, you may omit the +// parameter definition of the Closure def clos = { print it } clos( "hi" ) /* - Groovy can memorize closure results: - More info at: - http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ - http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize - http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html + Groovy can memorize closure results [1][2][3] */ def cl = {a, b -> sleep(3000) // simulate some time consuming processing @@ -256,9 +272,10 @@ callClosure(3, 4) /* Expando - The Expando class is a dynamic bean so we can add properties and we can add closures as methods to an instance of this class + The Expando class is a dynamic bean so we can add properties and we can add + closures as methods to an instance of this class - Reference: http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html + http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html */ def user = new Expando(name:"Roberto") assert 'Roberto' == user.name @@ -310,7 +327,8 @@ assertEquals "boo", f.boo /* TypeChecked and CompileStatic - Groovy, by nature, is and will always be a dynamic language but it supports typechecked and compilestatic + Groovy, by nature, is and will always be a dynamic language but it supports + typechecked and compilestatic More info: http://www.infoq.com/articles/new-groovy-20 */ @@ -373,7 +391,9 @@ Join a [Groovy user group](http://groovy.codehaus.org/User+Groups) * [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do) - +[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ +[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize +[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html diff --git a/hu-hu/go.html.markdown b/hu-hu/go.html.markdown index b3e8c1ca..460e23ec 100644 --- a/hu-hu/go.html.markdown +++ b/hu-hu/go.html.markdown @@ -1,8 +1,7 @@ --- -name: Go -category: language language: Go -filename: learngo.go +lang: hu-hu +filename: learngo-hu.go contributors: - ["Sonia Keys", "https://github.com/soniakeys"] translators: @@ -20,276 +19,307 @@ a mai számítógépek több magos processzorait, ez nagy rendszerek építésé A Go alap könyvtára mindenre területre kiterjed, ennek köszönhetően a nyelvnek egyre növekvő tábora van. -```Go +```go // Egy soros komment /* Több soros komment */ -// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python csomagkezelésére -// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz létre egy könyvtár helyett. +// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python +// csomagkezelésére +// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz +// létre egy könyvtár helyett. package main -// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a forrásfájlban +// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a +// forrásfájlban import ( - "fmt" // A Go alap könyvtárának része - "net/http" // Beépített webszerver! - "strconv" // Stringek átalakítására szolgáló csomag + "fmt" // A Go alap könyvtárának része + "net/http" // Beépített webszerver! + "strconv" // Stringek átalakítására szolgáló csomag ) // Funkció deklarás, a main nevű funkció a program kezdőpontja. func main() { - // Println kiírja a beadott paramétereket a standard kimenetre. - // Ha más csomagot funkcióját akarjuk használni, akkor azt jelezni kell a csomag nevével - fmt.Println("Hello world!") + // Println kiírja a beadott paramétereket a standard kimenetre. + // Ha más csomagot funkcióját akarjuk használni, akkor azt jelezni kell a + // csomag nevével + fmt.Println("Hello world!") - // Meghívunk egy másik funkciót ebből a csomagból - beyondHello() + // Meghívunk egy másik funkciót ebből a csomagból + beyondHello() } // A függvények paraméterei zárójelek között vannak. // Ha nincsenek paraméterek, akkor is kötelező a zárójel-pár. func beyondHello() { - var x int // Változó deklaráció, használat előtt muszáj ezt megtenni. - x = 3 // Változó értékadás - // "Rövid" deklaráció is létezik, ez az érték alapján deklarálja, definiálja és értéket is ad a változónak - y := 4 - sum, prod := learnMultiple(x, y) // a függvényeknek több visszatérési értéke is lehet - fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás - learnTypes() + var x int // Változó deklaráció, használat előtt muszáj ezt megtenni. + x = 3 // Változó értékadás + // "Rövid" deklaráció is létezik, ez az érték alapján deklarálja, + // definiálja és értéket is ad a változónak + y := 4 + sum, prod := learnMultiple(x, y) // a függvényeknek több + // visszatérési értéke is lehet + fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás + learnTypes() } // A funkcióknak elnevezett visszatérési értékük is lehet func learnMultiple(x, y int) (sum, prod int) { - return x + y, x * y // visszatérünk két értékkel - /* - sum = x + y - prod = x * y - return - Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor. - Üres return esetén, az elnevezett visszatérési változók - aktuális értékeikkel térnek vissza. */ + return x + y, x * y // visszatérünk két értékkel + /* + sum = x + y + prod = x * y + return + Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor. + Üres return esetén, az elnevezett visszatérési változók + aktuális értékeikkel térnek vissza. */ } // Beépített típusok func learnTypes() { - // Rövid deklarás az esetek többségében elég lesz a változókhoz - s := "Tanulj Go-t!" // string típus - - s2 := `A "nyers" stringekben lehetnek - újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön típusa - - // nem ASCII karakterek. Minden Go forrás UTF-8 és a stringek is azok. - g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert tárol - - f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites lebegőpontos szám - c := 3 + 4i // complex128, belsőleg két float64-el tárolva - - // Var szintaxis változó típus definiálással - var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az int-nél - var pi float32 = 22. / 7 - - // Rövid deklarásnál átalakítás is lehetséges - n := byte('\n') // byte típus, ami megegyezik az uint8-al - - // A tömböknek fordítás-időben fixált méretük van - var a4 [4]int // egy tömb 4 int-tel, mind 0-ra inicializálva - a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi értékekre - - // Szeleteknek dinamikus a méretük. A szeleteknek és a tömböknek is meg vannak az előnyeik - // de a szeleteket sokkal gyakrabban használjuk. - s3 := []int{4, 5, 9} // vesd össze a3-al, nincsenek pontok. - s4 := make([]int, 4) // allokál 4 int-et, mind 0-ra inicializálva - var d2 [][]float64 // ez csak deklaráció, semmi sincs még allokálva - bs := []byte("a slice") // típus konverzió szintaxisa - - p, q := learnMemory() // deklarál két mutatót (p,q), két int-re - fmt.Println(*p, *q) // * követi a mutatót. Ez a sor kiírja a két int értékét. - - // A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít - // a hash és dictionary típusokra más nyelvekben. - m := map[string]int{"three": 3, "four": 4} - m["one"] = 1 - - // A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban. - // Az aláhúzással "használod" a változókat, de eldobod az értéküket. - _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs - // Kiíratás is természetesen használatnak minősül - fmt.Println(s, c, a4, s3, d2, m) - - learnFlowControl() + // Rövid deklarás az esetek többségében elég lesz a változókhoz + s := "Tanulj Go-t!" // string típus + + s2 := `A "nyers" stringekben lehetnek + újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön + // típusa + + // nem ASCII karakterek. Minden Go forrás UTF-8 és a stringek is azok. + g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert + // tárol + + f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites + // lebegőpontos szám + c := 3 + 4i // complex128, belsőleg két float64-el tárolva + + // Var szintaxis változó típus definiálással + var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az + // int-nél + var pi float32 = 22. / 7 + + // Rövid deklarásnál átalakítás is lehetséges + n := byte('\n') // byte típus, ami megegyezik az uint8-al + + // A tömböknek fordítás-időben fixált méretük van + var a4 [4]int // egy tömb 4 int-tel, mind 0-ra inicializálva + a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi + // értékekre + + // Szeleteknek dinamikus a méretük. A szeleteknek és a tömböknek is meg + // vannak az előnyeik de a szeleteket sokkal gyakrabban használjuk. + s3 := []int{4, 5, 9} // vesd össze a3-al, nincsenek pontok. + s4 := make([]int, 4) // allokál 4 int-et, mind 0-ra inicializálva + var d2 [][]float64 // ez csak deklaráció, semmi sincs még allokálva + bs := []byte("a slice") // típus konverzió szintaxisa + + p, q := learnMemory() // deklarál két mutatót (p,q), két int-re + fmt.Println(*p, *q) // * követi a mutatót. Ez a sor kiírja a két int értékét. + + // A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít + // a hash és dictionary típusokra más nyelvekben. + m := map[string]int{"three": 3, "four": 4} + m["one"] = 1 + + // A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban. + // Az aláhúzással "használod" a változókat, de eldobod az értéküket. + _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs + // Kiíratás is természetesen használatnak minősül + fmt.Println(s, c, a4, s3, d2, m) + + learnFlowControl() } -// A Go nyelv teljesen szemétgyűjtött (garbage-collected). Megtalálhatók benne mutatók, de nincs mutató aritmetika. -// Ez azt jelenti, hogy üres mutatóval még mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet. +// A Go nyelv teljesen szemétgyűjtött (garbage-collected). Megtalálhatók benne +// mutatók, de nincs mutató aritmetika. Ez azt jelenti, hogy üres mutatóval még +// mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet. func learnMemory() (p, q *int) { - // Elnevezett visszatérési változóknak int-re mutató a típusa - p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát allokál, és visszaad rá egy mutatót. - // Az allokált int nullázva van, p többé nem üres mutató. - s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen. - s[3] = 7 // adjunk értéket az egyiknek - r := -2 // hozzánk létre egy lokális változót - return &s[3], &r // A & megadja a memóriacímét a változónak + // Elnevezett visszatérési változóknak int-re mutató a típusa + p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát + // allokál, és visszaad rá egy mutatót. + // Az allokált int nullázva van, p többé nem üres mutató. + s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen. + s[3] = 7 // adjunk értéket az egyiknek + r := -2 // hozzánk létre egy lokális változót + return &s[3], &r // A & megadja a memóriacímét a változónak } func expensiveComputation() int { - return 1e6 + return 1e6 } func learnFlowControl() { - // Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges. - if true { - fmt.Println("megmondtam") - } - // A kód formátumát a nyelvvel járó "go" parancssori program "go fmt" parancsa szabványosítja - if false { - // így lehet - } else { - // if/else-t csinálni - } - // Használjunk switchet a hosszabb elágazások alkalmazása helyett. - x := 1 - switch x { - case 0: - case 1: - // Az "esetek" nem "esnek át", tehát - case 2: - // ez nem fog lefutni, nincs szükség break-ekre. - } - // A for ciklus sem használ zárójeleket - for x := 0; x < 3; x++ { - fmt.Println("iteráció", x) - } - // itt az x == 1. - - // A for az egyetlen ciklus fajta a Go-ban, de több formája van. - for { // végtelen ciklus - break // csak vicceltem - continue // soha nem fut le - } - // Akárcsak a for-nál, az if-ben is lehet rövid deklarással egy lokális változót létrehozni - // ami az blokk összes if/else-n keresztül érvényes marad. - if y := expensiveComputation(); y > x { - x = y - } - // Függvényeket használhatjuk closure-ként is. - xBig := func() bool { - return x > 100 // a switch felett deklarált x-et használjuk itt - } - fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek) - x /= 1e5 // így most már x == 10 - fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis - - // Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t. - goto love + // Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges. + if true { + fmt.Println("megmondtam") + } + // A kód formátumát a nyelvvel járó "go" parancssori program "go fmt" + // parancsa szabványosítja + if false { + // így lehet + } else { + // if/else-t csinálni + } + // Használjunk switchet a hosszabb elágazások alkalmazása helyett. + x := 1 + switch x { + case 0: + case 1: + // Az "esetek" nem "esnek át", tehát + case 2: + // ez nem fog lefutni, nincs szükség break-ekre. + } + // A for ciklus sem használ zárójeleket + for x := 0; x < 3; x++ { + fmt.Println("iteráció", x) + } + // itt az x == 1. + + // A for az egyetlen ciklus fajta a Go-ban, de több formája van. + for { // végtelen ciklus + break // csak vicceltem + continue // soha nem fut le + } + // Akárcsak a for-nál, az if-ben is lehet rövid deklarással egy lokális + // változót létrehozni ami az blokk összes if/else-n keresztül érvényes + // marad. + if y := expensiveComputation(); y > x { + x = y + } + // Függvényeket használhatjuk closure-ként is. + xBig := func() bool { + return x > 100 // a switch felett deklarált x-et használjuk itt + } + fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek) + x /= 1e5 // így most már x == 10 + fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis + + // Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t. + goto love love: - learnInterfaces() // Itt kezdődnek az érdekes dolgok! + learnInterfaces() // Itt kezdődnek az érdekes dolgok! } -// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a String, ami visszatér egy stringgel. +// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a +// String, ami visszatér egy stringgel. type Stringer interface { - String() string + String() string } -// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van, x és y. +// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van, +// x és y. type pair struct { - x, y int + x, y int } -// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer interf +// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer +// interf func (p pair) String() string { // p lesz a "vevő" - // Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s megfelelőjével. - // A pontokkal érjük el a mindenkori p struktúra elemeit - return fmt.Sprintf("(%d, %d)", p.x, p.y) + // Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s + // megfelelőjével. A pontokkal érjük el a mindenkori p struktúra elemeit + return fmt.Sprintf("(%d, %d)", p.x, p.y) } func learnInterfaces() { - // A kapcsos zárójellel jelezzük, hogy egyből inicializálni - // szeretnénk a struktúra változóit a sorrendnek megfelelően. - p := pair{3, 4} - fmt.Println(p.String()) // meghívjuk a p String metódusát. - var i Stringer // deklaráljuk i-t Stringer típusú interfésznek - i = p // lehetséges, mert a pair struktúra eleget tesz a Stringer interfésznek - // Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb. - fmt.Println(i.String()) - - // Az fmt csomag funckciói automatikusan meghívják a String funkciót - // hogy megtudják egy objektum szöveges reprezentációját. - fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja a String metódust. - fmt.Println(i) // dettó - - learnErrorHandling() + // A kapcsos zárójellel jelezzük, hogy egyből inicializálni + // szeretnénk a struktúra változóit a sorrendnek megfelelően. + p := pair{3, 4} + fmt.Println(p.String()) // meghívjuk a p String metódusát. + var i Stringer // deklaráljuk i-t Stringer típusú interfésznek + i = p // lehetséges, mert a pair struktúra eleget tesz a + // Stringer interfésznek + // Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb. + fmt.Println(i.String()) + + // Az fmt csomag funckciói automatikusan meghívják a String funkciót + // hogy megtudják egy objektum szöveges reprezentációját. + fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja + // a String metódust. + fmt.Println(i) // dettó + + learnErrorHandling() } func learnErrorHandling() { - // ", ok" szokásos megoldás arra, hogy jól működött-e a függvény. - m := map[int]string{3: "three", 4: "four"} - if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban. - fmt.Println("nincs meg") - } else { - fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban. - } - // A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden "ok" volt-e - if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket, úgy se lesz jó jelen esetben - // kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax" - fmt.Println(err) - } - // Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás! - learnConcurrency() + // ", ok" szokásos megoldás arra, hogy jól működött-e a függvény. + m := map[int]string{3: "three", 4: "four"} + if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban. + fmt.Println("nincs meg") + } else { + fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban. + } + // A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden + // "ok" volt-e + if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket, + // úgy se lesz jó jelen + // esetben + // kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax" + fmt.Println(err) + } + // Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás! + learnConcurrency() } // c egy csatorna, egy konkurens-biztos kommunikációs objektum. func inc(i int, c chan int) { - c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így i+1-et küld be a csatornába + c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így + // i+1-et küld be a csatornába } // Az inc-et fogjuk arra használni, hogy konkurensen megnöveljünk számokat func learnConcurrency() { - // Ugyan az a make funkció amivel korábban szeleteket hoztunk létre. - // Make allokál mapokat, szeleteket és csatornákat. - c := make(chan int) - // Indítsunk három konkurens goroutine-t. A számok konkurensen lesznek - // megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig paralellizálva/egymás mellett. - // Mind a 3 ugyanabba a csatornába küldi az eredményeket. - go inc(0, c) // A go utasítás indít el goroutine-okat. - go inc(10, c) - go inc(-805, c) - // Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre. - // Nem lehet tudni milyen sorrendben fognak érkezni az eredmények! - fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a "<-" a beolvasó/kapó operátor - - cs := make(chan string) // még egy csatorna, ez stringekkel kommunikál - cc := make(chan chan string) // egy csatorna csatornával - go func() { c <- 84 }() // indítsunk egy új goroutine-t, csak azért hogy küldjünk egy számot - go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet küldünk - // A select olyan mint a switch, csak feltételek helyett csatorna műveletek vannak. - // Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet kommunikáció. - select { - case i := <-c: // a megkapott értéket el lehet tárolni egy változóban - fmt.Println("ez egy", i) - case <-cs: // vagy el lehet dobni az értékét - fmt.Println("ez egy string volt") - case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni - fmt.Println("sose futok le :'( ") - } - // Ezen a ponton vagy c vagy a cs goroutineja lefutott. - // Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik blokkolva vár. - - learnWebProgramming() // Go képes rá. Te is képes akarsz rá lenni. + // Ugyan az a make funkció amivel korábban szeleteket hoztunk létre. + // Make allokál mapokat, szeleteket és csatornákat. + c := make(chan int) + // Indítsunk három konkurens goroutine-t. A számok konkurensen lesznek + // megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig + // paralellizálva/egymás mellett. Mind a 3 ugyanabba a csatornába küldi az + // eredményeket. + go inc(0, c) // A go utasítás indít el goroutine-okat. + go inc(10, c) + go inc(-805, c) + // Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre. + // Nem lehet tudni milyen sorrendben fognak érkezni az eredmények! + fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a + // "<-" a beolvasó/kapó operátor + + cs := make(chan string) // még egy csatorna, ez stringekkel kommunikál + cc := make(chan chan string) // egy csatorna csatornával + go func() { c <- 84 }() // indítsunk egy új goroutine-t, csak azért + // hogy küldjünk egy számot + go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet + // küldünk + // A select olyan mint a switch, csak feltételek helyett csatorna műveletek + // vannak. Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet + // kommunikáció. + select { + case i := <-c: // a megkapott értéket el lehet tárolni egy változóban + fmt.Println("ez egy", i) + case <-cs: // vagy el lehet dobni az értékét + fmt.Println("ez egy string volt") + case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni + fmt.Println("sose futok le :'( ") + } + // Ezen a ponton vagy c vagy a cs goroutineja lefutott. + // Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik + // blokkolva vár. + + learnWebProgramming() // Go képes rá. Te is képes akarsz rá lenni. } // Egy funkció a http csomagból elindít egy webszervert. func learnWebProgramming() { - // A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd. - // Második paramétere egy interfész, pontosabban a http.Handler interfész. - err := http.ListenAndServe(":8080", pair{}) - fmt.Println(err) // nem felejtük el kiírni az esetleges hibákat! + // A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd. + // Második paramétere egy interfész, pontosabban a http.Handler interfész. + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // nem felejtük el kiírni az esetleges hibákat! } -// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az egyetlen metódusát a ServeHTTP-t. +// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az +// egyetlen metódusát a ServeHTTP-t. func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // Minden kapcsolatra elküldjük ezt a http.ResponseWriter-el - w.Write([]byte("Megtanultad a Go-t Y perc alatt!")) + // Minden kapcsolatra elküldjük ezt a http.ResponseWriter-el + w.Write([]byte("Megtanultad a Go-t Y perc alatt!")) } ``` -- cgit v1.2.3