diff options
-rw-r--r-- | es-es/git-es.html.markdown | 2 | ||||
-rw-r--r-- | objective-c.html.markdown | 186 | ||||
-rw-r--r-- | racket.html.markdown | 40 | ||||
-rw-r--r-- | zh-cn/clojure-cn.html.markdown | 16 | ||||
-rw-r--r-- | zh-cn/go-cn.html.markdown | 16 | ||||
-rw-r--r-- | zh-cn/ruby-cn.html.markdown | 3 |
6 files changed, 192 insertions, 71 deletions
diff --git a/es-es/git-es.html.markdown b/es-es/git-es.html.markdown index 0623b29b..5c9d3378 100644 --- a/es-es/git-es.html.markdown +++ b/es-es/git-es.html.markdown @@ -6,7 +6,7 @@ contributors: translator: - ["Raúl Ascencio", "http://rscnt.github.io"] filename: LearnGit.txt -language: es-es +lang: es-es --- diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 406b2e92..f2787649 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -4,6 +4,7 @@ language: Objective-C contributors: - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] - ["Yannick Loriot", "https://github.com/YannickL"] + - ["Levi Bostian", "https://github.com/levibostian"] filename: LearnObjectiveC.m --- @@ -28,7 +29,9 @@ int main (int argc, const char * argv[]) { // Create an autorelease pool to manage the memory into the program NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - + // If using automatic reference counting (ARC), use @autoreleasepool instead: + @autoreleasepool { + // Use NSLog to print lines to the console NSLog(@"Hello World!"); // Print the string "Hello World!" @@ -74,16 +77,16 @@ int main (int argc, const char * argv[]) short fortyTwoShort = [fortyTwoShortNumber shortValue]; // or 42 NSLog(@"%hi", fortyTwoShort); - NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:41]; - unsigned short fortyTwoUnsigned = [fortyTwoShortNumber unsignedShortValue]; // or 41 - NSLog(@"%hu", fortyTwoUnsigned); + NSNumber *fortyOneShortNumber = [NSNumber numberWithShort:41]; + unsigned short fortyOneUnsigned = [fortyOneShortNumber unsignedShortValue]; // or 41 + NSLog(@"%u", fortyOneUnsigned); NSNumber *fortyTwoLongNumber = @42L; long fortyTwoLong = [fortyTwoLongNumber longValue]; // or 42 NSLog(@"%li", fortyTwoLong); - NSNumber *fortyTwoLongNumber = @53L; - unsigned long fiftyThreeUnsigned = [fortyTwoLongNumber unsignedLongValue]; // or 53 + NSNumber *fiftyThreeLongNumber = @53L; + unsigned long fiftyThreeUnsigned = [fiftyThreeLongNumber unsignedLongValue]; // or 53 NSLog(@"%lu", fiftyThreeUnsigned); // Floating point literals @@ -116,6 +119,7 @@ int main (int argc, const char * argv[]) NSLog(@"%i", yesBool); // prints => 1 // Array object + // May contain different data types, but must be an Objective-C object. NSArray *anArray = @[@1, @2, @3, @4]; NSNumber *thirdNumber = anArray[2]; NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3" @@ -146,10 +150,6 @@ int main (int argc, const char * argv[]) [mutableSet addObject:@"Hello"]; NSLog(@"%@", mutableSet); // prints => {(Hello)} - // Set object - NSSet *set = [NSSet setWithObjects:@"Hello", @"Hello", @"World", nil]; - NSLog(@"%@", set); // prints => {(Hello, World)} - /////////////////////////////////////// // Operators /////////////////////////////////////// @@ -267,6 +267,9 @@ int main (int argc, const char * argv[]) // Clean up the memory you used into your program [pool drain]; + + // End of @autoreleasepool. + } // End the program return 0; @@ -276,79 +279,141 @@ int main (int argc, const char * argv[]) // Classes And Functions /////////////////////////////////////// -// Declare your class in a header(MyClass.h) file: -// Class Declaration Syntax: +// Declare your class in a header file (MyClass.h): +// Class declaration syntax: // @interface ClassName : ParentClassName <ImplementedProtocols> // { -// Member variable declarations; +// type name; <= variable declarations; // } -// -/+ (type) Method declarations; +// @property type name; <= property declarations. +// -/+ (type) Method declarations; <= Method declarations. // @end -@interface MyClass : NSObject <MyProtocol> +@interface MyClass : NSObject <MyProtocol> // NSObject is Objective-C's base object class. { - // Instance variable declarations (can exist in either interface or implementation file) + // Instance variable declarations (can exist in either interface or implementation file). int count; // Protected access by default. - @private id data; // Private access. (More convenient to declare in implementation file) + @private id data; // Private access. (More convenient to declare in implementation file). NSString *name; } -// Convenient notation to auto generate public access getter and setter -@property int count; -@property (copy) NSString *name; // Copy the object during assignment. -@property (readonly) id data; // Declare only a getter method. -// To access public variable in implementation file, use '_' followed by variable name: -_count = 5; -NSLog(@"%d", _count); // prints => 5 -// To access public variable outside implementation file, @property generates setter method -// automatically. Method name is 'set' followed by @property variable name: -MyClass *myClass = [[MyClass alloc] init]; // create MyClass object instance. -[myClass setCount:10]; -NSLog(@"%@", [myClass count]); // prints => 10 +// Convenient notation for public access variables to auto generate a setter method. +// By default, setter method name is 'set' followed by @property variable name. +@property int propInt; // Setter method name = 'setPropInt' +@property (copy) id copyId; // (copy) => Copy the object during assignment. +// (readonly) => Cannot set value outside @interface. +@property (readonly) NSString *roString; // Use @synthesize in @implementation to create accessor. // You can customize the getter and setter names instead of using default 'set' name: -@property (getter=countGet, setter=countSet:) int count; -[myClass countSet:32]; -NSLog(@"%i", [myClass countGet]); // prints => 32 -// For convenience, you may use dot notation to set object instance variables: -myClass.count = 45; -NSLog(@"%i", myClass.count); // prints => 45 - +@property (getter=lengthGet, setter=lengthSet:) int length; + // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; -// + for class method +// + for class methods: + (NSString *)classMethod; ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight; -// - for instance method +// - for instance methods: - (NSString *)instanceMethodWithParameter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; -@end +// Constructor methods with arguments: +- (id)initWithDistance:(int)defaultDistance; +// Objective-C method names are very descriptive. Always name methods according to their arguments. + +@end // States the end of the interface. + + +// To access public variables from the implementation file, @property generates a setter method +// automatically. Method name is 'set' followed by @property variable name: +MyClass *myClass = [[MyClass alloc] init]; // create MyClass object instance. +[myClass setCount:10]; +NSLog(@"%d", [myClass count]); // prints => 10 +// Or using the custom getter and setter method defined in @interface: +[myClass lengthSet:32]; +NSLog(@"%i", [myClass lengthGet]); // prints => 32 +// For convenience, you may use dot notation to set and access object instance variables: +myClass.count = 45; +NSLog(@"%i", myClass.count); // prints => 45 + +// Call class methods: +NSString *classMethodString = [MyClass classMethod]; +MyClass *classFromName = [MyClass myClassFromName:@"Hello"]; + +// Call instance methods: +MyClass *myClass = [[MyClass alloc] init]; // Create MyClass object instance. +NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hello"]; + +// Selectors. +// Way to dynamically represent methods. Used to call methods of a class, pass methods +// through functions to tell other classes they should call it, and to save methods +// as a variable. +// SEL is the data type. @selector() returns a selector from method name provided. +// methodAParameterAsString:andAParameterAsNumber: is method name for method in MyClass +SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:); +if ([myClass respondsToSelector:selectorVar]) { // Checks if class contains method. + // Must put all method arguments into one object to send to performSelector function. + NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil]; + [myClass performSelector:selectorVar withObject:arguments]; // Calls the method. +} else { + // NSStringFromSelector() returns a NSString of the method name of a given selector. + NSLog(@"MyClass does not have method: %@", NSStringFromSelector(selectedVar)); +} // Implement the methods in an implementation (MyClass.m) file: @implementation MyClass { - long count; // Private access instance variable. + long distance; // Private access instance variable. + NSNumber height; +} + +// To access a public variable from the interface file, use '_' followed by variable name: +_count = 5; // References "int count" from MyClass interface. +// Access variables defined in implementation file: +distance = 18; // References "long distance" from MyClass implementation. +// To use @property variable in implementation, use @synthesize to create accessor variable: +@synthesize roString = _roString; // _roString available now in @implementation. + +// Called before calling any class methods or instantiating any objects. ++ (void)initialize +{ + if (self == [MyClass class]) { + distance = 0; + } } -// Call when the object is releasing +// Counterpart to initialize method. Called when an object's reference count is zero. - (void)dealloc { + [height release]; // If not using ARC, make sure to release class variable objects + [super dealloc]; // and call parent class dealloc. } -// Constructors are a way of creating classes -// This is a default constructor which is called when the object is creating +// Constructors are a way of creating instances of a class. +// This is a default constructor which is called when the object is initialized. - (id)init { - if ((self = [super init])) + if ((self = [super init])) // 'super' used to access methods from parent class. { - self.count = 1; + self.count = 1; // 'self' used for object to call itself. } return self; } +// Can create constructors that contain arguments: +- (id)initWithDistance:(int)defaultDistance +{ + distance = defaultDistance; + return self; +} + (NSString *)classMethod { return [[self alloc] init]; } ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight +{ + height = defaultHeight; + return [[self alloc] init]; +} + - (NSString *)instanceMethodWithParameter:(NSString *)string { return @"New string"; @@ -359,13 +424,19 @@ NSLog(@"%i", myClass.count); // prints => 45 return @42; } +// To create a private method, create the method in the @implementation but not in the @interface. +- (NSNumber *)secretPrivateMethod { + return @72; +} +[self secretPrivateMethod]; // Calls private method. + // Methods declared into MyProtocol - (void)myProtocolMethod { // statements } -@end +@end // States the end of the implementation. /* * A protocol declares methods that can be implemented by any class. @@ -395,37 +466,40 @@ With all object interactions, follow the pattern of: (1) create the object, (2) use the object, (3) then free the object from memory. */ -MyClass *classVar = [MyClass alloc]; // alloc sets classVar's reference count to one. Returns pointer to object. +MyClass *classVar = [MyClass alloc]; // 'alloc' sets classVar's reference count to one. Returns pointer to object. [classVar release]; // Decrements classVar's reference count. -// retain claims ownership of existing object instance and increments reference count. Returns pointer to object. +// 'retain' claims ownership of existing object instance and increments reference count. Returns pointer to object. MyClass *newVar = [classVar retain]; // If classVar is released, object is still in memory because newVar is owner. [classVar autorelease]; // Removes ownership of object at end of @autoreleasepool block. Returns pointer to object. -// @property can use retain or assign as well for small convenient definitions. +// @property can use 'retain' and 'assign' as well for small convenient definitions. @property (retain) MyClass *instance; // Release old value and retain a new one (strong reference). @property (assign) NSSet *set; // Pointer to new value without retaining/releasing old (weak reference). +// Automatic Reference Counting (ARC) // Because memory management can be a pain, Xcode 4.2 and iOS 4 introduced Automatic Reference Counting (ARC). // ARC is a compiler feature that inserts retain, release, and autorelease automatically for you, so when using ARC, // you must not use retain, relase, or autorelease. -MyClass *arcMyClass = [[MyClass alloc] init]; // Without ARC, you will need to call: [arcMyClass release] after -// you're done using arcMyClass. But with ARC, there is no need. It will insert this release statement for you. +MyClass *arcMyClass = [[MyClass alloc] init]; +// ... code using arcMyClass +// Without ARC, you will need to call: [arcMyClass release] after you're done using arcMyClass. But with ARC, +// there is no need. It will insert this release statement for you. -// As for the "assign" and "retain" @property attributes, with ARC you use "weak" and "strong". -@property (weak) MyClass *weakVar; // weak does not take ownership of object. If original instance's reference count +// As for the 'assign' and 'retain' @property attributes, with ARC you use 'weak' and 'strong'. +@property (weak) MyClass *weakVar; // 'weak' does not take ownership of object. If original instance's reference count // is set to zero, weakVar will automatically receive value of nil to avoid application crashing. -@property (strong) MyClass *strongVar; // strong takes ownership of object. Ensures object will stay in memory to use. +@property (strong) MyClass *strongVar; // 'strong' takes ownership of object. Ensures object will stay in memory to use. // For regular variables (not @property declared variables), use the following: __strong NSString *strongString; // Default. Variable is retained in memory until it leaves it's scope. __weak NSSet *weakSet; // Weak reference to existing object. When existing object is released, weakSet is set to nil. -__unsafe_unretained NSArray *unsafeArray; // Like __weak but unsafeArray not set to nil when existing object is released. +__unsafe_unretained NSArray *unsafeArray; // Like __weak, but unsafeArray not set to nil when existing object is released. ``` ## Further Reading [Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) -[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/) +[Programming with Objective-C. Apple PDF book](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf) [iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) diff --git a/racket.html.markdown b/racket.html.markdown index adacd91d..eddc00bf 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -6,6 +6,7 @@ contributors: - ["th3rac25", "https://github.com/voila"] - ["Eli Barzilay", "https://github.com/elibarzilay"] - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"] + - ["Duong H. Nguyen", "https://github.com/cmpitg"] --- Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. @@ -600,6 +601,45 @@ vec ; => #(1 2 3 4) ;; expected: positive? ;; given: -5 ;; more details.... + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 11. Input & output +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Racket has this concept of "port", which is very similar to file +;; descriptors in other languages + +;; Open "/tmp/tmp.txt" and write "Hello World" +;; This would trigger an error if the file's already existed +(define out-port (open-output-file "/tmp/tmp.txt")) +(displayln "Hello World" out-port) +(close-output-port out-port) + +;; Append to "/tmp/tmp.txt" +(define out-port (open-output-file "/tmp/tmp.txt" + #:exists 'append)) +(displayln "Hola mundo" out-port) +(close-output-port out-port) + +;; Read from the file again +(define in-port (open-input-file "/tmp/tmp.txt")) +(displayln (read-line in-port)) +; => "Hello World" +(displayln (read-line in-port)) +; => "Hola mundo" +(close-input-port in-port) + +;; Alternatively, with call-with-output-file you don't need to explicitly +;; close the file +(call-with-output-file "/tmp/tmp.txt" + #:exists 'update ; Rewrite the content + (λ (out-port) + (displayln "World Hello!" out-port))) + +;; And call-with-input-file does the same thing for input +(call-with-input-file "/tmp/tmp.txt" + (λ (in-port) + (displayln (read-line in-port)))) ``` ## Further Reading diff --git a/zh-cn/clojure-cn.html.markdown b/zh-cn/clojure-cn.html.markdown index f65dda9d..d5d8232b 100644 --- a/zh-cn/clojure-cn.html.markdown +++ b/zh-cn/clojure-cn.html.markdown @@ -81,7 +81,7 @@ Clojure是运行在JVM上的Lisp家族中的一员。她比Common Lisp更强调 (seq? '(1 2 3)) ; => true (seq? [1 2 3]) ; => false -; 序列被访问时只需要提供一个值,所以序列可以被懒加载 —— 也就意味着可以定义一个无限序列: +; 序列被访问时只需要提供一个值,所以序列可以被懒加载——也就意味着可以定义一个无限序列: (range 4) ; => (0 1 2 3) (range) ; => (0 1 2 3 4 ...) (无限序列) (take 4 (range)) ; (0 1 2 3) @@ -163,11 +163,13 @@ x ; => 1 ; 哈希表 ;;;;;;;;;; -; 基于hash的map和基于数组的map(即arraymap)实现了相同的接口,hashmap查询起来比较快,但不保证元素的顺序。 +; 基于hash的map和基于数组的map(即arraymap)实现了相同的接口,hashmap查询起来比较快, +; 但不保证元素的顺序。 (class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap (class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap -; arraymap在足够大的时候,大多数操作会将其自动转换成hashmap,所以不用担心(对大的arraymap的查询性能)。 +; arraymap在足够大的时候,大多数操作会将其自动转换成hashmap, +; 所以不用担心(对大的arraymap的查询性能)。 ; map支持很多类型的key,但推荐使用keyword类型 ; keyword类型和字符串类似,但做了一些优化。 @@ -275,7 +277,7 @@ keymap ; => {:a 1, :b 2, :c 3} (str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst." ; (#""用来表示一个正则表达式) -; 你可以在一个namespace定义里用`:require`的方式来require(或者use,但最好不要用)模块。 +; 你可以在一个namespace定义里用:require的方式来require(或use,但最好不要用)模块。 ; 这样的话你无需引用模块列表。 (ns test (:require @@ -314,12 +316,14 @@ keymap ; => {:a 1, :b 2, :c 3} ; STM ;;;;;;;;;;;;;;;;; -; 软件内存事务(Software Transactional Memory)被clojure用来处理持久化的状态。clojure里内置了一些结构来使用STM。 +; 软件内存事务(Software Transactional Memory)被clojure用来处理持久化的状态。 +; clojure里内置了一些结构来使用STM。 ; atom是最简单的。给它传一个初始值 (def my-atom (atom {})) ; 用`swap!`更新atom。 -; `swap!`会以atom的当前值为第一个参数来调用一个指定的函数,`swap`其余的参数作为该函数的第二个参数。 +; `swap!`会以atom的当前值为第一个参数来调用一个指定的函数, +; `swap`其余的参数作为该函数的第二个参数。 (swap! my-atom assoc :a 1) ; Sets my-atom to the result of (assoc {} :a 1) (swap! my-atom assoc :b 2) ; Sets my-atom to the result of (assoc {:a 1} :b 2) diff --git a/zh-cn/go-cn.html.markdown b/zh-cn/go-cn.html.markdown index 7cc9c171..4a87dc21 100644 --- a/zh-cn/go-cn.html.markdown +++ b/zh-cn/go-cn.html.markdown @@ -5,6 +5,8 @@ filename: learngo-cn.go contributors: - ["Sonia Keys", "https://github.com/soniakeys"] - ["pantaovay", "https://github.com/pantaovay"] + - ["lidashuang", "https://github.com/lidashuang"] + --- 发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流,但它却提供了解决现实问题的最新最快的方法。 @@ -30,7 +32,7 @@ import ( ) // 函数声明:Main是程序执行的入口。 -// 不管你喜欢还是不喜欢,反正G就用了花括号来包住函数体。 +// 不管你喜欢还是不喜欢,反正Go就用了花括号来包住函数体。 func main() { // 往标准输出打印一行。 // 用包名fmt限制打印函数。 @@ -47,7 +49,7 @@ func beyondHello() { x = 3 // 变量赋值。 // 可以用:=来偷懒,它自动把变量类型、声明和赋值都搞定了。 y := 4 - sum, prod := learnMultiple(x, y) // 多个返回变量的函数 + sum, prod := learnMultiple(x, y) // 返回多个变量的函数 fmt.Println("sum:", sum, "prod:", prod) // 简单输出 learnTypes() // 少于y分钟,学的更多! } @@ -82,7 +84,7 @@ can include line breaks.` // 同样是String类型 var a4 [4] int // 有4个int变量的数组,初始为0 a3 := [...]int{3, 1, 5} // 有3个int变量的数组,同时进行了初始化 - // Slice 有动态大小。Array和Slice各有千秋,但是使用slice的地方更多些。 + // Slice 可以动态的增删。Array和Slice各有千秋,但是使用slice的地方更多些。 s3 := []int{4, 5, 9} // 和a3相比,这里没有省略号 s4 := make([]int, 4) // 分配一个有4个int型变量的slice,全部被初始化为0 @@ -114,7 +116,7 @@ func learnMemory() (p, q *int) { s := make([]int, 20) // 给20个int变量分配一块内存 s[3] = 7 // 赋值 r := -2 // 声明另一个局部变量 - return &s[3], &r // & 取址 + return &s[3], &r // & 取地址 } func expensiveComputation() int { @@ -149,7 +151,7 @@ func learnFlowControl() { // x在这里还是1。为什么? // for 是go里唯一的循环关键字,不过它有很多变种 - for { // 无限循环 + for { // 死循环 break // 骗你的 continue // 不会运行的 } @@ -239,7 +241,7 @@ func learnConcurrency() { go inc(-805, c) // 从channel中独处结果并打印。 // 打印出什么东西是不可预知的。 - fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是接收操作。 + fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是读操作。 cs := make(chan string) // 操作string的channel cc := make(chan chan string) // 操作channel的channel @@ -255,7 +257,7 @@ func learnConcurrency() { case <-cc: // 空的,还没作好通讯的准备 fmt.Println("didn't happen.") } - // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个保持阻塞。 + // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个一直阻塞。 learnWebProgramming() // Go很适合web编程,我知道你也想学! } diff --git a/zh-cn/ruby-cn.html.markdown b/zh-cn/ruby-cn.html.markdown index 619e6e92..3c47f3f9 100644 --- a/zh-cn/ruby-cn.html.markdown +++ b/zh-cn/ruby-cn.html.markdown @@ -6,6 +6,7 @@ contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["lidashuang", "https://github.com/lidashuang"] translators: - ["Lin Xiangyu", "https://github.com/oa414"] --- @@ -173,7 +174,7 @@ new_hash = { defcon: 3, action: true} new_hash.keys #=> [:defcon, :action] # 小贴士:数组和哈希表都是可枚举的 -# 它们可以共享一些有用的方法,比如each, map, count, 和more +# 它们可以共享一些有用的方法,比如each, map, count 等等 # 控制流 |