From 5aa692f5f3bbfa7b79224748dcfd8ca5fba7a8bc Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 12 Feb 2016 11:42:25 -0800 Subject: brainfuck->bf --- zh-cn/bf-cn.html.markdown | 70 ++++++++++++++++++++++++++++++++++++++++ zh-cn/brainfuck-cn.html.markdown | 70 ---------------------------------------- 2 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 zh-cn/bf-cn.html.markdown delete mode 100644 zh-cn/brainfuck-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/bf-cn.html.markdown b/zh-cn/bf-cn.html.markdown new file mode 100644 index 00000000..6cea3012 --- /dev/null +++ b/zh-cn/bf-cn.html.markdown @@ -0,0 +1,70 @@ +--- +language: bf +lang: zh-cn +contributors: + - ["Prajit Ramachandran", "http://prajitr.github.io/"] + - ["Mathias Bynens", "http://mathiasbynens.be/"] +translators: + - ["lyuehh", "https://github.com/lyuehh"] +--- + +Brainfuck 是一个极小的只有8个指令的图灵完全的编程语言。 + +``` +除"><+-.,[]"之外的的任何字符都会被忽略 (不包含双引号)。 + +Brainfuck 包含一个有30,000个单元为0的数组,和 +一个数据指针指向当前的单元。 + +8个指令如下: ++ : 指针指向的单元的值加1 +- : 指针指向的单元的值减1 +> : 将指针移动到下一个单元(右边的元素) +< : 将指针移动到上一个单元(左边的元素) +. : 打印当前单元的内容的ASCII值 (比如 65 = 'A'). +, : 读取一个字符到当前的单元 +[ : 如果当前单元的值是0,则向后调转到对应的]处 +] : 如果当前单元的值不是0,则向前跳转到对应的[处 + +[ 和 ] 组成了一个while循环。很明显,它们必须配对。 + +让我们看一些基本的brainfuck 程序。 + +++++++ [ > ++++++++++ < - ] > +++++ . + +这个程序打印字母'A'。首先,它把 #1 增加到6,使用它来作为循环条件, +然后,进入循环,将指针移动到 #2 ,将 #2 的值增加到10,然后 +移动回 #1,将单元 #1 的值减1,然后继续。循环共进行了6次。 + +这时,我们在 #1,它的值为0,#2 的值为60,我们移动到 +#2,将 #2 的内容加上5,然后将 #2 的内容打印出来,65在 +ASCII中表示'A', 所以'A'就会被打印出来。 + + +, [ > + < - ] > . + +这个程序从用户的输入中读取一个字符,然后把它复制到 #1。 +然后我们开始一个循环,移动到 #2,将 #2 的值加1,再移动回 #1,将 #1 +的值减1,直到 #1的值为0,这样 #2 里就保存了 #1 的旧值,循环结束时我们 +在 #1,这时我们移动到 #2,然后把字符以ASCII打印出来。 + +而且要记住的一点就是,空格在这里只是为了可读性,你可以将他们写成这样: + +,[>+<-]>. + +试着思考一下这段程序是干什么的: + +,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> + +这段程序从输入接收2个参数,然后将他们相乘。 + +先读取2个输入,然后开始外层循环,以 #1 作为终止条件,然后将指针移动到 +#2,然后开始 #2 的内层循环,将 #3 加1。但是这里有一个小问题,在内层 +循环结束的时候,#2 的值是0了,那么下次执行外层循环的时候,就有问题了。 +为了解决这个问题,我们可以增加 #4 的值,然后把 #4 的值复制到 #2, +最后结果就保存在 #3 中了。 +``` +好了这就是brainfuck了。也没那么难,是吧?为了好玩,你可以写你自己的 +brainfuck程序,或者用其他语言写一个brainfuck的解释器,解释器非常容易 +实现,但是如果你是一个自虐狂的话,你可以尝试用brainfuck写一个brainfuk的 +解释器。 diff --git a/zh-cn/brainfuck-cn.html.markdown b/zh-cn/brainfuck-cn.html.markdown deleted file mode 100644 index a6f3fa09..00000000 --- a/zh-cn/brainfuck-cn.html.markdown +++ /dev/null @@ -1,70 +0,0 @@ ---- -language: brainfuck -lang: zh-cn -contributors: - - ["Prajit Ramachandran", "http://prajitr.github.io/"] - - ["Mathias Bynens", "http://mathiasbynens.be/"] -translators: - - ["lyuehh", "https://github.com/lyuehh"] ---- - -Brainfuck 是一个极小的只有8个指令的图灵完全的编程语言。 - -``` -除"><+-.,[]"之外的的任何字符都会被忽略 (不包含双引号)。 - -Brainfuck 包含一个有30,000个单元为0的数组,和 -一个数据指针指向当前的单元。 - -8个指令如下: -+ : 指针指向的单元的值加1 -- : 指针指向的单元的值减1 -> : 将指针移动到下一个单元(右边的元素) -< : 将指针移动到上一个单元(左边的元素) -. : 打印当前单元的内容的ASCII值 (比如 65 = 'A'). -, : 读取一个字符到当前的单元 -[ : 如果当前单元的值是0,则向后调转到对应的]处 -] : 如果当前单元的值不是0,则向前跳转到对应的[处 - -[ 和 ] 组成了一个while循环。很明显,它们必须配对。 - -让我们看一些基本的brainfuck 程序。 - -++++++ [ > ++++++++++ < - ] > +++++ . - -这个程序打印字母'A'。首先,它把 #1 增加到6,使用它来作为循环条件, -然后,进入循环,将指针移动到 #2 ,将 #2 的值增加到10,然后 -移动回 #1,将单元 #1 的值减1,然后继续。循环共进行了6次。 - -这时,我们在 #1,它的值为0,#2 的值为60,我们移动到 -#2,将 #2 的内容加上5,然后将 #2 的内容打印出来,65在 -ASCII中表示'A', 所以'A'就会被打印出来。 - - -, [ > + < - ] > . - -这个程序从用户的输入中读取一个字符,然后把它复制到 #1。 -然后我们开始一个循环,移动到 #2,将 #2 的值加1,再移动回 #1,将 #1 -的值减1,直到 #1的值为0,这样 #2 里就保存了 #1 的旧值,循环结束时我们 -在 #1,这时我们移动到 #2,然后把字符以ASCII打印出来。 - -而且要记住的一点就是,空格在这里只是为了可读性,你可以将他们写成这样: - -,[>+<-]>. - -试着思考一下这段程序是干什么的: - -,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> - -这段程序从输入接收2个参数,然后将他们相乘。 - -先读取2个输入,然后开始外层循环,以 #1 作为终止条件,然后将指针移动到 -#2,然后开始 #2 的内层循环,将 #3 加1。但是这里有一个小问题,在内层 -循环结束的时候,#2 的值是0了,那么下次执行外层循环的时候,就有问题了。 -为了解决这个问题,我们可以增加 #4 的值,然后把 #4 的值复制到 #2, -最后结果就保存在 #3 中了。 -``` -好了这就是brainfuck了。也没那么难,是吧?为了好玩,你可以写你自己的 -brainfuck程序,或者用其他语言写一个brainfuck的解释器,解释器非常容易 -实现,但是如果你是一个自虐狂的话,你可以尝试用brainfuck写一个brainfuk的 -解释器。 -- cgit v1.2.3 From 3d36c241d392221d757c9c237f3b3cdac4691cf6 Mon Sep 17 00:00:00 2001 From: lankeren Date: Wed, 17 Feb 2016 20:32:06 +0800 Subject: Fix typo --- zh-cn/scala-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/scala-cn.html.markdown b/zh-cn/scala-cn.html.markdown index 508dd58e..f3327b5b 100644 --- a/zh-cn/scala-cn.html.markdown +++ b/zh-cn/scala-cn.html.markdown @@ -369,7 +369,7 @@ object Dog { // Case 类是有额外内建功能的类。Scala 初学者常遇到的问题之一便是何时用类 // 和何时用 case 类。界线比较模糊,但通常类倾向于封装,多态和行为。类中的值 -// 的作用域一般为 private , 只有方向是暴露的。case 类的主要目的是放置不可变 +// 的作用域一般为 private , 只有方法是暴露的。case 类的主要目的是放置不可变 // 数据。它们通常只有几个方法,且方法几乎没有副作用。 case class Person(name: String, phoneNumber: String) -- cgit v1.2.3 From 4e4163141750bf2bf13ef3618e2d0bd1274f7398 Mon Sep 17 00:00:00 2001 From: Shawn Zhang Date: Fri, 19 Feb 2016 19:16:39 +0800 Subject: Create the zh-cn version of typescript --- zh-cn/typescript-cn.html.markdown | 173 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 zh-cn/typescript-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/typescript-cn.html.markdown b/zh-cn/typescript-cn.html.markdown new file mode 100644 index 00000000..af870279 --- /dev/null +++ b/zh-cn/typescript-cn.html.markdown @@ -0,0 +1,173 @@ +--- +language: TypeScript +category: language +contributors: + - ["Philippe Vlérick", "https://github.com/pvlerick"] +translators: + - ["Shawn Zhang", "https://github.com/shawnzhang009"] +filename: learntypescript-cn.ts +lang: zh-cn +--- + +TypeScript是一门为开发大型JavaScript应用而设计的语言。TypeScript在JavaScript的基础上增加了类、模块、接口、泛型和静态类型(可选)等常见的概念。它是JavaScript的一个超集:所有JavaScript代码都是有效的TypeScript代码,所以任何JavaScript项目都可以无缝引入TypeScript. TypeScript编译器会把TypeScript代码编译成JavaScript代码。 + +本文只关注TypeScript额外增加的语法,区别于[JavaScript](../javascript-cn/). + +如需测试TypeScript编译器,你可以在[Playground](http://www.typescriptlang.org/Playground)码代码,它会自动编译成JavaScript代码然后直接显示出来。 + +```js +// TypeScript有三种基本类型 +var isDone: boolean = false; +var lines: number = 42; +var name: string = "Anders"; + +// 如果不知道是什么类型,可以使用"Any"(任意)类型 +var notSure: any = 4; +notSure = "maybe a string instead"; +notSure = false; // 亦可,定义为布尔型 + +// 对于集合的声明, 有类型化数组和泛型数组 +var list: number[] = [1, 2, 3]; +// 另外一种,使用泛型数组 +var list: Array = [1, 2, 3]; + +// 枚举: +enum Color {Red, Green, Blue}; +var c: Color = Color.Green; + +// 最后,"void"用于函数没有任何返回的特殊情况下 +function bigHorribleAlert(): void { + alert("I'm a little annoying box!"); +} + +// 函数是"第一等公民"(first class citizens), 支持使用箭头表达式和类型推断 + +// 以下是相等的,TypeScript编译器会把它们编译成相同的JavaScript代码 +var f1 = function(i: number): number { return i * i; } +// 返回推断类型的值 +var f2 = function(i: number) { return i * i; } +var f3 = (i: number): number => { return i * i; } +// 返回推断类型的值 +var f4 = (i: number) => { return i * i; } +// 返回推断类型的值, 单行程式可以不需要return关键字和大括号 +var f5 = (i: number) => i * i; + +// 接口是结构化的,任何具有这些属性的都需要兼容该接口 +interface Person { + name: string; + // 可选属性,使用"?"标识 + age?: number; + // 函数 + move(): void; +} + +// 实现"Person"接口的对象,当它有了"name"和"move"方法之后可被视为一个"Person" +var p: Person = { name: "Bobby", move: () => {} }; +// 带了可选参数的对象 +var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; +// 因为"age"不是"number"类型所以这不是一个"Person" +var invalidPerson: Person = { name: "Bobby", age: true }; + +// 接口同样可以描述一个函数的类型 +interface SearchFunc { + (source: string, subString: string): boolean; +} +// 参数名并不重要,参数类型才是重要的 +var mySearch: SearchFunc; +mySearch = function(src: string, sub: string) { + return src.search(sub) != -1; +} + +// 类 - 成员默认为公共的(public) +class Point { + // 属性 + x: number; + + // 构造器 - 这里面的public/private关键字会为属性和初始化值生成样板化代码 + // 这个例子中, 不需要更多的代码就可以把"y"定义成和"x"一样 + // 同样支持默认值 + + constructor(x: number, public y: number = 0) { + this.x = x; + } + + // 函数 + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + + // 静态成员 + static origin = new Point(0, 0); +} + +var p1 = new Point(10 ,20); +var p2 = new Point(25); //y为0 + +// 继承 +class Point3D extends Point { + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // 必须显式调用父类的构造器 + } + + // 重写 + dist() { + var d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } +} + +// 模块, "."可以作为子模块的分隔符 +module Geometry { + export class Square { + constructor(public sideLength: number = 0) { + } + area() { + return Math.pow(this.sideLength, 2); + } + } +} + +var s1 = new Geometry.Square(5); + +// 引入模块并定义本地别名 +import G = Geometry; + +var s2 = new G.Square(10); + +// 泛型 +// 类 +class Tuple { + constructor(public item1: T1, public item2: T2) { + } +} + +// 接口 +interface Pair { + item1: T; + item2: T; +} + +// 以及函数 +var pairToTuple = function(p: Pair) { + return new Tuple(p.item1, p.item2); +}; + +var tuple = pairToTuple({ item1:"hello", item2:"world"}); + +// 引用定义文件 +// + +// 模板字符串(使用反引号的字符串) +// 嵌入变量的模板字符串 +var name = 'Tyrone'; +var greeting = `Hi ${name}, how are you?` +// 有多行内容的模板字符串 +var multiline = `This is an example +of a multiline string`; + +``` + +## 参考资料 + * [TypeScript官网](http://www.typescriptlang.org/) + * [TypeScript语言规范说明书(pdf)](http://go.microsoft.com/fwlink/?LinkId=267238) + * [Anders Hejlsberg - TypeScript介绍](http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) + * [GitHub源码](https://github.com/Microsoft/TypeScript) + * [Definitely Typed - 类型定义仓库](http://definitelytyped.org/) -- cgit v1.2.3 From 3453e953557a68eb752e8098ff90c255188b5668 Mon Sep 17 00:00:00 2001 From: chenbridge Date: Tue, 23 Feb 2016 12:08:17 +0800 Subject: Update to Swift 2.1 Update to Swift 2.1 --- zh-cn/swift-cn.html.markdown | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/swift-cn.html.markdown b/zh-cn/swift-cn.html.markdown index 3efe4941..017a7812 100644 --- a/zh-cn/swift-cn.html.markdown +++ b/zh-cn/swift-cn.html.markdown @@ -31,7 +31,7 @@ import UIKit // Swift2.0 println() 及 print() 已经整合成 print()。 print("Hello, world") // 这是原本的 println(),会自动进入下一行 -print("Hello, world", appendNewLine: false) // 如果不要自动进入下一行,需设定进入下一行为 false +print("Hello, world", terminator: "") // 如果不要自动进入下一行,需设定结束符为空串 // 变量 (var) 的值设置后可以随意改变 // 常量 (let) 的值设置后不能改变 @@ -171,8 +171,8 @@ while i < 1000 { i *= 2 } -// do-while 循环 -do { +// repeat-while 循环 +repeat { print("hello") } while 1 == 2 @@ -212,11 +212,11 @@ default: // 在 Swift 里,switch 语句的 case 必须处理所有可能的情 func greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)." } -greet("Bob", "Tuesday") +greet("Bob", day: "Tuesday") -// 函数参数前带 `#` 表示外部参数名和内部参数名使用同一个名称。 +// 第一个参数表示外部参数名和内部参数名使用同一个名称。 // 第二个参数表示外部参数名使用 `externalParamName` ,内部参数名使用 `localParamName` -func greet2(#requiredName: String, externalParamName localParamName: String) -> String { +func greet2(requiredName requiredName: String, externalParamName localParamName: String) -> String { return "Hello \(requiredName), the day is \(localParamName)" } greet2(requiredName:"John", externalParamName: "Sunday") // 调用时,使用命名参数来指定参数的值 @@ -235,8 +235,8 @@ print("Gas price: \(price)") // 可变参数 func setup(numbers: Int...) { // 可变参数是个数组 - let number = numbers[0] - let argCount = numbers.count + let _ = numbers[0] + let _ = numbers.count } // 函数变量以及函数作为返回值返回 @@ -257,7 +257,7 @@ func swapTwoInts(inout a: Int, inout b: Int) { } var someIntA = 7 var someIntB = 3 -swapTwoInts(&someIntA, &someIntB) +swapTwoInts(&someIntA, b: &someIntB) print(someIntB) // 7 @@ -286,17 +286,10 @@ numbers = numbers.map({ number in 3 * number }) print(numbers) // [3, 6, 18] // 简洁的闭包 -numbers = sorted(numbers) { $0 > $1 } -// 函数的最后一个参数可以放在括号之外,上面的语句是这个语句的简写形式 -// numbers = sorted(numbers, { $0 > $1 }) +numbers = numbers.sort { $0 > $1 } print(numbers) // [18, 6, 3] -// 超级简洁的闭包,因为 `<` 是个操作符函数 -numbers = sorted(numbers, < ) - -print(numbers) // [3, 6, 18] - // // MARK: 结构体 @@ -305,7 +298,7 @@ print(numbers) // [3, 6, 18] // 结构体和类非常类似,可以有属性和方法 struct NamesTable { - let names = [String]() + let names: [String] // 自定义下标运算符 subscript(index: Int) -> String { @@ -516,7 +509,7 @@ protocol ShapeGenerator { // 一个类实现一个带 optional 方法的协议时,可以实现或不实现这个方法 // optional 方法可以使用 optional 规则来调用 @objc protocol TransformShape { - optional func reshaped() + optional func reshape() optional func canReshape() -> Bool } @@ -528,9 +521,9 @@ class MyShape: Rect { // 在 optional 属性,方法或下标运算符后面加一个问号,可以优雅地忽略 nil 值,返回 nil。 // 这样就不会引起运行时错误 (runtime error) - if let allow = self.delegate?.canReshape?() { + if let reshape = self.delegate?.canReshape?() where reshape { // 注意语句中的问号 - self.delegate?.reshaped?() + self.delegate?.reshape?() } } } @@ -542,8 +535,8 @@ class MyShape: Rect { // 扩展: 给一个已经存在的数据类型添加功能 -// 给 Square 类添加 `Printable` 协议的实现,现在其支持 `Printable` 协议 -extension Square: Printable { +// 给 Square 类添加 `CustomStringConvertible` 协议的实现,现在其支持 `CustomStringConvertible` 协议 +extension Square: CustomStringConvertible { var description: String { return "Area: \(self.getArea()) - ID: \(self.identifier)" } @@ -567,8 +560,8 @@ print(14.multiplyBy(3)) // 42 // 泛型: 和 Java 及 C# 的泛型类似,使用 `where` 关键字来限制类型。 // 如果只有一个类型限制,可以省略 `where` 关键字 -func findIndex(array: [T], valueToFind: T) -> Int? { - for (index, value) in enumerate(array) { +func findIndex(array: [T], _ valueToFind: T) -> Int? { + for (index, value) in array.enumerate() { if value == valueToFind { return index } -- cgit v1.2.3 From 484300f1db4509b08ed48526c61fcfcb48a6152b Mon Sep 17 00:00:00 2001 From: Zach Latta Date: Tue, 23 Feb 2016 16:12:44 -0800 Subject: Fix capitalization of GitHub --- zh-cn/markdown-cn.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/markdown-cn.html.markdown b/zh-cn/markdown-cn.html.markdown index b633714d..87ed46ad 100644 --- a/zh-cn/markdown-cn.html.markdown +++ b/zh-cn/markdown-cn.html.markdown @@ -53,7 +53,7 @@ __此文本也是__ **_或者这样。_** *__这个也是!__* - + ~~此文本为删除线效果。~~ @@ -142,7 +142,7 @@ __此文本也是__ John 甚至不知道 `go_to()` 方程是干嘛的! - + \`\`\`ruby def foobar @@ -150,7 +150,7 @@ def foobar end \`\`\` - + @@ -220,7 +220,7 @@ end 斜体化, 所以我就: \*这段置文字于星号之间\*。 - + | 第一列 | 第二列 | 第三列 | | :---------- | :------: | ----------: | -- cgit v1.2.3 From 072424afd77b9d772102f83508fcc337ffc81a7d Mon Sep 17 00:00:00 2001 From: "Shawn.Zhang" Date: Tue, 15 Mar 2016 02:29:56 +0800 Subject: fixed some sentences --- zh-cn/typescript-cn.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/typescript-cn.html.markdown b/zh-cn/typescript-cn.html.markdown index af870279..2651b1cb 100644 --- a/zh-cn/typescript-cn.html.markdown +++ b/zh-cn/typescript-cn.html.markdown @@ -11,7 +11,7 @@ lang: zh-cn TypeScript是一门为开发大型JavaScript应用而设计的语言。TypeScript在JavaScript的基础上增加了类、模块、接口、泛型和静态类型(可选)等常见的概念。它是JavaScript的一个超集:所有JavaScript代码都是有效的TypeScript代码,所以任何JavaScript项目都可以无缝引入TypeScript. TypeScript编译器会把TypeScript代码编译成JavaScript代码。 -本文只关注TypeScript额外增加的语法,区别于[JavaScript](../javascript-cn/). +本文只关注TypeScript额外增加的区别于[JavaScript](../javascript-cn/)的语法,. 如需测试TypeScript编译器,你可以在[Playground](http://www.typescriptlang.org/Playground)码代码,它会自动编译成JavaScript代码然后直接显示出来。 @@ -21,7 +21,7 @@ var isDone: boolean = false; var lines: number = 42; var name: string = "Anders"; -// 如果不知道是什么类型,可以使用"Any"(任意)类型 +// 如果不知道是什么类型,可以使用"any"(任意)类型 var notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // 亦可,定义为布尔型 @@ -52,7 +52,7 @@ var f4 = (i: number) => { return i * i; } // 返回推断类型的值, 单行程式可以不需要return关键字和大括号 var f5 = (i: number) => i * i; -// 接口是结构化的,任何具有这些属性的都需要兼容该接口 +// 接口是结构化的,任何具有这些属性的对象都与该接口兼容 interface Person { name: string; // 可选属性,使用"?"标识 @@ -83,8 +83,8 @@ class Point { // 属性 x: number; - // 构造器 - 这里面的public/private关键字会为属性和初始化值生成样板化代码 - // 这个例子中, 不需要更多的代码就可以把"y"定义成和"x"一样 + // 构造器 - 这里面的public/private关键字会为属性生成样板代码和初始化值 + // 这个例子中,y会被同x一样定义,不需要额外代码 // 同样支持默认值 constructor(x: number, public y: number = 0) { -- cgit v1.2.3 From 0dc90d1d6961eaecad66a94d63bc296853d3903e Mon Sep 17 00:00:00 2001 From: Louis Victory Alis Date: Sun, 27 Mar 2016 12:53:50 +0800 Subject: Update common-lisp-cn.html.markdown add ANSI Common Lisp --- zh-cn/common-lisp-cn.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zh-cn') diff --git a/zh-cn/common-lisp-cn.html.markdown b/zh-cn/common-lisp-cn.html.markdown index b82829a9..c7fe7e2c 100644 --- a/zh-cn/common-lisp-cn.html.markdown +++ b/zh-cn/common-lisp-cn.html.markdown @@ -14,6 +14,8 @@ ANSI Common Lisp 是一个广泛通用于各个工业领域的、支持多种范 免费的经典的入门书籍[《实用 Common Lisp 编程》](http://www.gigamonkeys.com/book/) +许多人都抱怨上面这本书的翻译。[《ANSI Common Lisp》](http://acl.readthedocs.org/en/latest/)也许对中文读者更友好一些。 + 另外还有一本热门的近期出版的 [Land of Lisp](http://landoflisp.com/). -- cgit v1.2.3 From 5481cbfd3662390a967d6a7695815fe0a0e85cd8 Mon Sep 17 00:00:00 2001 From: William Chang Date: Tue, 29 Mar 2016 22:31:15 +0800 Subject: Update lua-cn.html.markdown the filenameis is "learn-cn.lua", so the command should be "lua learn-cn.lua". --- zh-cn/lua-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/lua-cn.html.markdown b/zh-cn/lua-cn.html.markdown index f7065445..6736dc2a 100644 --- a/zh-cn/lua-cn.html.markdown +++ b/zh-cn/lua-cn.html.markdown @@ -416,7 +416,7 @@ lua-users.org上的[Lua简明参考](http://lua-users.org/files/wiki_insecure/us * os library 顺便说一下,整个文件是可运行的Lua; -保存为 learn-cn.lua 用命令 `lua learn.lua` 启动吧! +保存为 learn-cn.lua 用命令 `lua learn-cn.lua` 启动吧! 本文首次撰写于 [tylerneylon.com](http://tylerneylon.com) 同时也有 [github gist](https://gist.github.com/tylerneylon/5853042) 版. -- cgit v1.2.3 From 8b2eff3f0bf3622faeb2e0346a22470a7131bed2 Mon Sep 17 00:00:00 2001 From: kedaio Date: Tue, 7 Jun 2016 00:16:38 -0400 Subject: start translation for zfs - lang zh-cn first para translated. commit to see if this mess up the format. --- zh-cn/zfs-cn.html.markdown | 400 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 400 insertions(+) create mode 100644 zh-cn/zfs-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/zfs-cn.html.markdown b/zh-cn/zfs-cn.html.markdown new file mode 100644 index 00000000..bb80e369 --- /dev/null +++ b/zh-cn/zfs-cn.html.markdown @@ -0,0 +1,400 @@ +--- +category: tool +tool: zfs +contributors: + - ["sarlalian", "http://github.com/sarlalian"] +translators: + - ["Alan Cheng", "https://github.com/kedaio"] +filename: LearnZfs-cn.txt +lang: zh-cn +--- + +[ZFS](http://open-zfs.org/wiki/Main_Page) +是对存储相关技术重新思考的结果,它把传统的文件系统和卷管理器集成到一个工具当中. +ZFS有一些特有的术语,这把它和传统的存储系统区分开来。但它有很多功能聚焦于可用性。 + + +## ZFS概念 + +### 虚拟设备(Virtual Devices) + +A VDEV is similar to a raid device presented by a RAID card, there are several different +types of VDEV's that offer various advantages, including redundancy and speed. In general +VDEV's offer better reliability and safety than a RAID card. It is discouraged to use a +RAID setup with ZFS, as ZFS expects to directly manage the underlying disks. + +Types of VDEV's +* stripe (a single disk, no redundancy) +* mirror (n-way mirrors supported) +* raidz + * raidz1 (1-disk parity, similar to RAID 5) + * raidz2 (2-disk parity, similar to RAID 6) + * raidz3 (3-disk parity, no RAID analog) +* disk +* file (not recommended for production due to another filesystem adding unnecessary layering) + +Your data is striped across all the VDEV's present in your Storage Pool, so more VDEV's will +increase your IOPS. + +### Storage Pools + +ZFS uses Storage Pools as an abstraction over the lower level storage provider (VDEV), allow +you to separate the user visible file system from the physical layout. + +### ZFS Dataset + +ZFS datasets are analogous to traditional filesystems but with many more features. They +provide many of ZFS's advantages. Datasets support [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write) +snapshots, quota's, compression and de-duplication. + + +### Limits + +One directory may contain up to 2^48 files, up to 16 exabytes each. A single storage pool +can contain up to 256 zettabytes (2^78) of space, and can be striped across 2^64 devices. A +single host can have 2^64 storage pools. The limits are huge. + + +## Commands + +### Storage Pools + +Actions: +* List +* Status +* Destroy +* Get/Set properties + +List zpools + +```bash +# Create a raidz zpool +$ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2 + +# List ZPools +$ zpool list +NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT +zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - + +# List detailed information about a specific zpool +$ zpool list -v zroot +NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT +zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - + gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655 141G 106G 35.2G - 43% 75% +``` + +Status of zpools + +```bash +# Get status information about zpools +$ zpool status + pool: zroot + state: ONLINE + scan: scrub repaired 0 in 2h51m with 0 errors on Thu Oct 1 07:08:31 2015 +config: + + NAME STATE READ WRITE CKSUM + zroot ONLINE 0 0 0 + gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655 ONLINE 0 0 0 + +errors: No known data errors + +# Scrubbing a zpool to correct any errors +$ zpool scrub zroot +$ zpool status -v zroot + pool: zroot + state: ONLINE + scan: scrub in progress since Thu Oct 15 16:59:14 2015 + 39.1M scanned out of 106G at 1.45M/s, 20h47m to go + 0 repaired, 0.04% done +config: + + NAME STATE READ WRITE CKSUM + zroot ONLINE 0 0 0 + gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655 ONLINE 0 0 0 + +errors: No known data errors +``` + +Properties of zpools + +```bash + +# Getting properties from the pool properties can be user set or system provided. +$ zpool get all zroot +NAME PROPERTY VALUE SOURCE +zroot size 141G - +zroot capacity 75% - +zroot altroot - default +zroot health ONLINE - +... + +# Setting a zpool property +$ zpool set comment="Storage of mah stuff" zroot +$ zpool get comment +NAME PROPERTY VALUE SOURCE +tank comment - default +zroot comment Storage of mah stuff local +``` + +Remove zpool + +```bash +$ zpool destroy test +``` + + +### Datasets + +Actions: +* Create +* List +* Rename +* Delete +* Get/Set properties + +Create datasets + +```bash +# Create dataset +$ zfs create tank/root/data +$ mount | grep data +tank/root/data on /data (zfs, local, nfsv4acls) + +# Create child dataset +$ zfs create tank/root/data/stuff +$ mount | grep data +tank/root/data on /data (zfs, local, nfsv4acls) +tank/root/data/stuff on /data/stuff (zfs, local, nfsv4acls) + + +# Create Volume +$ zfs create -V zroot/win_vm +$ zfs list zroot/win_vm +NAME USED AVAIL REFER MOUNTPOINT +tank/win_vm 4.13G 17.9G 64K - +``` + +List datasets + +```bash +# List all datasets +$ zfs list +NAME USED AVAIL REFER MOUNTPOINT +zroot 106G 30.8G 144K none +zroot/ROOT 18.5G 30.8G 144K none +zroot/ROOT/10.1 8K 30.8G 9.63G / +zroot/ROOT/default 18.5G 30.8G 11.2G / +zroot/backup 5.23G 30.8G 144K none +zroot/home 288K 30.8G 144K none +... + +# List a specific dataset +$ zfs list zroot/home +NAME USED AVAIL REFER MOUNTPOINT +zroot/home 288K 30.8G 144K none + +# List snapshots +$ zfs list -t snapshot +zroot@daily-2015-10-15 0 - 144K - +zroot/ROOT@daily-2015-10-15 0 - 144K - +zroot/ROOT/default@daily-2015-10-15 0 - 24.2G - +zroot/tmp@daily-2015-10-15 124K - 708M - +zroot/usr@daily-2015-10-15 0 - 144K - +zroot/home@daily-2015-10-15 0 - 11.9G - +zroot/var@daily-2015-10-15 704K - 1.42G - +zroot/var/log@daily-2015-10-15 192K - 828K - +zroot/var/tmp@daily-2015-10-15 0 - 152K - +``` + +Rename datasets + +```bash +$ zfs rename tank/root/home tank/root/old_home +$ zfs rename tank/root/new_home tank/root/home +``` + +Delete dataset + +```bash +# Datasets cannot be deleted if they have any snapshots +zfs destroy tank/root/home +``` + +Get / set properties of a dataset + +```bash +# Get all properties +$ zfs get all zroot/usr/home │157 # Create Volume +NAME PROPERTY VALUE SOURCE │158 $ zfs create -V zroot/win_vm +zroot/home type filesystem - │159 $ zfs list zroot/win_vm +zroot/home creation Mon Oct 20 14:44 2014 - │160 NAME USED AVAIL REFER MOUNTPOINT +zroot/home used 11.9G - │161 tank/win_vm 4.13G 17.9G 64K - +zroot/home available 94.1G - │162 ``` +zroot/home referenced 11.9G - │163 +zroot/home mounted yes - +... + +# Get property from dataset +$ zfs get compression zroot/usr/home +NAME PROPERTY VALUE SOURCE +zroot/home compression off default + +# Set property on dataset +$ zfs set compression=gzip-9 mypool/lamb + +# Get a set of properties from all datasets +$ zfs list -o name,quota,reservation +NAME QUOTA RESERV +zroot none none +zroot/ROOT none none +zroot/ROOT/default none none +zroot/tmp none none +zroot/usr none none +zroot/home none none +zroot/var none none +... +``` + + +### Snapshots + +ZFS snapshots are one of the things about zfs that are a really big deal + +* The space they take up is equal to the difference in data between the filesystem and its snapshot +* Creation time is only seconds +* Recovery is as fast as you can write data. +* They are easy to automate. + +Actions: +* Create +* Delete +* Rename +* Access snapshots +* Send / Receive +* Clone + + +Create snapshots + +```bash +# Create a snapshot of a single dataset +zfs snapshot tank/home/sarlalian@now + +# Create a snapshot of a dataset and its children +$ zfs snapshot -r tank/home@now +$ zfs list -t snapshot +NAME USED AVAIL REFER MOUNTPOINT +tank/home@now 0 - 26K - +tank/home/sarlalian@now 0 - 259M - +tank/home/alice@now 0 - 156M - +tank/home/bob@now 0 - 156M - +... + +Destroy snapshots + +```bash +# How to destroy a snapshot +$ zfs destroy tank/home/sarlalian@now + +# Delete a snapshot on a parent dataset and its children +$ zfs destroy -r tank/home/sarlalian@now + +``` + +Renaming Snapshots + +```bash +# Rename a snapshot +$ zfs rename tank/home/sarlalian@now tank/home/sarlalian@today +$ zfs rename tank/home/sarlalian@now today + +# zfs rename -r tank/home@now @yesterday +``` + +Accessing snapshots + +```bash +# CD Into a snapshot directory +$ cd /home/.zfs/snapshot/ +``` + +Sending and Receiving + +```bash +# Backup a snapshot to a file +$ zfs send tank/home/sarlalian@now | gzip > backup_file.gz + +# Send a snapshot to another dataset +$ zfs send tank/home/sarlalian@now | zfs recv backups/home/sarlalian + +# Send a snapshot to a remote host +$ zfs send tank/home/sarlalian@now | ssh root@backup_server 'zfs recv tank/home/sarlalian' + +# Send full dataset with snapshos to new host +$ zfs send -v -R tank/home@now | ssh root@backup_server 'zfs recv tank/home' +``` + +Cloneing Snapshots + +```bash +# Clone a snapshot +$ zfs clone tank/home/sarlalian@now tank/home/sarlalian_new + +# Promoting the clone so it is no longer dependent on the snapshot +$ zfs promote tank/home/sarlalian_new +``` + +### Putting it all together + +This following a script utilizing FreeBSD, jails and ZFS to automate +provisioning a clean copy of a mysql staging database from a live replication +slave. + +```bash +#!/bin/sh + +echo "==== Stopping the staging database server ====" +jail -r staging + +echo "==== Cleaning up existing staging server and snapshot ====" +zfs destroy -r zroot/jails/staging +zfs destroy zroot/jails/slave@staging + +echo "==== Quiescing the slave database ====" +echo "FLUSH TABLES WITH READ LOCK;" | /usr/local/bin/mysql -u root -pmyrootpassword -h slave + +echo "==== Snapshotting the slave db filesystem as zroot/jails/slave@staging ====" +zfs snapshot zroot/jails/slave@staging + +echo "==== Starting the slave database server ====" +jail -c slave + +echo "==== Cloning the slave snapshot to the staging server ====" +zfs clone zroot/jails/slave@staging zroot/jails/staging + +echo "==== Installing the staging mysql config ====" +mv /jails/staging/usr/local/etc/my.cnf /jails/staging/usr/local/etc/my.cnf.slave +cp /jails/staging/usr/local/etc/my.cnf.staging /jails/staging/usr/local/etc/my.cnf + +echo "==== Setting up the staging rc.conf file ====" +mv /jails/staging/etc/rc.conf.local /jails/staging/etc/rc.conf.slave +mv /jails/staging/etc/rc.conf.staging /jails/staging/etc/rc.conf.local + +echo "==== Starting the staging db server ====" +jail -c staging + +echo "==== Makes the staging database not pull from the master ====" +echo "STOP SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging +echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging +``` + + +### Additional Reading + +* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs) +* [FreeBSD Handbook on ZFS](https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/zfs.html) +* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs) +* [Oracle's Tuning Guide](http://www.oracle.com/technetwork/articles/servers-storage-admin/sto-recommended-zfs-settings-1951715.html) +* [OpenZFS Tuning Guide](http://open-zfs.org/wiki/Performance_tuning) +* [FreeBSD ZFS Tuning Guide](https://wiki.freebsd.org/ZFSTuningGuide) -- cgit v1.2.3 From c4077317b378547902e88d5a9dab4ee1f5417a99 Mon Sep 17 00:00:00 2001 From: kedaio Date: Tue, 7 Jun 2016 03:38:55 -0400 Subject: Update zfs-cn.html.markdown completed translation --- zh-cn/zfs-cn.html.markdown | 194 ++++++++++++++++++++++----------------------- 1 file changed, 96 insertions(+), 98 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/zfs-cn.html.markdown b/zh-cn/zfs-cn.html.markdown index bb80e369..2e9321a4 100644 --- a/zh-cn/zfs-cn.html.markdown +++ b/zh-cn/zfs-cn.html.markdown @@ -16,59 +16,57 @@ ZFS有一些特有的术语,这把它和传统的存储系统区分开来。 ## ZFS概念 -### 虚拟设备(Virtual Devices) +### 虚拟设备(Virtual Devices,VDEV) -A VDEV is similar to a raid device presented by a RAID card, there are several different -types of VDEV's that offer various advantages, including redundancy and speed. In general -VDEV's offer better reliability and safety than a RAID card. It is discouraged to use a -RAID setup with ZFS, as ZFS expects to directly manage the underlying disks. +对于操作系统来说,VDEA和传统的RAID阵列卡所呈现的rai设备类似。VDEV有几种不同的类型,每种类型 +都有自己的优势,包括冗余和速度。一般来说,VDEV的可靠性和安全性比阵列卡要好。因此使用ZFS时不 +建议使用阵列卡。让ZFS直接管理磁盘。 -Types of VDEV's -* stripe (a single disk, no redundancy) -* mirror (n-way mirrors supported) +VDEV的类型 +* stripe (条带。单个磁盘,没有冗余) +* mirror (镜像。支持n-wa镜像) * raidz - * raidz1 (1-disk parity, similar to RAID 5) - * raidz2 (2-disk parity, similar to RAID 6) - * raidz3 (3-disk parity, no RAID analog) -* disk -* file (not recommended for production due to another filesystem adding unnecessary layering) + * raidz1 (一个奇偶校验磁盘, 类似于RAID 5) + * raidz2 (两个奇偶校验磁盘, 类似于RAID 6) + * raidz3 (三个奇偶校验磁盘, 没有类似RAID等级) +* disk (磁盘) +* file (文件。不推荐在生产环境中使用,因为中间又多了一层不必要的文件系统) -Your data is striped across all the VDEV's present in your Storage Pool, so more VDEV's will -increase your IOPS. +数据会以条带方式存储于存储池中的所有VDE上。因此一个存储池中的VDEV越多,IOPS就越高。 -### Storage Pools +### storage pool (存储池) -ZFS uses Storage Pools as an abstraction over the lower level storage provider (VDEV), allow -you to separate the user visible file system from the physical layout. +ZFS 使用存储池来作为底层存储提供者(VDEV)的抽象。这样可以把用户可见的文件系统和底层的物理磁盘 +布局分离开来。 -### ZFS Dataset +### ZFS 数据集(Dataset) -ZFS datasets are analogous to traditional filesystems but with many more features. They -provide many of ZFS's advantages. Datasets support [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write) -snapshots, quota's, compression and de-duplication. +ZFS 数据集类似于传统的文件系统(译者注:或者说是目录),但是提供了更多的功能。ZFS的很多优势也是 +在这一层体现出来的。数据集支持 [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write) +快照, 配额, 压缩和重复消除(de-duplication). -### Limits +### 限制 -One directory may contain up to 2^48 files, up to 16 exabytes each. A single storage pool -can contain up to 256 zettabytes (2^78) of space, and can be striped across 2^64 devices. A -single host can have 2^64 storage pools. The limits are huge. +一个目录最多可包含 2^48个文件, 每个文件最大可以是16 exabytes. 一个存储池最大可包含256 zettabytes 、 +(2^78) 的空间, 可以条带化地分布于2^64 设备上. 单一主机最多可以创建2^64个存储池。这些限制可以说是相 +当大。 -## Commands +## 命令 -### Storage Pools +### 存储池 -Actions: -* List -* Status -* Destroy -* Get/Set properties +Actions: (存储池操作) +* List (列举) +* Status (查看状态) +* Destroy (删除) +* Get/Set properties (获取/设置属性) -List zpools +List zpools (列举存储池(也叫zpool)) ```bash -# Create a raidz zpool +# Create a raidz zpool (创建一个raidz类型的存储池) $ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2 # List ZPools @@ -76,17 +74,17 @@ $ zpool list NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - -# List detailed information about a specific zpool +# List detailed information about a specific zpool (列出某一存储池的详细信息) $ zpool list -v zroot NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655 141G 106G 35.2G - 43% 75% ``` -Status of zpools +Status of zpools (存储池状态) ```bash -# Get status information about zpools +# Get status information about zpools (获取全部zpool信息) $ zpool status pool: zroot state: ONLINE @@ -99,7 +97,7 @@ config: errors: No known data errors -# Scrubbing a zpool to correct any errors +# Scrubbing a zpool to correct any errors (用scrub来更正存储池错误信息) $ zpool scrub zroot $ zpool status -v zroot pool: zroot @@ -116,11 +114,12 @@ config: errors: No known data errors ``` -Properties of zpools +Properties of zpools (存储池属性) ```bash # Getting properties from the pool properties can be user set or system provided. +# (获取zroot存储池的全部属性) $ zpool get all zroot NAME PROPERTY VALUE SOURCE zroot size 141G - @@ -129,7 +128,7 @@ zroot altroot - default zroot health ONLINE - ... -# Setting a zpool property +# Setting a zpool property (设置存储池属性,下例蛇这的是commen备注属性) $ zpool set comment="Storage of mah stuff" zroot $ zpool get comment NAME PROPERTY VALUE SOURCE @@ -137,48 +136,48 @@ tank comment - default zroot comment Storage of mah stuff local ``` -Remove zpool +Remove zpool (删除存储池) ```bash $ zpool destroy test ``` -### Datasets +### Datasets (数据集) -Actions: -* Create -* List -* Rename -* Delete -* Get/Set properties +Actions: (数据集相关操作) +* Create (创建) +* List (列举) +* Rename (重命名) +* Delete (删除) +* Get/Set properties (获取/设置属性) Create datasets ```bash -# Create dataset +# Create dataset (创建数据集) $ zfs create tank/root/data $ mount | grep data tank/root/data on /data (zfs, local, nfsv4acls) -# Create child dataset +# Create child dataset (创建子数据集) $ zfs create tank/root/data/stuff $ mount | grep data tank/root/data on /data (zfs, local, nfsv4acls) tank/root/data/stuff on /data/stuff (zfs, local, nfsv4acls) -# Create Volume +# Create Volume (创建卷) $ zfs create -V zroot/win_vm $ zfs list zroot/win_vm NAME USED AVAIL REFER MOUNTPOINT tank/win_vm 4.13G 17.9G 64K - ``` -List datasets +List datasets (列举数据集) ```bash -# List all datasets +# List all datasets (列举所有数据集) $ zfs list NAME USED AVAIL REFER MOUNTPOINT zroot 106G 30.8G 144K none @@ -189,12 +188,12 @@ zroot/backup 5.23G zroot/home 288K 30.8G 144K none ... -# List a specific dataset +# List a specific dataset (列举某一数据集) $ zfs list zroot/home NAME USED AVAIL REFER MOUNTPOINT zroot/home 288K 30.8G 144K none -# List snapshots +# List snapshots (列举快照) $ zfs list -t snapshot zroot@daily-2015-10-15 0 - 144K - zroot/ROOT@daily-2015-10-15 0 - 144K - @@ -207,24 +206,24 @@ zroot/var/log@daily-2015-10-15 zroot/var/tmp@daily-2015-10-15 0 - 152K - ``` -Rename datasets +Rename datasets (重命名数据集) ```bash $ zfs rename tank/root/home tank/root/old_home $ zfs rename tank/root/new_home tank/root/home ``` -Delete dataset +Delete dataset (删除数据集) ```bash -# Datasets cannot be deleted if they have any snapshots +# Datasets cannot be deleted if they have any snapshots (数据集如果有快照则无法删除) zfs destroy tank/root/home ``` -Get / set properties of a dataset +Get / set properties of a dataset (获取/设置数据集属性) ```bash -# Get all properties +# Get all properties (获取全部属性) $ zfs get all zroot/usr/home │157 # Create Volume NAME PROPERTY VALUE SOURCE │158 $ zfs create -V zroot/win_vm zroot/home type filesystem - │159 $ zfs list zroot/win_vm @@ -235,15 +234,15 @@ zroot/home referenced 11.9G - zroot/home mounted yes - ... -# Get property from dataset +# Get property from dataset (获取数据集属性) $ zfs get compression zroot/usr/home NAME PROPERTY VALUE SOURCE zroot/home compression off default -# Set property on dataset +# Set property on dataset (设置压缩属性compressio) $ zfs set compression=gzip-9 mypool/lamb -# Get a set of properties from all datasets +# Get a set of properties from all datasets (列举所有数据集的名称,配额和预留属性) $ zfs list -o name,quota,reservation NAME QUOTA RESERV zroot none none @@ -257,31 +256,31 @@ zroot/var none none ``` -### Snapshots +### Snapshots (快照) -ZFS snapshots are one of the things about zfs that are a really big deal +快照是ZFS 的一个非常重要的功能 -* The space they take up is equal to the difference in data between the filesystem and its snapshot -* Creation time is only seconds -* Recovery is as fast as you can write data. -* They are easy to automate. +* 快照占用的空间等于它和原始数据的差异量 +* 创建时间以秒计 +* 恢复时间和写入速度相同 +* 易于自动化 -Actions: -* Create -* Delete -* Rename -* Access snapshots -* Send / Receive -* Clone +Actions: (快照相关操作) +* Create (创建) +* Delete (删除) +* Rename (重命名) +* Access snapshots (访问) +* Send / Receive (发送/接收) +* Clone (克隆) -Create snapshots +Create snapshots (创建快照) ```bash -# Create a snapshot of a single dataset +# Create a snapshot of a single dataset (为单一数据集创建快照) zfs snapshot tank/home/sarlalian@now -# Create a snapshot of a dataset and its children +# Create a snapshot of a dataset and its children (为数据集及其子集创建快照) $ zfs snapshot -r tank/home@now $ zfs list -t snapshot NAME USED AVAIL REFER MOUNTPOINT @@ -291,65 +290,64 @@ tank/home/alice@now 0 - 156M - tank/home/bob@now 0 - 156M - ... -Destroy snapshots +Destroy snapshots (删除快照) ```bash -# How to destroy a snapshot +# How to destroy a snapshot (如何删除) $ zfs destroy tank/home/sarlalian@now -# Delete a snapshot on a parent dataset and its children +# Delete a snapshot on a parent dataset and its children (删除某一数据集及其子集的快照) $ zfs destroy -r tank/home/sarlalian@now ``` -Renaming Snapshots +Renaming Snapshots (重命名) ```bash -# Rename a snapshot +# Rename a snapshot (重命名快照,示例) $ zfs rename tank/home/sarlalian@now tank/home/sarlalian@today $ zfs rename tank/home/sarlalian@now today # zfs rename -r tank/home@now @yesterday ``` -Accessing snapshots +Accessing snapshots (访问快照) ```bash -# CD Into a snapshot directory +# CD Into a snapshot directory (cd进入一个快照目录) $ cd /home/.zfs/snapshot/ ``` Sending and Receiving ```bash -# Backup a snapshot to a file +# Backup a snapshot to a file (备份快照到一个文件) $ zfs send tank/home/sarlalian@now | gzip > backup_file.gz -# Send a snapshot to another dataset +# Send a snapshot to another dataset (发送快照到另一个数据集) $ zfs send tank/home/sarlalian@now | zfs recv backups/home/sarlalian -# Send a snapshot to a remote host +# Send a snapshot to a remote host (发送快照到一个远程主机) $ zfs send tank/home/sarlalian@now | ssh root@backup_server 'zfs recv tank/home/sarlalian' -# Send full dataset with snapshos to new host +# Send full dataset with snapshos to new host (发送数据集及其快照到一个新主机) $ zfs send -v -R tank/home@now | ssh root@backup_server 'zfs recv tank/home' ``` -Cloneing Snapshots +Cloneing Snapshots (克隆快照) ```bash # Clone a snapshot $ zfs clone tank/home/sarlalian@now tank/home/sarlalian_new -# Promoting the clone so it is no longer dependent on the snapshot +# Promoting the clone so it is no longer dependent on the snapshot(提升克隆,让它不再依赖原始数据) $ zfs promote tank/home/sarlalian_new ``` -### Putting it all together +### Putting it all together (汇总) -This following a script utilizing FreeBSD, jails and ZFS to automate -provisioning a clean copy of a mysql staging database from a live replication -slave. +下面这个脚本使用了FreeBSD, jails和ZFS,来自动在一个mysql群集的热备主机上为一个mysq staging数据库 +创建一份纯净的拷贝。 ```bash #!/bin/sh @@ -390,7 +388,7 @@ echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging ``` -### Additional Reading +### Additional Reading (延伸阅读) * [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs) * [FreeBSD Handbook on ZFS](https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/zfs.html) -- cgit v1.2.3 From 18a78a7aae5c8a6172777c92f5470a00258cbe36 Mon Sep 17 00:00:00 2001 From: kedaio Date: Tue, 7 Jun 2016 03:44:37 -0400 Subject: Update zfs-cn.html.markdown added remark on what is zfs clone --- zh-cn/zfs-cn.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/zfs-cn.html.markdown b/zh-cn/zfs-cn.html.markdown index 2e9321a4..3680678e 100644 --- a/zh-cn/zfs-cn.html.markdown +++ b/zh-cn/zfs-cn.html.markdown @@ -128,7 +128,7 @@ zroot altroot - default zroot health ONLINE - ... -# Setting a zpool property (设置存储池属性,下例蛇这的是commen备注属性) +# Setting a zpool property (设置存储池属性,下例这是设置commen备注属性) $ zpool set comment="Storage of mah stuff" zroot $ zpool get comment NAME PROPERTY VALUE SOURCE @@ -239,7 +239,7 @@ $ zfs get compression zroot/usr/home NAME PROPERTY VALUE SOURCE zroot/home compression off default -# Set property on dataset (设置压缩属性compressio) +# Set property on dataset (设置压缩属性compression) $ zfs set compression=gzip-9 mypool/lamb # Get a set of properties from all datasets (列举所有数据集的名称,配额和预留属性) @@ -271,7 +271,7 @@ Actions: (快照相关操作) * Rename (重命名) * Access snapshots (访问) * Send / Receive (发送/接收) -* Clone (克隆) +* Clone (克隆。译者注:关于clone和快照的区别可参看[这里](http://docs.oracle.com/cd/E19253-01/819-5461/gbcxz/index.html)) Create snapshots (创建快照) -- cgit v1.2.3 From 0a93da20009c51e28b2d12a142f06aaa680a231f Mon Sep 17 00:00:00 2001 From: kedaio Date: Tue, 7 Jun 2016 09:57:17 -0400 Subject: update according to reviewer feedback updated translation according to reviewer's feedback --- zh-cn/zfs-cn.html.markdown | 75 +++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 38 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/zfs-cn.html.markdown b/zh-cn/zfs-cn.html.markdown index 3680678e..fdf5277e 100644 --- a/zh-cn/zfs-cn.html.markdown +++ b/zh-cn/zfs-cn.html.markdown @@ -10,21 +10,21 @@ lang: zh-cn --- [ZFS](http://open-zfs.org/wiki/Main_Page) -是对存储相关技术重新思考的结果,它把传统的文件系统和卷管理器集成到一个工具当中. -ZFS有一些特有的术语,这把它和传统的存储系统区分开来。但它有很多功能聚焦于可用性。 +是重新思考与储存相关技术的结果,它把传统的文件系统和卷管理器集成到一个工具当中. +ZFS不但有把它和传统存储系统分开来的特有术语,也有很多聚焦于可用性的功能。 ## ZFS概念 ### 虚拟设备(Virtual Devices,VDEV) -对于操作系统来说,VDEA和传统的RAID阵列卡所呈现的rai设备类似。VDEV有几种不同的类型,每种类型 +对于操作系统来说,VDEV和传统的RAID阵列卡所呈现的raid设备类似。VDEV有几种不同的类型,每种类型 都有自己的优势,包括冗余和速度。一般来说,VDEV的可靠性和安全性比阵列卡要好。因此使用ZFS时不 建议使用阵列卡。让ZFS直接管理磁盘。 VDEV的类型 * stripe (条带。单个磁盘,没有冗余) -* mirror (镜像。支持n-wa镜像) +* mirror (镜像。支持n-way镜像) * raidz * raidz1 (一个奇偶校验磁盘, 类似于RAID 5) * raidz2 (两个奇偶校验磁盘, 类似于RAID 6) @@ -32,7 +32,7 @@ VDEV的类型 * disk (磁盘) * file (文件。不推荐在生产环境中使用,因为中间又多了一层不必要的文件系统) -数据会以条带方式存储于存储池中的所有VDE上。因此一个存储池中的VDEV越多,IOPS就越高。 +数据会以条带方式存储于存储池中的所有VDEV上。因此一个存储池中的VDEV越多,IOPS就越高。 ### storage pool (存储池) @@ -66,15 +66,15 @@ Actions: (存储池操作) List zpools (列举存储池(也叫zpool)) ```bash -# Create a raidz zpool (创建一个raidz类型的存储池) +# 创建一个raidz类型的存储池(名称为bucket) $ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2 -# List ZPools +# 列出所有存储池 $ zpool list NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - -# List detailed information about a specific zpool (列出某一存储池的详细信息) +# 列出某一存储池的详细信息 $ zpool list -v zroot NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 141G 106G 35.2G - 43% 75% 1.00x ONLINE - @@ -84,7 +84,7 @@ zroot 141G 106G 35.2G - 43% Status of zpools (存储池状态) ```bash -# Get status information about zpools (获取全部zpool信息) +# 获取全部zpool状态信息 $ zpool status pool: zroot state: ONLINE @@ -97,7 +97,7 @@ config: errors: No known data errors -# Scrubbing a zpool to correct any errors (用scrub来更正存储池错误信息) +# 用scrub来更正存储池错误信息 $ zpool scrub zroot $ zpool status -v zroot pool: zroot @@ -118,8 +118,7 @@ Properties of zpools (存储池属性) ```bash -# Getting properties from the pool properties can be user set or system provided. -# (获取zroot存储池的全部属性) +# 获取某一存储池的全部属性。属性可能是系统提供,也可能是用户设置 $ zpool get all zroot NAME PROPERTY VALUE SOURCE zroot size 141G - @@ -128,7 +127,7 @@ zroot altroot - default zroot health ONLINE - ... -# Setting a zpool property (设置存储池属性,下例这是设置commen备注属性) +# 设置存储池属性,下例这是设置comment(备注)属性 $ zpool set comment="Storage of mah stuff" zroot $ zpool get comment NAME PROPERTY VALUE SOURCE @@ -155,19 +154,19 @@ Actions: (数据集相关操作) Create datasets ```bash -# Create dataset (创建数据集) +# 创建数据集 $ zfs create tank/root/data $ mount | grep data tank/root/data on /data (zfs, local, nfsv4acls) -# Create child dataset (创建子数据集) +# 创建子数据集 $ zfs create tank/root/data/stuff $ mount | grep data tank/root/data on /data (zfs, local, nfsv4acls) tank/root/data/stuff on /data/stuff (zfs, local, nfsv4acls) -# Create Volume (创建卷) +# 创建卷 $ zfs create -V zroot/win_vm $ zfs list zroot/win_vm NAME USED AVAIL REFER MOUNTPOINT @@ -177,7 +176,7 @@ tank/win_vm 4.13G 17.9G 64K - List datasets (列举数据集) ```bash -# List all datasets (列举所有数据集) +# 列出所有数据集 $ zfs list NAME USED AVAIL REFER MOUNTPOINT zroot 106G 30.8G 144K none @@ -188,12 +187,12 @@ zroot/backup 5.23G zroot/home 288K 30.8G 144K none ... -# List a specific dataset (列举某一数据集) +# 列举某一数据集的信息 $ zfs list zroot/home NAME USED AVAIL REFER MOUNTPOINT zroot/home 288K 30.8G 144K none -# List snapshots (列举快照) +# 列出快照 $ zfs list -t snapshot zroot@daily-2015-10-15 0 - 144K - zroot/ROOT@daily-2015-10-15 0 - 144K - @@ -216,14 +215,14 @@ $ zfs rename tank/root/new_home tank/root/home Delete dataset (删除数据集) ```bash -# Datasets cannot be deleted if they have any snapshots (数据集如果有快照则无法删除) +# 数据集如果有快照则无法删除 zfs destroy tank/root/home ``` Get / set properties of a dataset (获取/设置数据集属性) ```bash -# Get all properties (获取全部属性) +# 获取数据集全部属性 $ zfs get all zroot/usr/home │157 # Create Volume NAME PROPERTY VALUE SOURCE │158 $ zfs create -V zroot/win_vm zroot/home type filesystem - │159 $ zfs list zroot/win_vm @@ -234,15 +233,15 @@ zroot/home referenced 11.9G - zroot/home mounted yes - ... -# Get property from dataset (获取数据集属性) +# 获取数据集属性 $ zfs get compression zroot/usr/home NAME PROPERTY VALUE SOURCE zroot/home compression off default -# Set property on dataset (设置压缩属性compression) +# 设置数据集属性(下例为设置压缩属性compression) $ zfs set compression=gzip-9 mypool/lamb -# Get a set of properties from all datasets (列举所有数据集的名称,配额和预留属性) +# 列举所有数据集的名称、配额和预留属性 $ zfs list -o name,quota,reservation NAME QUOTA RESERV zroot none none @@ -277,10 +276,10 @@ Actions: (快照相关操作) Create snapshots (创建快照) ```bash -# Create a snapshot of a single dataset (为单一数据集创建快照) +# 为单一数据集创建快照 zfs snapshot tank/home/sarlalian@now -# Create a snapshot of a dataset and its children (为数据集及其子集创建快照) +# 为数据集及其子集创建快照 $ zfs snapshot -r tank/home@now $ zfs list -t snapshot NAME USED AVAIL REFER MOUNTPOINT @@ -293,10 +292,10 @@ tank/home/bob@now 0 - 156M - Destroy snapshots (删除快照) ```bash -# How to destroy a snapshot (如何删除) +# 如何删除快照 $ zfs destroy tank/home/sarlalian@now -# Delete a snapshot on a parent dataset and its children (删除某一数据集及其子集的快照) +# 删除某一数据集及其子集的快照 $ zfs destroy -r tank/home/sarlalian@now ``` @@ -304,7 +303,7 @@ $ zfs destroy -r tank/home/sarlalian@now Renaming Snapshots (重命名) ```bash -# Rename a snapshot (重命名快照,示例) +# 重命名快照 $ zfs rename tank/home/sarlalian@now tank/home/sarlalian@today $ zfs rename tank/home/sarlalian@now today @@ -314,37 +313,37 @@ $ zfs rename tank/home/sarlalian@now today Accessing snapshots (访问快照) ```bash -# CD Into a snapshot directory (cd进入一个快照目录) +# cd进入一个快照目录 $ cd /home/.zfs/snapshot/ ``` Sending and Receiving ```bash -# Backup a snapshot to a file (备份快照到一个文件) +# 备份快照到一个文件 $ zfs send tank/home/sarlalian@now | gzip > backup_file.gz -# Send a snapshot to another dataset (发送快照到另一个数据集) +# 发送快照到另一个数据集 $ zfs send tank/home/sarlalian@now | zfs recv backups/home/sarlalian -# Send a snapshot to a remote host (发送快照到一个远程主机) +# 发送快照到一个远程主机 $ zfs send tank/home/sarlalian@now | ssh root@backup_server 'zfs recv tank/home/sarlalian' -# Send full dataset with snapshos to new host (发送数据集及其快照到一个新主机) +# 发送完整数据集及其快照到一个新主机 $ zfs send -v -R tank/home@now | ssh root@backup_server 'zfs recv tank/home' ``` Cloneing Snapshots (克隆快照) ```bash -# Clone a snapshot +# 克隆一个快照 $ zfs clone tank/home/sarlalian@now tank/home/sarlalian_new -# Promoting the clone so it is no longer dependent on the snapshot(提升克隆,让它不再依赖原始数据) +# 提升克隆,让它不再依赖原始快照 $ zfs promote tank/home/sarlalian_new ``` -### Putting it all together (汇总) +### 汇总 下面这个脚本使用了FreeBSD, jails和ZFS,来自动在一个mysql群集的热备主机上为一个mysq staging数据库 创建一份纯净的拷贝。 @@ -388,7 +387,7 @@ echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging ``` -### Additional Reading (延伸阅读) +### 延伸阅读 * [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs) * [FreeBSD Handbook on ZFS](https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/zfs.html) -- cgit v1.2.3 From 83eb5dd94eabe8f3309beacdbe5efa7ef719fefe Mon Sep 17 00:00:00 2001 From: WeizhongTu Date: Thu, 23 Jun 2016 13:50:44 +0800 Subject: fix spelling mistake --- zh-cn/r-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/r-cn.html.markdown b/zh-cn/r-cn.html.markdown index 0c46bc22..55a29b11 100644 --- a/zh-cn/r-cn.html.markdown +++ b/zh-cn/r-cn.html.markdown @@ -285,7 +285,7 @@ while (a > 4) { } # 记住,在 R 语言中 for / while 循环都很慢 -# 建议使用 apply()(我们一会介绍)来错做一串数据(比如一列或者一行数据) +# 建议使用 apply()(我们一会介绍)来操作一串数据(比如一列或者一行数据) # IF/ELSE -- cgit v1.2.3 From a186508d6f4c57dba8f1f573b476741a45f7895f Mon Sep 17 00:00:00 2001 From: LuJimin Date: Sun, 31 Jul 2016 13:04:45 +0800 Subject: =?UTF-8?q?=E5=88=9B=E5=BB=BAKotlin=E7=9A=84=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 创建Kotlin的中文翻译版本 --- zh-cn/kotlin-cn.html.markdown | 346 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 zh-cn/kotlin-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown new file mode 100644 index 00000000..ac15c564 --- /dev/null +++ b/zh-cn/kotlin-cn.html.markdown @@ -0,0 +1,346 @@ +--- +language: kotlin +contributors: + - ["S Webber", "https://github.com/s-webber"] +translators: + - ["Jimin Lu", "https://github.com/lujimin"] +filename: LearnKotlin-cn.kt +--- + +Kotlin是一门适用于JVM、Android和浏览器的静态类型编程语言。它100%兼容Java。 +[了解更多。](https://kotlinlang.org/) + +```java +// 单行注释从 // 开始 +/* +多行注释看起来像这样。 +*/ + +// "package" 关键字的工作方式与Java相同。 +package com.learnxinyminutes.kotlin + +/* +Kotlin程序的入口点是一个"main"函数 +该函数传递一个包含任何命令行参数的数组。 +*/ +fun main(args: Array) { + /* + 使用"var"或"val"来声明一个值。 + "val"声明的值不能被重新赋值,而"vars"声明的值可以。 + */ + val fooVal = 10 // 以后我们不能再次给fooVal赋值 + var fooVar = 10 + fooVar = 20 // fooVar可以被再次赋值 + + /* + 在大多数情况下,Kotlin可以确定变量的类型是什么, + 所以我们不必要每次都去明确指定它。 + 我们可以像这样明确地声明一个变量的类型: + */ + val foo : Int = 7 + + /* + 可以采取和Java类似的方法来表示一个字符串。 + 用反斜杠来转义字符。 + */ + val fooString = "My String Is Here!"; + val barString = "Printing on a new line?\nNo Problem!"; + val bazString = "Do you want to add a tab?\tNo Problem!"; + println(fooString); + println(barString); + println(bazString); + + /* + 原始字符串用三重引号(""")来定义。 + 原始字符串可以包含新的行以及其他任何字符。 + */ + val fooRawString = """ +fun helloWorld(val name : String) { + println("Hello, world!") +} +""" + println(fooRawString) + + /* + 字符串可以包含模板表达式。 + 模板表达式从一个美元符号($)开始。 + */ + val fooTemplateString = "$fooString has ${fooString.length} characters" + println(fooTemplateString) + + /* + 当某个变量的值可以为 null 的时候,我们必须被明确指定它是可为空的。 + 在变量声明处的类型后面加上?来标识它是可为空的。 + 我们可以用?.操作符来访问可为空的变量。 + 我们可以用?:操作符来指定一个替代值在变量为空的时候使用。 + */ + var fooNullable: String? = "abc" + println(fooNullable?.length) // => 3 + println(fooNullable?.length ?: -1) // => 3 + fooNullable = null + println(fooNullable?.length) // => null + println(fooNullable?.length ?: -1) // => -1 + + /* + 使用"fun"关键字来声明一个函数。 + 函数的参数在函数名后面的括号内指定。 + 函数的参数可以设定一个默认值。 + 如果需要的话,函数的返回值类型可以在参数后面指定。 + */ + fun hello(name: String = "world") : String { + return "Hello, $name!" + } + println(hello("foo")) // => Hello, foo! + println(hello(name = "bar")) // => Hello, bar! + println(hello()) // => Hello, world! + + /* + 用"vararg"关键字来修饰一个函数的参数 + 来允许可变参数传递给该函数 + */ + fun varargExample(vararg names: Int) { + println("Argument has ${names.size} elements") + } + varargExample() // => Argument has 0 elements + varargExample(1) // => Argument has 1 elements + varargExample(1, 2, 3) // => Argument has 3 elements + + /* + 当函数只包含一个单独的表达式时,大括号可以被省略。 + 函数体可以被指定在一个=符号后面。 + */ + fun odd(x: Int): Boolean = x % 2 == 1 + println(odd(6)) // => false + println(odd(7)) // => true + + // 如果返回值类型可以被推断,那么我们不需要指定它。 + fun even(x: Int) = x % 2 == 0 + println(even(6)) // => true + println(even(7)) // => false + + // 函数可以用函数作为参数并且可以返回函数。 + fun not(f: (Int) -> Boolean) : (Int) -> Boolean { + return {n -> !f.invoke(n)} + } + // 命名函数可以被指定为参数使用::操作符。 + val notOdd = not(::odd) + val notEven = not(::even) + // 匿名函数可以被指定为参数。 + val notZero = not {n -> n == 0} + /* + 如果一个匿名函数只有一个参数 + 那么它的声明可以被省略(连同->)。 + 这个参数的名字是"it"。 + */ + val notPositive = not {it > 0} + for (i in 0..4) { + println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}") + } + + // "class"关键字用来声明类。 + class ExampleClass(val x: Int) { + fun memberFunction(y: Int) : Int { + return x + y + } + + infix fun infixMemberFunction(y: Int) : Int { + return x * y + } + } + /* + 我们调用构造方法来创建一个新的实例。 + 注意,Kotlin没有"new"关键字。 + */ + val fooExampleClass = ExampleClass(7) + // 可以使用一个点号来调用成员函数。 + println(fooExampleClass.memberFunction(4)) // => 11 + /* + 如果使用"infix"关键字来标记一个函数 + 那么可以使用中缀表示法来调用该函数。 + */ + println(fooExampleClass infixMemberFunction 4) // => 28 + + /* + 数据类是创建只包含数据的类的一个简洁的方法。 + "hashCode"、"equals"和"toString"方法将被自动生成。 + */ + data class DataClassExample (val x: Int, val y: Int, val z: Int) + val fooData = DataClassExample(1, 2, 4) + println(fooData) // => DataClassExample(x=1, y=2, z=4) + + // 数据类有一个"copy"函数 + val fooCopy = fooData.copy(y = 100) + println(fooCopy) // => DataClassExample(x=1, y=100, z=4) + + // 对象可以被解构成为多个变量 + val (a, b, c) = fooCopy + println("$a $b $c") // => 1 100 4 + + // "with"函数类似于JavaScript中的"with"用法。 + data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) + val fooMutableDate = MutableDataClassExample(7, 4, 9) + with (fooMutableDate) { + x -= 2 + y += 2 + z-- + } + println(fooMutableDate) // => MutableDataClassExample(x=5, y=6, z=8) + + /* + 我们可以使用"listOf"函数来创建一个list。 + 这个list是不可变的 - 元素不可以被添加或删除。 + */ + val fooList = listOf("a", "b", "c") + println(fooList.size) // => 3 + println(fooList.first()) // => a + println(fooList.last()) // => c + // 可以通过索引来访问list中的元素。 + println(fooList[1]) // => b + + // 可以使用"mutableListOf"函数来创建一个可变的list。 + val fooMutableList = mutableListOf("a", "b", "c") + fooMutableList.add("d") + println(fooMutableList.last()) // => d + println(fooMutableList.size) // => 4 + + // 我们可以使用"setOf"函数来创建一个set。 + val fooSet = setOf("a", "b", "c") + println(fooSet.contains("a")) // => true + println(fooSet.contains("z")) // => false + + // 我们可以使用"mapOf"函数来创建一个map。 + val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9) + // 可以通过键来访问map中的值。 + println(fooMap["a"]) // => 8 + + /* + 序列表示惰性求值集合。 + 我们可以使用"generateSequence"函数来创建一个序列。 + */ + val fooSequence = generateSequence(1, {it + 1}) + val x = fooSequence.take(10).toList() + println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + // 一个用序列来生成斐波那契数列的例子。 + fun fibonacciSequence() : Sequence { + var a = 0L + var b = 1L + + fun next() : Long { + val result = a + b + a = b + b = result + return a + } + + return generateSequence(::next) + } + val y = fibonacciSequence().take(10).toList() + println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + + // Kotlin为集合提供高阶函数。 + val z = (1..9).map {it * 3} + .filter {it < 20} + .groupBy {it % 2 == 0} + .mapKeys {if (it.key) "even" else "odd"} + println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]} + + // 任何提供迭代器的都可以使用"for"循环。 + for (c in "hello") { + println(c) + } + + // "while"循环的用法和其他语言一样。 + var ctr = 0 + while (ctr < 5) { + println(ctr) + ctr++ + } + do { + println(ctr) + ctr++ + } while (ctr < 10) + + // "when"可以用来替代"if-else if"链。 + val i = 10 + when { + i < 7 -> println("first block") + fooString.startsWith("hello") -> println("second block") + else -> println("else block") + } + + // "when"可以带参数。 + when (i) { + 0, 21 -> println("0 or 21") + in 1..20 -> println("in the range 1 to 20") + else -> println("none of the above") + } + + // "when"可以作为一个函数,提供返回值。 + var result = when (i) { + 0, 21 -> "0 or 21" + in 1..20 -> "in the range 1 to 20" + else -> "none of the above" + } + println(result) + + /* + 我们可以通过使用"is"操作符来检查一个对象是否是特定类型的。 + 如果对象通过了类型检查那么它可以作为该类型使用而不需要强制转换它。 + */ + fun smartCastExample(x: Any) : Boolean { + if (x is Boolean) { + // x自动转换为Boolean + return x + } else if (x is Int) { + // x自动转换为Int + return x > 0 + } else if (x is String) { + // x自动转换为String + return x.isNotEmpty() + } else { + return false + } + } + println(smartCastExample("Hello, world!")) // => true + println(smartCastExample("")) // => false + println(smartCastExample(5)) // => true + println(smartCastExample(0)) // => false + println(smartCastExample(true)) // => true + + /* + 扩展是用来添加新的功能到一个类的。 + 它类似于C#的扩展方法。 + */ + fun String.remove(c: Char): String { + return this.filter {it != c} + } + println("Hello, world!".remove('l')) // => Heo, word! + + println(EnumExample.A) // => A + println(ObjectExample.hello()) // => hello +} + +// 枚举类和Java的枚举类型类似。 +enum class EnumExample { + A, B, C +} + +/* +"object"关键字用来创建单例对象。 +我们不能把它赋给一个变量,但我们可以通过它的名字引用它。 +这类似于Scala的单例对象。 +*/ +object ObjectExample { + fun hello() : String { + return "hello" + } +} + +``` + +### 进一步阅读 + +* [Kotlin教程](https://kotlinlang.org/docs/tutorials/) +* [在您的浏览器中使用Kotlin](http://try.kotlinlang.org/) +* [Kotlin资源列表](http://kotlin.link/) -- cgit v1.2.3 From 7645f54e5eb3be229ed05bc99f1fc0c5db4c90fb Mon Sep 17 00:00:00 2001 From: LuJimin Date: Sun, 31 Jul 2016 13:13:44 +0800 Subject: fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改bug --- zh-cn/kotlin-cn.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'zh-cn') diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown index ac15c564..5998f33f 100644 --- a/zh-cn/kotlin-cn.html.markdown +++ b/zh-cn/kotlin-cn.html.markdown @@ -5,6 +5,7 @@ contributors: translators: - ["Jimin Lu", "https://github.com/lujimin"] filename: LearnKotlin-cn.kt +lang: zh-cn --- Kotlin是一门适用于JVM、Android和浏览器的静态类型编程语言。它100%兼容Java。 -- cgit v1.2.3 From bc158cba137307ff34f4ef4b6b3e7fde1afb9179 Mon Sep 17 00:00:00 2001 From: LuJimin Date: Tue, 2 Aug 2016 23:51:15 +0800 Subject: fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改一些小问题。 --- zh-cn/kotlin-cn.html.markdown | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown index 5998f33f..1fd12f5b 100644 --- a/zh-cn/kotlin-cn.html.markdown +++ b/zh-cn/kotlin-cn.html.markdown @@ -27,7 +27,7 @@ Kotlin程序的入口点是一个"main"函数 fun main(args: Array) { /* 使用"var"或"val"来声明一个值。 - "val"声明的值不能被重新赋值,而"vars"声明的值可以。 + "val"声明的值不能被重新赋值,而"var"声明的值可以。 */ val fooVal = 10 // 以后我们不能再次给fooVal赋值 var fooVar = 10 @@ -53,7 +53,7 @@ fun main(args: Array) { /* 原始字符串用三重引号(""")来定义。 - 原始字符串可以包含新的行以及其他任何字符。 + 原始字符串可以包含换行符以及其他任何字符。 */ val fooRawString = """ fun helloWorld(val name : String) { @@ -73,7 +73,7 @@ fun helloWorld(val name : String) { 当某个变量的值可以为 null 的时候,我们必须被明确指定它是可为空的。 在变量声明处的类型后面加上?来标识它是可为空的。 我们可以用?.操作符来访问可为空的变量。 - 我们可以用?:操作符来指定一个替代值在变量为空的时候使用。 + 我们可以用?:操作符来指定一个在变量为空时使用的替代值。 */ var fooNullable: String? = "abc" println(fooNullable?.length) // => 3 @@ -96,8 +96,7 @@ fun helloWorld(val name : String) { println(hello()) // => Hello, world! /* - 用"vararg"关键字来修饰一个函数的参数 - 来允许可变参数传递给该函数 + 用"vararg"关键字来修饰一个函数的参数来允许可变参数传递给该函数 */ fun varargExample(vararg names: Int) { println("Argument has ${names.size} elements") @@ -123,7 +122,7 @@ fun helloWorld(val name : String) { fun not(f: (Int) -> Boolean) : (Int) -> Boolean { return {n -> !f.invoke(n)} } - // 命名函数可以被指定为参数使用::操作符。 + // 命名函数可以用::运算符被指定为参数。 val notOdd = not(::odd) val notEven = not(::even) // 匿名函数可以被指定为参数。 @@ -286,7 +285,7 @@ fun helloWorld(val name : String) { println(result) /* - 我们可以通过使用"is"操作符来检查一个对象是否是特定类型的。 + 我们可以通过使用"is"操作符来检查一个对象是否是某个类型的。 如果对象通过了类型检查那么它可以作为该类型使用而不需要强制转换它。 */ fun smartCastExample(x: Any) : Boolean { @@ -310,7 +309,7 @@ fun helloWorld(val name : String) { println(smartCastExample(true)) // => true /* - 扩展是用来添加新的功能到一个类的。 + 扩展是用来给一个类添加新的功能的。 它类似于C#的扩展方法。 */ fun String.remove(c: Char): String { -- cgit v1.2.3 From 12e1739706da8ead292f1c21c4cf54afbd4f4087 Mon Sep 17 00:00:00 2001 From: Jakukyo Friel Date: Sun, 21 Aug 2016 15:51:29 +0800 Subject: go: [zh-cn] fix a typo. --- zh-cn/go-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/go-cn.html.markdown b/zh-cn/go-cn.html.markdown index 49224085..fa4540a2 100644 --- a/zh-cn/go-cn.html.markdown +++ b/zh-cn/go-cn.html.markdown @@ -129,7 +129,7 @@ func learnFlowControl() { fmt.Println("told ya") } // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了, - // 也不用容忍被人的代码风格。 + // 也不用容忍别人的代码风格。 if false { // pout } else { -- cgit v1.2.3 From f4371a8367ab4dbb5ac3e112fe6a5fc6654a28c8 Mon Sep 17 00:00:00 2001 From: Jiang haiyun Date: Thu, 25 Aug 2016 14:32:09 +0800 Subject: Sync with the English version & minor tweaks --- zh-cn/yaml-cn.html.markdown | 82 ++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 24 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/yaml-cn.html.markdown b/zh-cn/yaml-cn.html.markdown index fc510eb5..bbda20e9 100644 --- a/zh-cn/yaml-cn.html.markdown +++ b/zh-cn/yaml-cn.html.markdown @@ -4,33 +4,36 @@ contributors: - ["Adam Brenecki", "https://github.com/adambrenecki"] translators: - ["Zach Zhang", "https://github.com/checkcheckzz"] + - ["Jiang Haiyun", "https://github.com/haiiiiiyun"] filename: learnyaml-cn.yaml lang: zh-cn --- -YAML是一个数据序列化语言,被设计成人类直接可写可读的。 +YAML 是一个数据序列化语言,被设计成人类直接可写可读的。 -它是JSON的严格超集,增加了语法显著换行符和缩进,就像Python。但和Python不一样, -YAML根本不容许文字制表符。 +它是 JSON 的严格超集,增加了语法显著换行符和缩进,就像 Python。但和 Python 不一样, +YAML 根本不容许文字制表符。 ```yaml -# YAML中的注解看起来像这样。 +# YAML 中的注解看起来像这样。 ################ -# 标量类型 # +# 标量类型 # ################ -# 我们的根对象 (它们在整个文件里延续) 将会是一个地图, +# 我们的根对象 (它们在整个文件里延续) 将会是一个映射, # 它等价于在别的语言里的一个字典,哈西表或对象。 key: value another_key: Another value goes here. a_number_value: 100 +# 如果你想将数字 1 作为值,你必须要将它括在引号中。 +# 不然 YAML 解析器会假定它是一个布尔值 true。 scientific_notation: 1e+12 boolean: true null_value: null key with spaces: value -# 注意到字符串不需要被引用。但是,它们可以被引用。 +# 注意到字符串不需要被括在引号中。但是,它们可以被括起来。 "Keys can be quoted too.": "Useful if you want to put a ':' in your key." # 多行字符串既可以写成像一个'文字块'(使用 |), @@ -54,7 +57,7 @@ folded_style: > this text will appear over two lines. #################### -# 集合类型 # +# 集合类型 # #################### # 嵌套是通过缩进完成的。 @@ -64,18 +67,24 @@ a_nested_map: another_nested_map: hello: hello -# 地图不用有字符串键值。 +# 映射的键值不必是字符串。 0.25: a float key -# 键值也可以是多行对象,用?表明键值的开始。 +# 键值也可以是复合型的,比如多行对象 +# 我们用 ? 后跟一个空格来表示一个复合键的开始。 ? | This is a key that has multiple lines : and this is its value -# YAML也容许键值是集合类型,但是很多语言将会抱怨。 +# YAML 也允许使用复杂键语法表示序列间的映射关系。 +# 但有些语言的解析器可能会不支持。 +# 一个例子: +? - Manchester United + - Real Madrid +: [ 2001-01-01, 2002-02-02 ] -# 序列 (等价于表或数组) 看起来像这样: +# 序列 (等价于列表或数组) 看起来像这样: a_sequence: - Item 1 - Item 2 @@ -87,50 +96,75 @@ a_sequence: - This is a sequence - inside another sequence -# 因为YAML是JSON的超集,你也可以写JSON风格的地图和序列: +# 因为 YAML 是 JSON 的超集,你也可以写 JSON 风格的映射和序列: json_map: {"key": "value"} json_seq: [3, 2, 1, "takeoff"] ####################### -# 其余的YAML特点 # +# 其余的 YAML 特性 # ####################### -# YAML还有一个方便的特点叫'锚',它让你简单地在整个文件里重复内容。 -# 两个键值将会有相同的值: +# YAML 还有一个方便的特性叫 '锚',它能让你很容易在文档中进行文本复用。 +# 如下两个键会有相同的值: anchored_content: &anchor_name This string will appear as the value of two keys. other_anchor: *anchor_name -# YAML还有标签,你可以用它显示地声明类型。 +# 锚也可被用来复制/继承属性 +base: &base + name: Everyone has same name + +foo: &foo + <<: *base + age: 10 + +bar: &bar + <<: *base + age: 20 + +# foo 和 bar 将都含有 name: Everyone has same name + +# YAML 还有标签,你可以用它显示地声明类型。 explicit_string: !!str 0.5 -# 一些解析器实现特定语言的标签,就像这个为了Python的复数类型。 +# 一些解析器实现特定语言的标签,就像这个针对 Python 的复数类型。 python_complex_number: !!python/complex 1+2j +# 我们也可以在 YAML 的复合键中使用特定语言的标签 +? !!python/tuple [5, 7] +: Fifty Seven +# 将会是 Python 中的 {(5, 7): 'Fifty Seven'} + #################### -# 其余的YAML类型 # +# 其余的 YAML 类型 # #################### -# 字符串和数字不是仅有的YAML可以理解的标量。 -# ISO 格式的日期和日期时间文字也是可以被解析的。 +# 除了字符串和数字,YAML 还能理解其它标量。 +# ISO 格式的日期和日期时间文本也可以被解析。 datetime: 2001-12-15T02:59:43.1Z datetime_with_spaces: 2001-12-14 21:59:43.10 -5 date: 2002-12-14 -# 这个!!binary标签表明一个字符串实际上是一个二进制blob的base64编码表示。 +# 这个 !!binary 标签表明这个字符串实际上 +# 是一个用 base64 编码表示的二进制 blob。 gif_file: !!binary | R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= -# YAML还有一个集合类型,它看起来像这样: +# YAML 还有一个集合类型,它看起来像这样: set: ? item1 ? item2 ? item3 -# 像Python一样,集合仅是有null数值的地图;上面的集合等价于: +# 像 Python 一样,集合仅是值为 null 的映射;上面的集合等价于: set2: item1: null item2: null item3: null ``` + +### 更多资源 + ++ [YAML official website](http://yaml.org/) ++ [Online YAML Validator](http://codebeautify.org/yaml-validator) -- cgit v1.2.3 From 1732caa9f45d512601bd6457a7672072a9879c84 Mon Sep 17 00:00:00 2001 From: Jiang haiyun Date: Fri, 26 Aug 2016 08:43:08 +0800 Subject: [vim/zh-cn] translation --- zh-cn/vim-cn.html.markdown | 236 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 zh-cn/vim-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/vim-cn.html.markdown b/zh-cn/vim-cn.html.markdown new file mode 100644 index 00000000..0c3f6043 --- /dev/null +++ b/zh-cn/vim-cn.html.markdown @@ -0,0 +1,236 @@ +--- +category: tool +tool: vim +filename: LearnVim-cn.txt +contributors: + - ["RadhikaG", "https://github.com/RadhikaG"] +translators: + - ["Jiang Haiyun", "https://github.com/haiiiiiyun"] +lang: zh-cn +--- + + +[Vim](www.vim.org) +(Vi IMproved) 是 Unix 上的流行编辑器 vi 的克隆版本。这个文本编辑器 +是为性能和提升效率而设计的,并且在大多数基于 unix 的系统上普遍存在。 +它有大量的快捷键可用来快速导航到文件的特定位置,以便进行快速编辑。 + +## Vim 导航基础 + +``` + vim # 在 Vim 中打开 + :q # 退出 Vim + :w # 保存当前文件 + :wq # 保存文件并退出 Vim + :q! # 退出 Vim 并且不保存文件 + # ! *强制* 执行 :q, 因此没有保存就退出 Vim + :x # 保存文件并且退出 Vim, 是 :wq 的简写版本 + + u # 撤销 + CTRL+R # 重做 + + h # 左移一个字符 + j # 下移一行 + k # 上移一行 + l # 右移一个字符 + + # 在行内移动 + + 0 # 移到行首 + $ # 移到行尾 + ^ # 移到行内的第一个非空白字符处 + + # 在文本中查找 + + /word # 光标之后的所有该词都高亮显示 + ?word # 光标之前的所有该词都高亮显示 + n # 查找后将光标移到该词的下一个出现位置 + N # 光标移到该词的上一个出现位置 + + :%s/foo/bar/g # 将文件每一行上的所有 'foo' 都改成 'bar' + :s/foo/bar/g # 将当前行上的所有 'foo' 都改成 'bar' + + # 跳到字符处 + + f<字符> # 向前跳移到 <字符> 上 + t<字符> # 向前跳移到 <字符> 的左侧 + + # 例如, + f< # 向前跣到 < 上 + t< # 向前跳移到 < 的左侧 + + # 按词移动 + + w # 前移一个词 + b # 后移一个词 + e # 移到当前词的末尾 + + # 移动的其它命令 + + gg # 移到文件顶部 + G # 移到文件末尾 + :NUM # 移到第 NUM 行 (NUM 是任意数字) + H # 移到屏幕顶部 + M # 移到屏幕中间位置 + L # 移到屏幕末尾 +``` + +## 模式: + +Vim 基于 **模式** 这个概念。 + +命令模式 - Vim 启动后就处于这个模式,用于导航和操作命令 +插入模式 - 用于在你的文件中进行修改 +可视模式 - 用于高亮文本并对它们进行操作 +Ex 模式 - 用于跳到底部的 ':' 提示行上输入命令 + +``` + i # 在光标位置前,将 Vim 切换到插入模式 + a # 在光标位置后,将 Vim 切换到插入模式 + v # 将 Vim 切换到可视模式 + : # 将 Vim 切换到 ex 模式 + # 无论你当前处于什么模式,都返回到命令模式 + + # 复制和粘贴文本 + + y # 复制所选的内容 + yy # 复制当前行 + d # 删除所选的内容 + dd # 删除当前行 + p # 在当前光标位置后粘贴复制的文本 + P # 在当前光标位置前粘贴复制的文本 + x # 删除当前光标位置处的字符 +``` + +## Vim 的 '语法' + +Vim 可以被认为是按 '动词-修饰词-名词' 格式编排的一组命令: + +动词 - 你的动作 +修饰词 - 你如何执行你的动作 +名词 - 你的动作所作用于的对象 + +关于 '动词','修饰词',和 '名词' 的几个重要例子: + +``` + # '动词' + + d # 删除 + c # 修改 + y # 复制 + v # 可视化选择 + + # '修饰词' + + i # 内部的 + a # 周围的 + NUM # 数字 (NUM 是任意数字) + f # 查找文本并位于其上 + t # 查找文本并停于其前面 + / # 从光标处开始查找字符串 + ? # 在光标前查找字符串 + + # '名词' + + w # 词 + s # 句子 + p # 段落 + b # 块 + + # 示例 '语句' 或命令 + + d2w # 删除 2 个词 + cis # 修改段落内的内容 + yip # 复制段落内的内容 (复制你所在的段落) + ct< # 修改直到括号开启处 + # 对你的当前位置直到下个括号开启处的内容进行修改 + d$ # 删除直到行尾 +``` + +## 一些快捷键和技巧 + + +``` + > # 将所选内容缩进一级 + < # 将所选内容取消缩进一级 + :earlier 15m # 将文档还原到 15 分钟前的状态 + :later 15m # 逆转上述命令 + ddp # 相邻行交换位置,先 dd 再 p + . # 重复之前动作 +``` + +## 宏 + +宏基本上来说就是可录制的动作。 +当你开始录制宏时,它会记录你使用的 **每个** 动作和命令, +直到你停止录制。当调用宏时,它会将这个完全相同的动作和命令序列 +再次应用于所选文本之上。 + +``` + qa # 开始录制一个叫 'a' 的宏 + q # 停止录制 + @a # 重播宏 +``` + +### 配置 ~/.vimrc + +.vimrc 可用于在启动时对 Vim 进行配置。 + +这里是一个示例 ~/.vimrc 文件: + +``` +" 示例 ~/.vimrc +" 2015.10 + +" 需要 Vim iMproved 版本 +set nocompatible + +" 根据文件名检测文件类型,以便能进行智能自动缩进等操作。 +filetype indent plugin on + +" 开启语法高亮 +syntax on + +" 更好的命令行补全 +set wildmenu + +" 除了当使用大写字母时使用大小写无关查找 +set ignorecase +set smartcase + +" 当新开一行时,如果没有开启文件特定的缩进规则, +" 则缩进保持与你当前行一致 +set autoindent + +" 在左侧显示行号 +set number + +" 缩进选项,根据个人偏好进行修改 + +" 每个 TAB 的可视空格数 +set tabstop=4 + +" 编辑时 TAB 对应的空格数 +set softtabstop=4 + +" 当使用缩进操作 (>> 和 <<) 时缩进的空格数 +set shiftwidth=4 + +" 将 TAB 转换成空格 +set expandtab + +" 为缩进和对齐开启智能化的 TAB 和空格切换功能 +set smarttab +``` + +### 参考 + +[Vim | Home](http://www.vim.org/index.php) + +`$ vimtutor` + +[A vim Tutorial and Primer](https://danielmiessler.com/study/vim/) + +[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about) + +[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim) -- cgit v1.2.3 From 00302473841679ef7d3115fa03aac481f31d390f Mon Sep 17 00:00:00 2001 From: haiiiiiyun Date: Sat, 27 Aug 2016 13:38:04 +0800 Subject: zh-cn ruby page sync with English version & minor tweaks --- zh-cn/ruby-cn.html.markdown | 401 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 339 insertions(+), 62 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/ruby-cn.html.markdown b/zh-cn/ruby-cn.html.markdown index 14d38137..2d181de0 100644 --- a/zh-cn/ruby-cn.html.markdown +++ b/zh-cn/ruby-cn.html.markdown @@ -10,6 +10,7 @@ contributors: - ["ftwbzhao", "https://github.com/ftwbzhao"] translators: - ["Lin Xiangyu", "https://github.com/oa414"] + - ["Jiang Haiyun", "https://github.com/haiiiiiyun"] --- ```ruby @@ -35,6 +36,13 @@ translators: 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 +2**5 #=> 32 +5 % 3 #=> 2 + +# 位运算符 +3 & 5 #=> 1 +3 | 5 #=> 7 +3 ^ 5 #=> 6 # 算术符号只是语法糖而已 # 实际上是调用对象的方法 @@ -42,7 +50,7 @@ translators: 10.* 5 #=> 50 # 特殊的值也是对象 -nil # 空 +nil # 相当于其它语言中的 null true # 真 false # 假 @@ -54,13 +62,11 @@ false.class #=> FalseClass 1 == 1 #=> true 2 == 1 #=> false -# 不等运算符 +# 不相等运算符 1 != 1 #=> false 2 != 1 #=> true -!true #=> false -!false #=> true -# 除了false自己,nil是唯一的值为false的对象 +# 除了false自己,nil是唯一的另一个值为false的对象 !nil #=> true !false #=> true @@ -72,6 +78,26 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true + +# 组合比较运算符 +1 <=> 10 #=> -1 +10 <=> 1 #=> 1 +1 <=> 1 #=> 0 + +# 逻辑运算符 +true && false #=> false +true || false #=> true +!true #=> false + +# 也有优先级更低的逻辑运算符 +# 它们用于控制流结构中,用来串接语句,直到返回true或false。 + +# `do_something_else` 只当 `do_something` 返回true时才会被调用 +do_something() and do_something_else() +# `log_error` 只当 `do_something` 返回false时才会被调用 +do_something() or log_error() + + # 字符串是对象 'I am a string'.class #=> String @@ -81,9 +107,28 @@ placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" +# 尽可能优先使用单引号的字符串 +# 双引号的字符串会进行一些额外的内部处理 + +# 合并字符串,但不能和数字合并 +'hello ' + 'world' #=> "hello world" +'hello ' + 3 #=> TypeError: can't convert Fixnum into String +'hello ' + 3.to_s #=> "hello 3" + +# 合并字符串及其运算符 +'hello ' * 3 #=> "hello hello hello " + +# 字符串追加 +'hello' << ' world' #=> "hello world" -# 输出值 +# 打印输出,并在末尾加换行符 puts "I'm printing!" +#=> I'm printing! +#=> nil + +# 打印输出,不加换行符 +print "I'm printing!" +#=> I'm printing! => nil # 变量 x = 25 #=> 25 @@ -96,17 +141,16 @@ x = y = 10 #=> 10 x #=> 10 y #=> 10 -# 按照惯例,用 snake_case 作为变量名 +# 按照惯例,使用类似snake_case风格的变量名 snake_case = true -# 使用具有描述性的运算符 +# 使用有意义的变量名 path_to_project_root = '/good/name/' path = '/bad/name/' # 符号(Symbols,也是对象) -# 符号是不可变的,内部用整数类型表示的可重用的值。 -# 通常用它代替字符串来有效地表示有意义的值。 - +# 符号是不可变的,内部用整数值表示的可重用的常数 +# 通常用它代替字符串来有效地表示有意义的值 :pending.class #=> Symbol @@ -132,26 +176,36 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] array[0] #=> 1 array[12] #=> nil -# 像运算符一样,[var]形式的访问 -# 也就是一个语法糖 -# 实际上是调用对象的[] 方法 +# 像运算符一样,[var] 形式的访问 +# 也只是语法糖 +# 实际上是调用对象的 [] 方法 array.[] 0 #=> 1 array.[] 12 #=> nil # 从尾部开始 array[-1] #=> 5 +array.last #=> 5 # 同时指定开始的位置和长度 array[2, 3] #=> [3, 4, 5] -# 或者指定一个范围 +# 将数组逆序 +a=[1,2,3] +a.reverse! #=> [3,2,1] + +# 或者指定一个区间 array[1..3] #=> [2, 3, 4] # 像这样往数组增加一个元素 array << 6 #=> [1, 2, 3, 4, 5, 6] +# 或者像这样 +array.push(6) #=> [1, 2, 3, 4, 5, 6] -# 哈希表是Ruby的键值对的基本数据结构 -# 哈希表由大括号定义 +# 检查元素是否包含在数组中 +array.include?(1) #=> true + +# 哈希表是 Ruby 的主要键/值对表示法 +# 哈希表由大括号表示 hash = {'color' => 'green', 'number' => 5} hash.keys #=> ['color', 'number'] @@ -163,19 +217,14 @@ hash['number'] #=> 5 # 查询一个不存在地键将会返回nil hash['nothing here'] #=> nil -# 用 #each 方法来枚举哈希表: -hash.each do |k, v| - puts "#{k} is #{v}" -end - -# 从Ruby 1.9开始, 用符号作为键的时候有特别的记号表示: +# 从Ruby 1.9开始,用符号作为键的时候有特别的记号表示: -new_hash = { defcon: 3, action: true} +new_hash = { defcon: 3, action: true } new_hash.keys #=> [:defcon, :action] # 小贴士:数组和哈希表都是可枚举的 -# 它们可以共享一些有用的方法,比如each, map, count 等等 +# 它们共享一些有用的方法,比如each,map,count等等 # 控制流 @@ -196,9 +245,15 @@ end #=> iteration 4 #=> iteration 5 -# 然而 -# 没人用for循环 -# 用`each`来代替,就像这样 + +# 但是,没有人用for循环。 +# 你应该使用"each"方法,然后再传给它一个块。 +# 所谓块就是可以传给像"each"这样的方法的代码段。 +# 它类似于其它语言中的lambdas, 匿名函数或闭包。 +# +# 区间上的"each"方法会对区间中的每个元素运行一次块代码。 +# 我们将counter作为一个参数传给了块。 +# 调用带有块的"each"方法看起来如下: (1..5).each do |counter| puts "iteration #{counter}" @@ -209,6 +264,23 @@ end #=> iteration 4 #=> iteration 5 +# 你也可以将块包含在一个大括号中: +(1..5).each { |counter| puts "iteration #{counter}" } + +# 数据结构中的内容也可以使用each来遍历。 +array.each do |element| + puts "#{element} is part of the array" +end +hash.each do |key, value| + puts "#{key} is #{value}" +end + +# 如果你还需要索引值,可以使用"each_with_index",并且定义 +# 一个索引变量 +array.each_with_index do |element, index| + puts "#{element} is number #{index} in the array" +end + counter = 1 while counter <= 5 do puts "iteration #{counter}" @@ -220,6 +292,20 @@ end #=> iteration 4 #=> iteration 5 +# Ruby 中还有很多有用的循环遍历函数, +# 如"map","reduce","inject"等等。 +# 以map为例,它会遍历数组,并根据你在 +# 块中定义的逻辑对它进行处理,然后返回 +# 一个全新的数组。 +array = [1,2,3,4,5] +doubled = array.map do |element| + element * 2 +end +puts doubled +#=> [2,4,6,8,10] +puts array +#=> [1,2,3,4,5] + grade = 'B' case grade @@ -236,6 +322,33 @@ when 'F' else puts "Alternative grading system, eh?" end +#=> "Better luck next time" + +# case也可以用区间 +grade = 82 +case grade +when 90..100 + puts 'Hooray!' +when 80...90 + puts 'OK job' +else + puts 'You failed!' +end +#=> "OK job" + +# 异常处理: +begin + # 这里的代码可能会抛出异常 + raise NoMemoryError, 'You ran out of memory.' +rescue NoMemoryError => exception_variable + puts 'NoMemoryError was raised', exception_variable +rescue RuntimeError => other_exception_variable + puts 'RuntimeError was raised now' +else + puts 'This runs if no exceptions were thrown at all' +ensure + puts 'This code always runs no matter what' +end # 函数 @@ -243,7 +356,7 @@ def double(x) x * 2 end -# 函数 (以及所有的方法块) 隐式地返回了最后语句的值 +# 函数 (以及所有的块) 隐式地返回最后语句的值 double(2) #=> 4 # 当不存在歧义的时候括号是可有可无的 @@ -261,8 +374,8 @@ sum 3, 4 #=> 7 sum sum(3,4), 5 #=> 12 # yield -# 所有的方法都有一个隐式的块参数 -# 可以用yield参数调用 +# 所有的方法都有一个隐式的,可选的块参数 +# 可以用 'yield' 关键字调用 def surround puts "{" @@ -276,45 +389,84 @@ surround { puts 'hello world' } # hello world # } +# 可以向函数传递一个块 +# "&"标记传递的块是一个引用 +def guests(&block) + block.call 'some_argument' +end -# 用class关键字定义一个类 -class Human - - # 一个类变量,它被这个类地所有实例变量共享 - @@species = "H. sapiens" +# 可以传递多个参数,这些参数会转成一个数组, +# 这也是使用星号符 ("*") 的原因: +def guests(*array) + array.each { |guest| puts guest } +end - # 构造函数 - def initialize(name, age=0) - # 将参数name的值赋给实例变量@name - @name = name - # 如果没有给出age, 那么会采用参数列表中地默认地值 - @age = age - end +# 如果函数返回一个数组,在赋值时可以进行拆分: +def foods + ['pancake', 'sandwich', 'quesadilla'] +end +breakfast, lunch, dinner = foods +breakfast #=> 'pancake' +dinner #=> 'quesadilla' - # 基本的 setter 方法 - def name=(name) - @name = name - end +# 按照惯例,所有返回布尔值的方法都以?结尾 +5.even? # false +5.odd? # true - # 基本地 getter 方法 - def name - @name - end +# 如果方法名末尾有!,表示会做一些破坏性的操作,比如修改调用者自身。 +# 很多方法都会有一个!的版本来进行修改,和一个非!的版本 +# 只用来返回更新了的结果 +company_name = "Dunder Mifflin" +company_name.upcase #=> "DUNDER MIFFLIN" +company_name #=> "Dunder Mifflin" +company_name.upcase! # we're mutating company_name this time! +company_name #=> "DUNDER MIFFLIN" - # 一个类方法以self.开头 - # 它可以被类调用,但不能被类的实例调用 - def self.say(msg) - puts "#{msg}" - end - def species - @@species - end +# 用class关键字定义一个类 +class Human + # 一个类变量,它被这个类的所有实例变量共享 + @@species = "H. sapiens" + + # 基本构造函数 + def initialize(name, age = 0) + # 将参数值赋给实例变量"name" + @name = name + # 如果没有给出age,那么会采用参数列表中的默认值 + @age = age + end + + # 基本的setter方法 + def name=(name) + @name = name + end + + # 基本地getter方法 + def name + @name + end + + # 以上的功能也可以用下面的attr_accessor来封装 + attr_accessor :name + + # Getter/setter方法也可以像这样单独创建 + attr_reader :name + attr_writer :name + + # 类方法通过使用self与实例方法区别开来。 + # 它只能通过类来调用,不能通过实例调用。 + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end end -# 类的例子 +# 初始化一个类 jim = Human.new("Jim Halpert") dwight = Human.new("Dwight K. Schrute") @@ -327,7 +479,132 @@ jim.name #=> "Jim Halpert II" dwight.species #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" -# 调用对象的方法 -Human.say("Hi") #=> "Hi" +# 调用类方法 +Human.say('Hi') #=> "Hi" + +# 变量的作用域由它们的名字格式定义 +# 以$开头的变量具有全局域 +$var = "I'm a global var" +defined? $var #=> "global-variable" + +# 以@开头的变量具有实例作用域 +@var = "I'm an instance var" +defined? @var #=> "instance-variable" + +# 以@@开头的变量具有类作用域 +@@var = "I'm a class var" +defined? @@var #=> "class variable" + +# 以大写字母开头的变量是常数 +Var = "I'm a constant" +defined? Var #=> "constant" + +# 类也是对象。因此类也可以有实例变量。 +# 类变量在类以及其继承者之间共享。 + +# 基类 +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 + +module ModuleExample + def foo + 'foo' + end +end + +# '包含'模块后,模块的方法会绑定为类的实例方法 +# '扩展'模块后,模块的方法会绑定为类方法 + +class Person + include ModuleExample +end + +class Book + extend ModuleExample +end + +Person.foo # => NoMethodError: undefined method `foo' for Person:Class +Person.new.foo # => 'foo' +Book.foo # => 'foo' +Book.new.foo # => NoMethodError: undefined method `foo' + +# 当包含或扩展一个模块时,相应的回调代码会被执行。 +module ConcernExample + def self.included(base) + base.extend(ClassMethods) + base.send(:include, InstanceMethods) + end + + module ClassMethods + def bar + 'bar' + end + end + + module InstanceMethods + def qux + 'qux' + end + end +end + +class Something + include ConcernExample +end + +Something.bar # => 'bar' +Something.qux # => NoMethodError: undefined method `qux' +Something.new.bar # => NoMethodError: undefined method `bar' +Something.new.qux # => 'qux' ``` + + +## 其它资源 + +- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges. +- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials. +- [Official Documentation](http://ruby-doc.org/core) +- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/) +- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online. +- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide. +- [Try Ruby](http://tryruby.org) - Learn the basic of Ruby programming language, interactive in the browser. -- cgit v1.2.3 From acad88cde9dd5d5584126a4cde7f772587181f78 Mon Sep 17 00:00:00 2001 From: Bo Liu Date: Wed, 7 Sep 2016 16:17:35 +0800 Subject: Update c++-cn.html.markdown (#2362) --- zh-cn/c++-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/c++-cn.html.markdown b/zh-cn/c++-cn.html.markdown index e1551e2b..5cf7e4c5 100644 --- a/zh-cn/c++-cn.html.markdown +++ b/zh-cn/c++-cn.html.markdown @@ -188,7 +188,7 @@ int main() cout << "Your favorite number is " << myInt << "\n"; // 显示“Your favorite number is ” - cerr << "Used for error messages"; + cerr << "Used for error messages"; } ///////// -- cgit v1.2.3 From 8aaffa3f625ec16b8003ae9459666bb71436d464 Mon Sep 17 00:00:00 2001 From: Bo Liu Date: Sun, 11 Sep 2016 19:20:26 +0800 Subject: Update c++-cn.html.markdown Line 325: update myDog.printDog() to myDog.print(). --- zh-cn/c++-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/c++-cn.html.markdown b/zh-cn/c++-cn.html.markdown index 5cf7e4c5..d71aa8d6 100644 --- a/zh-cn/c++-cn.html.markdown +++ b/zh-cn/c++-cn.html.markdown @@ -322,7 +322,7 @@ int main() { Dog myDog; // 此时显示“A dog has been constructed” myDog.setName("Barkley"); myDog.setWeight(10); - myDog.printDog(); // 显示“Dog is Barkley and weighs 10 kg” + myDog.print(); // 显示“Dog is Barkley and weighs 10 kg” return 0; } // 显示“Goodbye Barkley” -- cgit v1.2.3 From f6ada69ff92e1b501114a7ea1df3073c9131a570 Mon Sep 17 00:00:00 2001 From: Abner Chou Date: Fri, 23 Sep 2016 11:48:27 -0400 Subject: Update translators info (#2383) --- zh-cn/visualbasic-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/visualbasic-cn.html.markdown b/zh-cn/visualbasic-cn.html.markdown index 95f01ed6..59f18fe2 100644 --- a/zh-cn/visualbasic-cn.html.markdown +++ b/zh-cn/visualbasic-cn.html.markdown @@ -3,7 +3,7 @@ language: Visual Basic contributors: - ["Brian Martin", "http://brianmartin.biz"] translators: - - ["Abner Chou", "http://github.com/NoahDragon"] + - ["Abner Chou", "http://cn.abnerchou.me"] lang: zh-cn filename: learnvisualbasic.vb-cn --- -- cgit v1.2.3 From 970aff0dadeeed4362881f8088e81f4d703000a7 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Tue, 27 Sep 2016 16:42:40 +0200 Subject: Fix comment in Rust docs: box -> mine (#2365) The "box" is referring to the Box variable a few lines back, but that is called "mine", not "box". --- zh-cn/rust-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/rust-cn.html.markdown b/zh-cn/rust-cn.html.markdown index 17a897df..5d3fc693 100644 --- a/zh-cn/rust-cn.html.markdown +++ b/zh-cn/rust-cn.html.markdown @@ -268,7 +268,7 @@ fn main() { var = 3; let ref_var: &i32 = &var; - println!("{}", var); //不像 `box`, `var` 还可以继续使用 + println!("{}", var); //不像 `mine`, `var` 还可以继续使用 println!("{}", *ref_var); // var = 5; // 编译报错,因为 `var` 被借用了 // *ref_var = 6; // 编译报错,因为 `ref_var` 是不可变引用 -- cgit v1.2.3 From 9234c9fea4d2db040e73def518117f4135d71940 Mon Sep 17 00:00:00 2001 From: Evan Date: Sat, 1 Oct 2016 23:33:04 +0300 Subject: Update Swift pre-decrement syntax to match v3.0 (#2395) --- zh-cn/swift-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/swift-cn.html.markdown b/zh-cn/swift-cn.html.markdown index 017a7812..cba9252d 100644 --- a/zh-cn/swift-cn.html.markdown +++ b/zh-cn/swift-cn.html.markdown @@ -373,7 +373,7 @@ internal class Rect: Shape { func shrink() { if sideLength > 0 { - --sideLength + sideLength -= 1 } } -- cgit v1.2.3 From d6f6995ddec59ecb9bb39ed97b0e47a8c19b58a6 Mon Sep 17 00:00:00 2001 From: samcv Date: Sun, 23 Oct 2016 05:19:46 -0700 Subject: Fix for the three tutorials with broken YAML markup, causing them not to be accessible at all or show up in the index (#2497) * Fix typo causing typescript-ru.html.markdown not to parse correctly in the header * Fix typo in markdown-ru.html.markdown causing page not to parse correctly * Oh no! both translator and contributors fields in the header used fullwidth commas instead of normal ones. This fixes processing of this file :) --- zh-cn/vim-cn.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/vim-cn.html.markdown b/zh-cn/vim-cn.html.markdown index 0c3f6043..5298351a 100644 --- a/zh-cn/vim-cn.html.markdown +++ b/zh-cn/vim-cn.html.markdown @@ -3,9 +3,9 @@ category: tool tool: vim filename: LearnVim-cn.txt contributors: - - ["RadhikaG", "https://github.com/RadhikaG"] + - ["RadhikaG", "https://github.com/RadhikaG"] translators: - - ["Jiang Haiyun", "https://github.com/haiiiiiyun"] + - ["Jiang Haiyun", "https://github.com/haiiiiiyun"] lang: zh-cn --- -- cgit v1.2.3 From 9dd20e7a675d2d53df354264a6905c1a4e438bc5 Mon Sep 17 00:00:00 2001 From: minoriwww Date: Wed, 26 Oct 2016 19:29:28 +0800 Subject: [latex/zh-cn] translation (#2515) --- zh-cn/latex-cn.html.markdown | 277 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 zh-cn/latex-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/latex-cn.html.markdown b/zh-cn/latex-cn.html.markdown new file mode 100644 index 00000000..83491acd --- /dev/null +++ b/zh-cn/latex-cn.html.markdown @@ -0,0 +1,277 @@ +--- +language: latex +contributors: + - ["Chaitanya Krishna Ande", "http://icymist.github.io"] + - ["Colton Kohnke", "http://github.com/voltnor"] + - ["Sricharan Chiruvolu", "http://sricharan.xyz"] + - ["Ramanan Balakrishnan", "https://github.com/ramananbalakrishnan"] + - ["Svetlana Golubeva", "https://attillax.github.io/"] +translators: + - ["Dp Leo", "https://github.com/minoriwww"] +filename: learn-latex-cn.tex +lang: zh-cn +--- + +```tex +% 所有的注释行以 % 开头 +% 没有多行注释语法 + +% LaTeX 不是一个“所见即所得” 的文字处理软件 +% 这与 MS Word,和 OpenOffice Writer 不同 + +% 每一个LaTeX命令由反斜线 (\) 开始 + +% LaTeX 文档以对编译对象文档的定义开始 +% 这些文档包括书籍,报告,演示等 +% 文档的选项出现在中括号里 +% 下例中,我们设定文章字体为12pt +\documentclass[12pt]{article} + +% 之后我们定义该文档所用的库 +% 如果想要引入图片,彩色字,或是其他语言的源码在您的文档中 +% 您需要增强 LaTeX 的功能。这将通过添加库来实现 +% 下例中将要为展示数据引入 float 和 caption 库 +% 为超链接引入 hyperref 库 +\usepackage{caption} +\usepackage{float} +\usepackage{hyperref} + +% 我们还可以定义其他文档属性! +\author{Chaitanya Krishna Ande, Colton Kohnke, Sricharan Chiruvolu \& \\ +Svetlana Golubeva} +\date{\today} +\title{Learn \LaTeX \hspace{1pt} in Y Minutes!} + +% 现在我们开始正文 +% 这一行之前都是“序章” +\begin{document} +% 如果想设定作者,时间,标题字段我们可使用 LaTeX 来建立标题页 +\maketitle + +% 分章节时,可以建立目录 +% 我们需要编译文档两次来保证他们顺序正确 +% 使用目录来分开文档是很好的做法 +% 这里我们使用 \newpage 操作符 +\newpage +\tableofcontents + +\newpage + +% 许多研究论文有摘要部分。这可以使用预定义的指令来实现 +% 它应被放在逻辑上正确的位置,即顶部标题等的下面和文章主体的上面 +% 该指令可以再报告和文章中使用 +\begin{abstract} + \LaTeX \hspace{1pt} documentation written as \LaTeX! How novel and totally not + my idea! +\end{abstract} + +% 章节指令非常直观 +% 所有章节标题会自动地添加到目录中 +\section{Introduction} +Hello, my name is Colton and together we're going to explore \LaTeX! + +\section{Another section} +This is the text for another section. I think it needs a subsection. + +\subsection{This is a subsection} % 子章节同样非常直观 +I think we need another one + +\subsubsection{Pythagoras} +Much better now. +\label{subsec:pythagoras} + +% 使用型号我们可以借助 LaTeX 内置的编号功能 +% 这一技巧也在其他指令中有效 +\section*{This is an unnumbered section} +然而并不是所有章节都要被标序号 + +\section{Some Text notes} +%\section{Spacing} % 需要增加有关空白间隔的信息 +\LaTeX \hspace{1pt} is generally pretty good about placing text where it should +go. If +a line \\ needs \\ to \\ break \\ you add \textbackslash\textbackslash +\hspace{1pt} to the source code. \\ + +\section{Lists} +Lists are one of the easiest things to create in \LaTeX! I need to go shopping +tomorrow, so let's make a grocery list. +\begin{enumerate} % 此处创建了一个“枚举”环境 + % \item 使枚举增加一个单位 + \item Salad. + \item 27 watermelon. + \item A single jackrabbit. + % 我们甚至可以通过使用 [] 覆盖美剧的数量 + \item[how many?] Medium sized squirt guns. + + Not a list item, but still part of the enumerate. + +\end{enumerate} % 所有环境都有终止符 + +\section{Math} + +使用 \LaTeX \hspace{1pt} 的一个最主要的方面是学术论文和技术文章 +通常在数学和科学的领域 +因此我们需要在文章中插入特殊符号! \\ + +数学符号极多,远超出你能在键盘上找到的那些; +集合关系符,箭头,操作符,希腊字符等等 \\ + +集合与关系在数学文章中很重要 +如声明所有 x 属于 X $\forall$ x $\in$ X. \\ +% 注意我们需要在这些符号之前和之后增加 $ 符号 +% 因为在编写时我们处于 text-mode,然而数学符号只在 math-mode 中存在 +% text mode 进入 math-mode 使用 $ 操作符 +% 反之亦然,变量同时会在 math-mode 中被渲染。 +% 我们也可以使用 \[\] 来进入 math mode + +\[a^2 + b^2 = c^2 \] + +My favorite Greek letter is $\xi$. I also like $\beta$, $\gamma$ and $\sigma$. +I haven't found a Greek letter yet that \LaTeX \hspace{1pt} doesn't know +about! \\ + +常用函数操作符同样很重要: +trigonometric functions ($\sin$, $\cos$, $\tan$), +logarithms 和 exponentials ($\log$, $\exp$), +limits ($\lim$), etc. +在 LaTeX 指令中预定义 +让我们写一个等式看看发生了什么: +$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$ \\ + +分数可以写成以下形式: + +% 10 / 7 +$$ ^{10}/_{7} $$ + +% 相对比较复杂的分数可以写成 +% \frac{numerator}{denominator} +$$ \frac{n!}{k!(n - k)!} $$ \\ + +我们同样可以插入公式(equations)在环境 ``equation environment'' 下。 + +% 展示数学相关时,使用方程式环境 +\begin{equation} % 进入 math-mode + c^2 = a^2 + b^2. + \label{eq:pythagoras} % 为了下一步引用 +\end{equation} % 所有 \begin 语句必须有end语句对应 + +引用我们的新等式! +Eqn.~\ref{eq:pythagoras} is also known as the Pythagoras Theorem which is also +the subject of Sec.~\ref{subsec:pythagoras}. A lot of things can be labeled: +figures, equations, sections, etc. + +求和(Summations)与整合(Integrals)写作 sum 和 int : + +% 一些编译器会提醒在等式环境中的空行 + +\begin{equation} + \sum_{i=0}^{5} f_{i} +\end{equation} +\begin{equation} + \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x +\end{equation} + +\section{Figures} + +让我们插入图片。图片的放置非常微妙。 +我在每次使用时都会查找可用选项。 + +\begin{figure}[H] % H 是放置选项的符号 + \centering % 图片在本页居中 + % 宽度放缩为页面的0.8倍 + %\includegraphics[width=0.8\linewidth]{right-triangle.png} + % 需要使用想象力决定是否语句超出编译预期 + \caption{Right triangle with sides $a$, $b$, $c$} + \label{fig:right-triangle} +\end{figure} + +\subsection{Table} +插入表格与插入图片方式相同 + +\begin{table}[H] + \caption{Caption for the Table.} + % 下方的 {} 描述了表格中每一行的绘制方式 + % 同样,我在每次使用时都会查找可用选项。 + \begin{tabular}{c|cc} + Number & Last Name & First Name \\ % 每一列被 & 分开 + \hline % 水平线 + 1 & Biggus & Dickus \\ + 2 & Monty & Python + \end{tabular} +\end{table} + +\section{Getting \LaTeX \hspace{1pt} to not compile something (i.e. Source Code)} +现在增加一些源代码在 \LaTeX \hspace{1pt} 文档中, +我们之后需要 \LaTeX \hspace{1pt} 不翻译这些内容而仅仅是把他们打印出来 +这里使用 verbatim environment。 + +% 也有其他库存在 (如. minty, lstlisting, 等) +% 但是 verbatim 是最基础和简单的一个 +\begin{verbatim} + print("Hello World!") + a%b; % 在这一环境下我们可以使用 % + random = 4; #decided by fair random dice roll +\end{verbatim} + +\section{Compiling} + +现在你大概想了解如何编译这个美妙的文档 +然后得到饱受称赞的 \LaTeX \hspace{1pt} pdf文档 +(这个文档确实被编译了)。 \\ +得到最终文档,使用 \LaTeX \hspace{1pt} 组合步骤: + \begin{enumerate} + \item Write the document in plain text (the ``source code''). + \item Compile source code to produce a pdf. + The compilation step looks like this (in Linux): \\ + \begin{verbatim} + > pdflatex learn-latex.tex + \end{verbatim} + \end{enumerate} + +许多 \LaTeX \hspace{1pt}编译器把步骤1和2在同一个软件中进行了整合 +所以你可以只看步骤1完全不看步骤2 +步骤2同样在以下情境中使用情景 \footnote{以防万一,当你使用引用时 + (如 Eqn.~\ref{eq:pythagoras}),你将需要多次运行步骤2 +来生成一个媒介文件 *.aux 。}. +% 同时这也是在文档中增加脚标的方式 + +在步骤1中,用普通文本写入格式化信息 +步骤2的编译阶段则注意在步骤1 中定义的格式信息。 + +\section{Hyperlinks} +同样可以在文档中加入超链接 +使用如下命令在序言中引入库: +\begin{verbatim} + \usepackage{hyperref} +\end{verbatim} + +有两种主要的超链接方式 \\ +\url{https://learnxinyminutes.com/docs/latex/}, 或 +\href{https://learnxinyminutes.com/docs/latex/}{shadowed by text} +% 你不可以增加特殊空格和符号,因为这将会造成编译错误 + +这个库同样在输出PDF文档时制造略缩的列表,或在目录中激活链接 + + +\section{End} + +这就是全部内容了! + +% 通常,你会希望文章中有个引用部分 +% 最简单的建立方式是使用书目提要章节 +\begin{thebibliography}{1} + % 与其他列表相同, \bibitem 命令被用来列出条目 + % 每个记录可以直接被文章主体引用 + \bibitem{latexwiki} The amazing \LaTeX \hspace{1pt} wikibook: {\em +https://en.wikibooks.org/wiki/LaTeX} + \bibitem{latextutorial} An actual tutorial: {\em http://www.latex-tutorial.com} +\end{thebibliography} + +% 结束文档 +\end{document} +``` + +## LaTeX 进阶 + +* The amazing LaTeX wikibook: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX) +* An actual tutorial: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/) -- cgit v1.2.3 From da31b7080cc15c50802cc70d8452ade219c9be20 Mon Sep 17 00:00:00 2001 From: Yunfeng Wang Date: Mon, 31 Oct 2016 19:55:48 +0800 Subject: fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `查询一个不存在地键将会返回nil` ->`查询一个不存在的键将会返回nil` --- zh-cn/ruby-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/ruby-cn.html.markdown b/zh-cn/ruby-cn.html.markdown index 2d181de0..657a913d 100644 --- a/zh-cn/ruby-cn.html.markdown +++ b/zh-cn/ruby-cn.html.markdown @@ -214,7 +214,7 @@ hash.keys #=> ['color', 'number'] hash['color'] #=> 'green' hash['number'] #=> 5 -# 查询一个不存在地键将会返回nil +# 查询一个不存在的键将会返回nil hash['nothing here'] #=> nil # 从Ruby 1.9开始,用符号作为键的时候有特别的记号表示: -- cgit v1.2.3 From be9f44cf10ad22547d5f86f69e6c992e3f241302 Mon Sep 17 00:00:00 2001 From: Jiang Haiyun Date: Tue, 13 Dec 2016 09:28:54 +0800 Subject: New translation: less-zh-cn (#2595) --- zh-cn/less-cn.html.markdown | 387 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 387 insertions(+) create mode 100644 zh-cn/less-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/less-cn.html.markdown b/zh-cn/less-cn.html.markdown new file mode 100644 index 00000000..365a0287 --- /dev/null +++ b/zh-cn/less-cn.html.markdown @@ -0,0 +1,387 @@ +--- +language: less +filename: learnless-cn.less +contributors: + - ["Saravanan Ganesh", "http://srrvnn.me"] +translators: + - ["Jiang Haiyun", "http://www.atjiang.com"] +lang: zh-cn +--- + + +Less是一种CSS预处理器,它增加了诸如变量、嵌套、mixin等功能。 +Less(以及其它预处理器,如[Sass](http://sass-lang.com/))能帮助开发人员编写易维护,DRY (Don't Repeat Yourself) 的代码。 + +```css + + +//单行注释在编译成CSS后会被删除。 + +/* 多行注释将保留. */ + + + +/* 变量 +==============================*/ + + +/* 你可以将一个CSS值(如一个颜色值)保存到变量中。 + 使用'@'符号来创建一个变量。*/ + +@primary-color: #a3a4ff; +@secondary-color: #51527f; +@body-font: 'Roboto', sans-serif; + +/* 你可以在你的样式文件中使用这些变量。 + 现在假如你想修改颜色,你只需修改一次即可。*/ + +body { + background-color: @primary-color; + color: @secondary-color; + font-family: @body-font; +} + +/* 以上将编译成: */ + +body { + background-color: #a3a4ff; + color: #51527F; + font-family: 'Roboto', sans-serif; +} + + +/* 相比于在你的样式文件中逐个修改,这种方式维护性更好。 */ + + + +/* Mixins +==============================*/ + + +/* 如果你要为多个元素编写同样的代码, + 你可能想实现轻松地重用。*/ + +.center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +/* 你只需简单地将选择子作为样式添加进来就能使用mixin了 */ + +div { + .center; + background-color: @primary-color; +} + +/* 它将编译成: */ + +.center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #a3a4ff; +} + +/* 通过在选择子后添加括号,可以使这些mixin代码不被编译 */ + +.center() { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +div { + .center; + background-color: @primary-color; +} + +/* 将编译成: */ +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #a3a4ff; +} + + + +/* 嵌套 +==============================*/ + + +/* Less允许你在选择子中嵌套选择子 */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: #f00; + } +} + +/* '&'将被替换成父选择子。*/ +/* 你也可以嵌套伪类。 */ +/* 注意过度嵌套将会导致代码难以维护。 + 最佳实践推荐在嵌套时不超过3层。 + 例如: */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: red; + + &:hover { + background-color: blue; + } + + a { + color: white; + } + } +} + +/* 编译成: */ + +ul { + list-style-type: none; + margin-top: 2em; +} + +ul li { + background-color: red; +} + +ul li:hover { + background-color: blue; +} + +ul li a { + color: white; +} + + + +/* 函数 +==============================*/ + + +/* Less提供的函数可以用来完成多种任务。 + 考虑以下情况: */ + +/* 函数可以通过其名称及传入其所需的参数来调用。 */ + +body { + width: round(10.25px); +} + +.header { + background-color: lighten(#000, 0.5); +} + +.footer { + background-color: fadeout(#000, 0.25) +} + +/* 编译成: */ + +body { + width: 10px; +} + +.header { + background-color: #010101; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* 你也可以定义自己的函数。函数非常类似于mixin。 + 当你在函数和mixin之间抉择时, + 记住mixin最适合用来创建CSS而函数更适合于 + 处理那些可能在你的Less代码中使用的逻辑。 + '数学运算符'部分的例子是转成可重用函数的最佳选择。*/ + +/* 该函数计算两数的平均值: */ + +.average(@x, @y) { + @average-result: ((@x + @y) / 2); +} + +div { + .average(16px, 50px); // "调用"mixin + padding: @average-result; // 使用它的"返回"值 +} + +/* 编译成: */ + +div { + padding: 33px; +} + + + +/* 扩展 (继承) +==============================*/ + + +/* 扩展是在选择子间共享属性的一种方法。 */ + +.display { + height: 50px; +} + +.display-success { + &:extend(.display); + border-color: #22df56; +} + +/* 编译成: */ +.display, +.display-success { + height: 50px; +} +.display-success { + border-color: #22df56; +} + +/* 扩展一条CSS语句优于创建一个mixin, + 这是由其组合所有共享相同基样式的类的方式决定的。 + 如果使用mixin完成,其属性将会在调用了该mixin的每条语句中重复。 + 虽然它不至会影响你的工作流,但它会在由Less编译器 + 生成的的文件中添加不必要的代码。*/ + + + +/* 片段与导入 +==============================*/ + + +/* Less允许你创建片段文件。它有助于你的Less代码保持模块化。 + 片段文件习惯上以'_'开头,例如 _reset.css,并被导入到 + 一个将会被编译成CSS的主less文件中。*/ + +/* 考虑以下的CSS,我们将把它们放入一个叫_reset.css的文件中 */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Less提供的@import能用来将片段导入到文件中。 + 它与传统的CSS @import语句不同,无需通过 + HTTP请求获取导入文件。Less提取导入文件 + 并将它们与编译后的代码结合起来。 */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* 编译成: */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + + +/* 数学运算符 +==============================*/ + + +/* Less提供以下的运算符: +, -, *, /, 和 %。 + 相比于使用你事先手工计算好了的数值,它们 + 对于直接在你的Less文件中计算数值很有用。 + 以下是设置一个两列设计的例子。*/ + +@content-area: 960px; +@main-content: 600px; +@sidebar-content: 300px; + +@main-size: @main-content / @content-area * 100%; +@sidebar-size: @sidebar-content / @content-area * 100%; +@gutter: 100% - (@main-size + @sidebar-size); + +body { + width: 100%; +} + +.main-content { + width: @main-size; +} + +.sidebar { + width: @sidebar-size; +} + +.gutter { + width: @gutter; +} + +/* 编译成: */ + +body { + width: 100%; +} + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + +.gutter { + width: 6.25%; +} + + +``` + +## 实践Less + +如果你想在你的浏览器中尝试LESS,参阅: +* [Codepen](http://codepen.io/) +* [LESS2CSS](http://lesscss.org/less-preview/) + +## 兼容性 + +Less可以用于任何项目中,只要你有程序能将它编译成CSS即可。你还需要验证你所使用的CSS是否与你的目标浏览器兼容。 + +[QuirksMode CSS](http://www.quirksmode.org/css/)和[CanIUse](http://caniuse.com) 对于检查兼容性来说都是不错的资源。 + +## 延伸阅读资料 +* [Official Documentation](http://lesscss.org/features/) +* [Less CSS - Beginner's Guide](http://www.hongkiat.com/blog/less-basic/) -- cgit v1.2.3 From a65150f706a599dcee2ed2e7107241e847cbaf2c Mon Sep 17 00:00:00 2001 From: Jiang Haiyun Date: Wed, 14 Dec 2016 10:24:49 +0800 Subject: [sass/zh-cn] new translation (#2597) --- zh-cn/sass-cn.html.markdown | 585 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 585 insertions(+) create mode 100644 zh-cn/sass-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/sass-cn.html.markdown b/zh-cn/sass-cn.html.markdown new file mode 100644 index 00000000..985c6470 --- /dev/null +++ b/zh-cn/sass-cn.html.markdown @@ -0,0 +1,585 @@ +--- +language: sass +filename: learnsass-cn.scss +contributors: + - ["Laura Kyle", "https://github.com/LauraNK"] + - ["Sean Corrales", "https://github.com/droidenator"] + - ["Kyle Mendes", "https://github.com/pink401k"] + - ["Keith Miyake", "https://github.com/kaymmm"] +translators: + - ["Jiang Haiyun", "http://www.atjiang.com"] +lang: zh-cn +--- + +Sass是一种CSS扩展语言,它增加了诸如变量、嵌套、mixin等功能。 +Sass(以及其它预处理器,如[Less](http://lesscss.org/)等) 能帮助开发人员编写易维护和 DRY (Don't Repeat Yourself)的代码。 + +Sass有两种不同的语法可选用。SCSS的语法和CSS的相同,但增加了Sass的额外功能。或者Sass(原来的语法),它使用缩进而非大括号和分号。 + +本教程使用SCSS编写。 + +如果你已熟悉CSS3,你可能相对能较快地掌握Sass。它并没有提供任何新的类型属性,而只是提供了一些工具使你能更高效的编写CSS,并且使维护更加容易。 + +```scss + + +// 单行注释当Sass被编译成CSS后会被删除。 + +/* 多行注释将保留. */ + +/* 变量 +============================== */ + + + +/* 你可以将一个CSS值(如一个颜色值)保存到变量中。 +使用'$'符号来创建一个变量。*/ + +$primary-color: #A3A4FF; +$secondary-color: #51527F; +$body-font: 'Roboto', sans-serif; + +/* 你可以在你的样式文件中使用变量。 + 现在假如你想修改颜色,你只需修改一次即可。*/ + +body { + background-color: $primary-color; + color: $secondary-color; + font-family: $body-font; +} + +/* 以上将编译成: */ +body { + background-color: #A3A4FF; + color: #51527F; + font-family: 'Roboto', sans-serif; +} + +/* 相比于在你的样式文件中逐个进行修改,这种方式维护性更好。 */ + + + +/* 控制指令 +============================== */ + +/* Sass允许你使用@if, @else, @for, @while, 和 @each 来控制 + 你的代码如何编译成CSS */ + +/* @if/@else块的行为和你可能预想的会完全相同 */ + +$debug: true !default; + +@mixin debugmode { + @if $debug { + @debug "Debug mode enabled"; + + display: inline-block; + } + @else { + display: none; + } +} + +.info { + @include debugmode; +} + +/* 如果$debug设置为了true, .info 将会显示; 如果设置为false那么 + .info 将不显示。 + +注意: @debug将在命令行中输出调试信息。 +在调试你的SCSS时它对于检查变量很有用。*/ + +.info { + display: inline-block; +} + +/* @for是控制循环,它能遍历区间值。 +它对于设置一组元素的类型特别有用。 +有两种形式,"through"和"to"。前者包括最末那个值, +而后者止于最末那个值。 +*/ + +@for $c from 1 to 4 { + div:nth-of-type(#{$c}) { + left: ($c - 1) * 900 / 3; + } +} + +@for $c from 1 through 3 { + .myclass-#{$c} { + color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3); + } +} + +/* 将编译成: */ + +div:nth-of-type(1) { + left: 0; +} + +div:nth-of-type(2) { + left: 300; +} + +div:nth-of-type(3) { + left: 600; +} + +.myclass-1 { + color: #555555; +} + +.myclass-2 { + color: #aaaaaa; +} + +.myclass-3 { + color: white; +// SASS automatically converts #FFFFFF to white +} + +/* @while也非常直白: */ + +$columns: 4; +$column-width: 80px; + +@while $columns > 0 { + .col-#{$columns} { + width: $column-width; + left: $column-width * ($columns - 1); + } + + $columns: $columns - 1; +} + +/* 将输出以下CSS: */ + +.col-4 { + width: 80px; + left: 240px; +} + +.col-3 { + width: 80px; + left: 160px; +} + +.col-2 { + width: 80px; + left: 80px; +} + +.col-1 { + width: 80px; + left: 0px; +} + +/* @each函数类似@for, 除了它使用一个列表而不是序列值 +注意: 你指定列表的方式和指定其它变量一样, +用空格作为分隔符。 */ + +$social-links: facebook twitter linkedin reddit; + +.social-links { + @each $sm in $social-links { + .icon-#{$sm} { + background-image: url("images/#{$sm}.png"); + } + } +} + +/* 将输出: */ + +.social-links .icon-facebook { + background-image: url("images/facebook.png"); +} + +.social-links .icon-twitter { + background-image: url("images/twitter.png"); +} + +.social-links .icon-linkedin { + background-image: url("images/linkedin.png"); +} + +.social-links .icon-reddit { + background-image: url("images/reddit.png"); +} + + +/* Mixins +==============================*/ + +/* 如果你发现你要为多个元素编写相同的代码, +你可能想将那些代码保存到一个mixin中。 + +使用'@mixin'指令,再为你的mixin加上一个名称。*/ + +@mixin center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +/* 你可以通过'@include'及mixin名来调用mixin。 */ + +div { + @include center; + background-color: $primary-color; +} + +/* 将编译成: */ +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #A3A4FF; +} + +/* 你可以使用mixin来创建一个快捷属性。*/ + +@mixin size($width, $height) { + width: $width; + height: $height; +} + +/* 你可以通过传入width和height参数来调用它。*/ + +.rectangle { + @include size(100px, 60px); +} + +.square { + @include size(40px, 40px); +} + +/* 编译成: */ +.rectangle { + width: 100px; + height: 60px; +} + +.square { + width: 40px; + height: 40px; +} + + + +/* 函数 +============================== */ + + + +/* Sass提供的函数可以用来完成各种各样的任务。 + 考虑以下情况 */ + +/* 函数可以通过其名称及传入其所需的参数来调用 */ +body { + width: round(10.25px); +} + +.footer { + background-color: fade_out(#000000, 0.25); +} + +/* 编译成: */ + +body { + width: 10px; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* 你也可以定义你自己的函数。函数非常类似于mixin。 + 当你在函数和mixin之间抉择时,记住mixin最适合于创建CSS而函数更适合于 + 处理那些可能在你的Sass代码中使用的逻辑。'数学运算符'部分的例子 + 是转成可重用函数的最理想选择。 */ + +/* 该函数将接收一个目标尺寸大小和父结点尺寸大小,然后计算并 + 返回百分数 */ + +@function calculate-percentage($target-size, $parent-size) { + @return $target-size / $parent-size * 100%; +} + +$main-content: calculate-percentage(600px, 960px); + +.main-content { + width: $main-content; +} + +.sidebar { + width: calculate-percentage(300px, 960px); +} + +/* 编译成: */ + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + + + +/* 扩展 (继承) +============================== */ + + + +/* 扩展是在选择子间共享属性的一种方法。 */ + +.display { + @include size(5em, 5em); + border: 5px solid $secondary-color; +} + +.display-success { + @extend .display; + border-color: #22df56; +} + +/* 编译成: */ +.display, .display-success { + width: 5em; + height: 5em; + border: 5px solid #51527F; +} + +.display-success { + border-color: #22df56; +} + +/* 扩展一条CSS语句优于创建一个mixin, + 这是由Sass组合所有共享相同基样式的类的方式决定的。 + 如果使用mixin完成,width, height, 和border将会在 + 调用了该mixin的每条语句中重复。虽然它不至于会影响你的工作流, + 但它会在由Sass编译器生成的的文件中添加不必要的代码。*/ + + +/* 嵌套 +============================== */ + + + +/* Sass允许在选择子中嵌套选择子 */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: #FF0000; + } +} + +/* '&'将被父选择子替换。*/ +/* 你也可以嵌套伪类。 */ +/* 注意过度嵌套将导致你的代码难以维护。 +最佳实践推荐在嵌套时不超过3层。 +例如: */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: red; + + &:hover { + background-color: blue; + } + + a { + color: white; + } + } +} + +/* 编译成: */ + +ul { + list-style-type: none; + margin-top: 2em; +} + +ul li { + background-color: red; +} + +ul li:hover { + background-color: blue; +} + +ul li a { + color: white; +} + + + +/* 片段与导入 +============================== */ + + + +/* Sass允许你创建片段文件。它有助于你的Sass代码保持模块化。 + 片段文件应该以 '_' 开头,例如 _reset.css。 + 片段不会输出到CSS中。*/ + +/* 考虑以下的CSS,我们会将它们放入一个叫作_reset.css的文件中 */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Sass提供的@import能用来将片段导入到文件中。 + 它与传统的CSS @import语句不同,不需要通过 + 另外的HTTP请求来获取导入的文件。 + Sass提取导入文件并将它与编译后的代码结合起来。 */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* 编译成: */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + + +/* 占位符选择子 +============================== */ + + + +/* 占位符在创建用于扩展的CSS语句时非常有用。 + 如果你想创建一条只通过@extend使用的CSS语句,你可以利用占位符来实现。 + 占位符以'%'而非'.'或'#'开头。占位符不会出现在编译后的CSS中 */ + +%content-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + @extend %content-window; + background-color: #0000ff; +} + +/* 编译成: */ + +.message-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + background-color: #0000ff; +} + + + +/* 数学运算 +============================== */ + + + +/* Sass提供以下的运算符: +, -, *, /, 和 %。它们 + 相比于使用你事先手工计算好了的数值,它们 + 对于直接在你的Sass文件中计算数值很有用。 + 以下是设置一个简单的两列设计的例子。*/ + +$content-area: 960px; +$main-content: 600px; +$sidebar-content: 300px; + +$main-size: $main-content / $content-area * 100%; +$sidebar-size: $sidebar-content / $content-area * 100%; +$gutter: 100% - ($main-size + $sidebar-size); + +body { + width: 100%; +} + +.main-content { + width: $main-size; +} + +.sidebar { + width: $sidebar-size; +} + +.gutter { + width: $gutter; +} + +/* 编译成: */ + +body { + width: 100%; +} + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + +.gutter { + width: 6.25%; +} + +``` + +## SASS还是Sass? +该语言的名字,“Sass”,是一个词,不是一个缩写。 +你有没想过Sass是否是一个缩写词?你可能没有,但我反正会告诉你。 +该语言的名字是一个单词,不是一个缩写词。 +由于人们老是将它写成"SASS",语言的作者开玩笑地称它为"Syntactically Awesome StyleSheets"。 + + +## 实践Sass +如果你想在你的浏览器中尝试Sass,参阅[SassMeister](http://sassmeister.com/)。 +你可以选用任一种语法,只需进到设置页然后选择Sass或SCSS。 + + +## 兼容性 +Sass可以用于任何项目中,只要你有程序能将它编译成CSS即可。你还需要验证你所使用的CSS是否与你的目标浏览器兼容。 + +[QuirksMode CSS](http://www.quirksmode.org/css/)和[CanIUse](http://caniuse.com)对于检查兼容性来说都是不错的资源。 + + +## 延伸阅读资料 +* [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html) +* [The Sass Way](http://thesassway.com/) 上提供了教程(初学者-高级)和文章。 -- cgit v1.2.3 From 66e541e9e55d89d705319c2cf0c01e6319be817b Mon Sep 17 00:00:00 2001 From: Yusheng Date: Wed, 14 Dec 2016 18:13:04 +0800 Subject: [r/zh-cn] fix a bug (#2598) * fix a bug * translate comment & fix another bug --- zh-cn/r-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/r-cn.html.markdown b/zh-cn/r-cn.html.markdown index 55a29b11..d576db29 100644 --- a/zh-cn/r-cn.html.markdown +++ b/zh-cn/r-cn.html.markdown @@ -303,7 +303,7 @@ if (4 > 3) { # 定义如下 jiggle <- function(x) { - x + rnorm(x, sd=.1) #add in a bit of (controlled) noise + x = x + rnorm(1, sd=.1) # 添加一点(正态)波动 return(x) } -- cgit v1.2.3 From 393bab2f6e5ebfb6e924929785e821ac6e602efa Mon Sep 17 00:00:00 2001 From: Jiang Haiyun Date: Fri, 23 Dec 2016 23:07:15 +0800 Subject: [AngularJS/zh-cn] new translation (#2607) * .. * [angularjs/zh-cn] new translation * Update angularjs-cn.html.markdown --- zh-cn/angularjs-cn.html.markdown | 710 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 710 insertions(+) create mode 100644 zh-cn/angularjs-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/angularjs-cn.html.markdown b/zh-cn/angularjs-cn.html.markdown new file mode 100644 index 00000000..418a817d --- /dev/null +++ b/zh-cn/angularjs-cn.html.markdown @@ -0,0 +1,710 @@ +--- +category: tool +tool: AngularJS +contributors: + - ["Walter Cordero", "http://waltercordero.com"] +filename: learnangular-cn.html +translators: + - ["Jiang Haiyun", "http://www.atjiang.com"] +lang: zh-cn +--- + +## AngularJS 教程。 + +AngularJS 1.0 版在 2012 年发布。 +Miško Hevery, 一位 Google 员工, 从 2009 年开始开发 AngularJS。 +结果发现这个想法很好,从而该项目现在也被 Google 官方所支持了。 + +AngularJS 是一个 JavaScript 框架。它可以通过一个 "script" 标签添加到一个 HTML 页面中。 +AngularJS 通过指令扩展了 HTML 属性,并且通过表达式将数据绑定到 HTML。 + +## 你应该已经了解了的知识 + +在学习 AngularJS 之前, 你应该对以下知识有了基本的了解: + +- HTML +- CSS +- JavaScript + +```html +// AngularJS 是一个 JavaScript 框架。它是一个用 JavaScript 写的库。 +// AngularJS 以一个 JavaScript 文件的形式发布,并且能通过一个 script 标签添加到一个网页中: +// + +/////////////////////////////////// +// AngularJS 扩展 HTML + +//AngularJS 通过 ng-directives 扩展 HTML。 +//ng-app 指令定义一个 AngularJS 应用。 +//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。 +//ng-bind 指令将应用的数据绑定到 HTML 视图上。 + + + + +
+

Name:

+

+
+ + + +/* + * 例子解析: + * AngularJS 在网页加载后自动开启。 + * ng-app 指令告诉 AngularJS:
元素是 AngularJS 应用的 "所有者"。 + * ng-model 指令将 input 输入框的值绑定到应用的 name 变量上。 + * ng-bind 指令将

元素的 innerHTML 绑定到应用的 name 变量上。 +*/ + 这里是要解析的内容 + +/////////////////////////////////// +// AngularJS 表达式 + +// AngularJS 表达式写在双括号内: {{ 表达式 }}。 +// AngularJS 表达式采用和 ng-bind 指令一样的方式将数据绑定到 HTML。 +// AngularJS 将在编写表达式的原样位置上 "输出" 数据。 +// AngularJS 表达式非常像 JavaScript 表达式:它们能包含文本,运算符和变量。 +// 例如 {{ 5 + 5 }} 或 {{ firstName + " " + lastName }} + + + + +

+

My first expression: {{ 5 + 5 }}

+
+ + + +//如果你删除了 ng-app 指令, HTML 将原样显示表达式,不对它进行解析: + + + + +
+

My first expression: {{ 5 + 5 }}

+
+ + + +// AngularJS 表达式采用和 ng-bind 指令一样的方式将 AngularJS 数据绑定到 HTML。 + + + + +
+

Name:

+

{{name}}

+
+ + + +// AngularJS 的数字类似 JavaScript 的数字: +
+

Total in dollar: {{ quantity * cost }}

+
+ +//AngularJS 的字符串类似 JavaScript 的字符串: +
+

The name is

+
+ +//AngularJS 的对象类似 JavaScript 的对象: +
+

The name is {{ person.lastName }}

+
+ +//AngularJS 的数组类似 JavaScript 的数组: +
+

The third result is {{ points[2] }}

+
+ +// 和 JavaScript 表达式一样, AngularJS 表达式能包含文本,运算符和变量。 +// 和 JavaScript 表达式不同, AngularJS 表达式能写在 HTML 内。 +// AngularJS 表达式不支持条件,循环和异常,而 JavaScript 表达式却支持。 +// AngularJS 表达式支持过滤器,而 JavaScript 表达式不支持。 + +/////////////////////////////////// +// AngularJS 指令 + + +//AngularJS 指令使用前缀 ng- 扩展 HTML 属性。 +//ng-app 指令初始化一个 AngularJS 应用。 +//ng-init 指令初始化应用的数据。 +//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。 +
+

Name:

+

You wrote: {{ firstName }}

+
+ +//使用 ng-init 并不常见。你将在有关控制器的章节中学习如何初始化数据。 + +//ng-repeat 指令会重复一个 HTML 元素: +
+
    +
  • + {{ x }} +
  • +
+
+ +//ng-repeat 指令用在一个对象数组上: +
+
    +
  • + {{ x.name + ', ' + x.country }} +
  • +
+
+ +// AngularJS 最适合用于数据库 CRUD (Create Read Update Delete) 的应用。 +// 只需设想这些对象都是来自一个数据库的记录。 + +// ng-app 指令定义一个 AngularJS 应用的根元素。 +// ng-app 指令将在页面加载后自动启动(自动初始化)应用。 +// 稍后你将学习如何为 ng-app 设置一个值(如 ng-app="myModule"), 来连接代码模块。 + +// ng-init 指令为一个 AngularJS 应用定义初始值。 +// 通常,你不太使用 ng-init。你会转而使用一个控制器或模块。 +// 你将在稍后学到更多有关控制器和模块的内容。 + +//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。 +//ng-model 指令还能: +//为应用的数据提供类型验证 (number, email, required)。 +//为应用的数据提供状态信息 (invalid, dirty, touched, error)。 +//为 HTML 元素提供 CSS 类。 +//将 HTML 元素绑定到 HTML 表单。 + +//ng-repeat 指令为集合(一个数组)中的每个元素克隆出 HTML 元素。 + +/////////////////////////////////// +// AngularJS 控制器 + +// AngularJS 控制器控制 AngularJS 应用中的数据。 +// AngularJS 控制器就是常规的 JavaScript 对象。 + +// AngularJS 应用由控制器控制。 +// ng-controller 指令定义应用的控制器。 +// 一个控制器就是一个 JavaScript 对象, 通过标准的 JavaScript 对象构建器创建。 + +
+ +First Name:
+Last Name:
+
+Full Name: {{firstName + " " + lastName}} + +
+ + + +//应用的解析: + +//AngularJS 应用通过 ng-app="myApp" 定义。该应用运行在
内。 +//ng-controller="myCtrl" 属性是一个 AngularJS 指令。它定义了一个控制器。 +//myCtrl 函数是一个 JavaScript 函数。 +//AngularJS 将使用一个 $scope 对象来调用控制器。 +//AngularJS 中, $scope 就是该应用对象(应用的变量和函数的所有者)。 +//该控制器在 $scope 内创建了两个属性(即变量 firstName 和 lastName)。 +//ng-model 指令将输入表单项绑定到控制器的属性上(firstName 和 lastName)。 + +//以上的例子演示了一个包含有两个属性 lastName 和 firstName 的控制器。 +//一个控制器也可以有方法(函数的变量): +
+ +First Name:
+Last Name:
+
+Full Name: {{fullName()}} + +
+ + + +//在较大型的应用中, 通常是将控制器代码保存在外部文件中。 +//只需将 标签之间的代码复制到一个名为 personController.js 的外部文件中: + +
+ +First Name:
+Last Name:
+
+Full Name: {{firstName + " " + lastName}} + +
+ + + +// 为方便下个例子使用,我们将创建一个新的控制器文件: +angular.module('myApp', []).controller('namesCtrl', function($scope) { + $scope.names = [ + {name:'Jani',country:'Norway'}, + {name:'Hege',country:'Sweden'}, + {name:'Kai',country:'Denmark'} + ]; +}); + +//将文件保存为 namesController.js: +//然后在一个应用中使用该控制器: + +
+ +
    +
  • + {{ x.name + ', ' + x.country }} +
  • +
+ +
+ + + +/////////////////////////////////// +// AngularJS 过滤器 + +// 过滤器可以通过一个管道符添加到表达式和指令上。 +// AngularJS 过滤器能用来转换数据: + +- **currency**: 将一个数字格式化成货币格式。 +- **filter**: 从一个数组中选择一组子集元素。 +- **lowercase**: 将一个字符串格式化成小写形式。 +- **orderBy**: 依据一个表达式排序一个数组。 +- **upper**: 将一个字符串格式化成大写形式。 + +//一个过滤器可以通过一个管道符 (|) 及一个过滤器表达式添加到一个表达式上。 +//(在下面的两个例子中,我们将使用前一章中的 person 控制器) +//uppercase 过滤器将字符串格式化成大写格式: +
+ +

The name is {{ lastName | uppercase }}

+ +
+ +//lowercase 过滤器将字符串格式化成小写格式: +
+ +

The name is {{ lastName | lowercase }}

+ +
+ +//currency 过滤器将一个数字格式化成货币格式: +
+ + + + +

Total = {{ (quantity * price) | currency }}

+ +
+ +//一个过滤器可以通过一个管道符 (|) 及一个过滤器表达式添加到一个指令上。 +//orderBy 过滤器根据一个表达式排序一个数组: +
+ +
    +
  • + {{ x.name + ', ' + x.country }} +
  • +
+ +
+ +//一个输入框过滤器可以通过一个管道符 (|) +//以及后跟一个冒号和模式名的 filter 添加到一个指令上。 +//该过滤器从一个数组中选择一个子集: + +
+ +

+ +
    +
  • + {{ (x.name | uppercase) + ', ' + x.country }} +
  • +
+ +
+ +/////////////////////////////////// +// AngularJS AJAX - $http + +//$http 是一个从远程服务器读取数据的 AngularJS 服务。 + +// 以下数据可由一个 web 服务器提供: +// http://www.w3schools.com/angular/customers.php +// **访问 URL 来查看数据格式** + +// AngularJS $http 是一个从 web 服务器上读取数据的核心服务。 +// $http.get(url) 这个函数用来读取服务器数据。 +
+ +
    +
  • + {{ x.Name + ', ' + x.Country }} +
  • +
+ +
+ + + +// 应用解析: + +// AngularJS 应用由 ng-app 定义。该应用运行在一个
中。 +// ng-controller 指令命名控制器对象。 +// customersCtrl 函数是一个标准的 JavaScript 对象构造器。 +// AngularJS 会使用一个 $scope 和 $http 对象来调用 customersCtrl。 +// $scope 就是该应用对象(应用的变量和函数的所有者)。 +// $http 是一个用于请求外部数据的 XMLHttpRequest 对象。 +// $http.get() 从 http://www.w3schools.com/angular/customers.php 读取 JSON 数据。 +// 如果成功, 该控制器会根据来自服务器的 JSON 数据,在 $scope 中创建一个属性 (names)。 + + +// 向不同的服务器(不同于请求页)请求数据,称作跨站 HTTP 请求。 +// 跨站请求在网站上很普遍。许多网页会从不同的服务器加载 CSS,图片和脚本。 +// 在现代浏览器中,基于安全原因,从脚本内进行跨站 HTTP 请求是被禁止的。 +// 下面的这行代码,已被加入到我们的 PHP 例子中,以便允许跨站访问。 +header("Access-Control-Allow-Origin: *"); + + +/////////////////////////////////// +// AngularJS 表格 + +// 使用 angular 显示表格非常简单: +
+ + + + + + +
{{ x.Name }}{{ x.Country }}
+ +
+ + + +// 要排序表格,添加一个 orderBy 过滤器: + + + + + +
{{ x.Name }}{{ x.Country }}
+ +// 要显示表格索引值,添加一个带有 $index 的 : + + + + + + +
{{ $index + 1 }}{{ x.Name }}{{ x.Country }}
+ +// 使用 $even 和 $odd + + + + + + + +
{{ x.Name }}{{ x.Name }}{{ x.Country }}{{ x.Country }}
+ +/////////////////////////////////// +// AngularJS HTML DOM + +//AngularJS 有用于将应用的数据绑定到 HTML DOM 元素属性的指令。 + +// ng-disabled 指令将 AngularJS 应用的数据绑定到 HTML 元素的 disabled 属性上。 + +
+ +

+ +

+ +

+Button +

+ +
+ +//应用解析: + +// ng-disabled 指令将应用的 mySwitch 数据绑定到 HTML 按钮的 disabled 属性上。 +// ng-model 指令将 HTML checkbox 元素的值绑定到 mySwitch 的值上。 +// 如果 mySwitch 的值求值为 true,则该按钮将被禁用: +

+ +

+ +// 如果 mySwitch 的值求值为 false,则该按钮将不会被禁用: +

+ +

+ +// ng-show 指令显示或隐藏一个 HTML 元素。 + +
+ +

I am visible.

+ +

I am not visible.

+ +
+ +// ng-show 指令基于 ng-show 的值显示(或隐藏)一个 HTML 元素。 +// 你可以使用任何能求值成 true 或 false 的表达式: +
+

I am visible.

+
+ +/////////////////////////////////// +// AngularJS 事件 + +// AngularJS 有它自己的 HTML 事件指令。 + +// ng-click 指令定义一个 AngularJS 点击事件。 +
+ + + +

{{ count }}

+ +
+ + +// ng-hide 指令可用于设置一个应用的部分区域的可见性。 +// 值 ng-hide="true" 使得一个 HTML 元素不可见。 +// 值 ng-hide="false" 使得一个 HTML 元素可见。 +
+ + + +

+First Name:
+Last Name:
+
+Full Name: {{firstName + " " + lastName}} +

+ +
+ + + +//应用解析: + +// personController 的第一部分和讲述控制器章节中的一样。 +// 该应用有一个默认属性(一个变量):$scope.myVar = false: +// ng-hide 指令依据 myVar 的值(true 或 false), +// 设置

元素的可见性,该元素含有两个输入框。 +// 函数 toggle() 将 myVar 在 true 和 false 间进行切换。 +// 值 ng-hide="true" 使得该元素不可见。 + + +// ng-show 指令也能用来设置一个应用的某部分的可见性。 +// 值 ng-show="false" 使得一个 HTML 元素不可见。 +// 值 ng-show="true" 使得一个 HTML 元素可见。 +// 这个例子与上面的一样,但用 ng-show 替代了 ng-hide: +

+ + + +

+First Name:
+Last Name:
+
+Full Name: {{firstName + " " + lastName}} +

+ +
+ + + +/////////////////////////////////// +// AngularJS 模块 + +// 一个 AngularJS 模块定义一个应用。 +// 模块是一个应用的不同部分所在的一个容器。 +// 模块是应用控制器的一个容器。 +// 控制器总是隶属于一个模块。 + +// 这个应用 ("myApp") 有一个控制器 ("myCtrl"): + + + + + + +
+{{ firstName + " " + lastName }} +
+ + + + + + +// 在 AngularJS 应用中通常将模块和控制器放置在 JavaScript 文件中。 +// 在本例中,"myApp.js" 包含了一个应用模块的定义,而 "myCtrl.js" 包含了控制器: + + + + + + +
+{{ firstName + " " + lastName }} +
+ + + + + + + +//myApp.js +var app = angular.module("myApp", []); + +// 模块定义中的 [] 参数可用来定义依赖的模块。 + +// myCtrl.js +app.controller("myCtrl", function($scope) { + $scope.firstName = "John"; + $scope.lastName= "Doe"; +}); + +// JavaScript 中应该避免使用全局函数。它们会非常容易地被覆盖 +// 或被其它脚本破坏。 + +// AngularJS 脚本通过将所有函数保存在模块内,缓解了这种问题。 + +// 虽然 HTML 应用中通常是将脚本放置在 +// 元素的末尾,但还是推荐你要么在 +// 中要么在 的开头处加载 AngularJS 库。 + +// 这是因为对 angular.module 的调用只有在库被加载后才能被编译。 + + + + + + +
+{{ firstName + " " + lastName }} +
+ + + + + + + +/////////////////////////////////// +// AngularJS 应用 + +// AngularJS 模块定义 AngularJS 应用。 +// AngularJS 控制器控制 AngularJS 应用。 +// ng-app 指令定义该应用,ng-controller 定义该控制器。 +
+ First Name:
+ Last Name:
+
+ Full Name: {{firstName + " " + lastName}} +
+ + +// AngularJS 模块定义应用: +var app = angular.module('myApp', []); + +// AngularJS 控制器控制应用: +app.controller('myCtrl', function($scope) { + $scope.firstName= "John"; + $scope.lastName= "Doe"; +}); +``` + +## 来源 & 参考 + +**例子** + +- http://www.w3schools.com/angular/angular_examples.asp + +**参考** + +- http://www.w3schools.com/angular/angular_ref_directives.asp +- http://www.w3schools.com/angular/default.asp -- cgit v1.2.3 From ae16d450781aecd9ff4423deb2cf67317d309080 Mon Sep 17 00:00:00 2001 From: omgee Date: Tue, 3 Jan 2017 09:41:35 +0200 Subject: [c++/en,es,it,br,ru,ch] Fix forgotten namespace std:: (#2619) --- zh-cn/c++-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/c++-cn.html.markdown b/zh-cn/c++-cn.html.markdown index d71aa8d6..87951bc3 100644 --- a/zh-cn/c++-cn.html.markdown +++ b/zh-cn/c++-cn.html.markdown @@ -315,7 +315,7 @@ void Dog::print() const void Dog::~Dog() { - cout << "Goodbye " << name << "\n"; + std::cout << "Goodbye " << name << "\n"; } int main() { -- cgit v1.2.3 From 7afc3bc59ea6f9f3dfdcdc1bfc65aeb0b9fb67a7 Mon Sep 17 00:00:00 2001 From: c4605 Date: Thu, 9 Feb 2017 23:35:04 +0800 Subject: =?UTF-8?q?[zh-cn/common-lisp]=20when=20describe=20return=20values?= =?UTF-8?q?=20of=20gethash,=20=E4=B8=A4=E4=B8=AA=E5=80=BC=E6=98=AFt=20->?= =?UTF-8?q?=20=E7=AC=AC=E4=BA=8C=E4=B8=AA=E5=80=BC=E6=98=AFt=20(#2614)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zh-cn/common-lisp-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/common-lisp-cn.html.markdown b/zh-cn/common-lisp-cn.html.markdown index c7fe7e2c..b8979ed0 100644 --- a/zh-cn/common-lisp-cn.html.markdown +++ b/zh-cn/common-lisp-cn.html.markdown @@ -289,7 +289,7 @@ nil ; 逻辑假,或者空列表 ;; (通过键)检索对应的值 (gethash 'a *m*) ; => 1, t -;; 注意此处有一细节:Common Lisp往往返回多个值。`gethash`返回的两个值是t,代表找到了这个元素;返回nil表示没有找到这个元素。 +;; 注意此处有一细节:Common Lisp往往返回多个值。`gethash`返回的第二个值是t,代表找到了这个元素;返回nil表示没有找到这个元素。 ;;(译者注:返回的第一个值表示给定的键所对应的值或者nil;) ;;(第二个是一个布尔值,表示在哈希表中是否存在这个给定的键) ;; 例如,如果可以找到给定的键所对应的值,则返回一个t,否则返回nil -- cgit v1.2.3 From dc34cd25a9308435bae75099dbd8200c82efdc88 Mon Sep 17 00:00:00 2001 From: EtaoinWu Date: Tue, 21 Feb 2017 17:07:10 +0800 Subject: [dynamic-programming/cn]Add Translation (#2663) * [dynamic-programming/cn]Add Translation * Update dynamic-programming-cn.html.markdown --- zh-cn/dynamic-programming-cn.html.markdown | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 zh-cn/dynamic-programming-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/dynamic-programming-cn.html.markdown b/zh-cn/dynamic-programming-cn.html.markdown new file mode 100644 index 00000000..b75c5404 --- /dev/null +++ b/zh-cn/dynamic-programming-cn.html.markdown @@ -0,0 +1,57 @@ +--- +category: Algorithms & Data Structures +name: Dynamic Programming +contributors: + - ["Akashdeep Goel", "http://github.com/akashdeepgoel"] +filename: dynamic-programming-cn.html.markdown +lang: zh-cn +translators: + - ["EtaoinWu", "https://github.com/EtaoinWu"] +--- + +# 动态规划 + +## 简介 + +动态规划是一种实用的技巧,它可以用来解决一系列特定问题。它的思路很简单,如果你对某个给定的输入解决了一个问题,那么你可以保存已有信息,以避免重复计算,节约计算时间。 + +记住,只有那些没有办法记住历史的才被迫做更多的苦力。(Fibonacci就是一个显然的例子) + +## 解决问题的方式 + +1. *自顶向下* : 利用分支策略分解问题。如果你已经解决过当前子问题了,那么就返回已有信息。如果当前子问题没有计算过,那么就对它进行计算。这样的方法很易于思考、很直观。这被称作“记忆化”。 + +2. *自底向上* : 首先分析问题,将问题分解为不同规模的问题,并决定它们的顺序,按顺序计算,直到解决给定规模的问题。这样的流程可以保证在解决较大的问题之前解决(它所依赖的)较小的问题。这种流程被称作“动态规划”。 + +## 动态规划的例子 + +最长上升子序列问题。给定`S= {a[1] , a[2] , a[3], a[4], ............., a[n-1], a[n] }`,求出一个子序列,使得对于所有在这个子序列中所有满足`j a[j] and dp[i] Date: Fri, 24 Mar 2017 14:53:41 -0300 Subject: [kotlin/all] Rename variable fooMutableDate to fooMutableData (#2688) (#2689) --- zh-cn/kotlin-cn.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown index 1fd12f5b..eb69156b 100644 --- a/zh-cn/kotlin-cn.html.markdown +++ b/zh-cn/kotlin-cn.html.markdown @@ -178,13 +178,13 @@ fun helloWorld(val name : String) { // "with"函数类似于JavaScript中的"with"用法。 data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) - val fooMutableDate = MutableDataClassExample(7, 4, 9) - with (fooMutableDate) { + val fooMutableData = MutableDataClassExample(7, 4, 9) + with (fooMutableData) { x -= 2 y += 2 z-- } - println(fooMutableDate) // => MutableDataClassExample(x=5, y=6, z=8) + println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8) /* 我们可以使用"listOf"函数来创建一个list。 -- cgit v1.2.3 From 63a6eb1182205389388e13b680962199ac9b3ffb Mon Sep 17 00:00:00 2001 From: Andy Date: Sat, 1 Apr 2017 22:19:58 +0200 Subject: [haskell] Updating second headline (#2695) * Haskell: Updating second headline * Haskell: Updating second headline (es) * Haskell: Updating second headline (fr) * Haskell: Updating second headline (pl) * Haskell: Updating second headline (pt) * Haskell: Updating second headline (ro) * Haskell: Updating second headline (ru) * Haskell: Updating second headline (cn) --- zh-cn/haskell-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/haskell-cn.html.markdown b/zh-cn/haskell-cn.html.markdown index b0b1183f..c854169e 100644 --- a/zh-cn/haskell-cn.html.markdown +++ b/zh-cn/haskell-cn.html.markdown @@ -66,7 +66,7 @@ not False -- True ---------------------------------------------------- --- 列表和元组 +-- 2. 列表和元组 ---------------------------------------------------- -- 一个列表中的每一个元素都必须是相同的类型。 -- cgit v1.2.3 From 67953779e8d845aae0ff4910e8e50cc9f2676f56 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 18 Apr 2017 17:44:22 +0800 Subject: update zh-cn version to latest (#2709) as shown --- zh-cn/json-cn.html.markdown | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/json-cn.html.markdown b/zh-cn/json-cn.html.markdown index 3a8db2cf..73d3eb57 100644 --- a/zh-cn/json-cn.html.markdown +++ b/zh-cn/json-cn.html.markdown @@ -8,12 +8,21 @@ filename: learnjson-cn.json lang: zh-cn --- -因为JSON是一个极其简单的数据交换形式,这个最有可能将会是曾经最简单 -的Learn X in Y Minutes。 +因为JSON是一个极其简单的数据交换格式,本教程最有可能成为有史以来最简单的 +Learn X in Y Minutes。 -最纯正形式的JSON没有实际的注解,但是大多数解析器将会 -接受C-风格(//, /\* \*/)的注解。为了这个目的,但是, -一切都将会是100%有效的JSON。幸亏,它是不言自明的。 +纯正的JSON实际上没有注释,但是大多数解析器都 +接受C-风格(//, /\* \*/)的注释。为了兼容性,最好不要在其中写这样形式的注释。 + +因此,本教程的一切都会是100%有效的JSON。幸亏,它的表达能力很丰富。 + +支持的数据类型: + +- 字符串: "hello", "\"A quote.\"", "\u0abe", "Newline.\n" +- 数字: 23, 0.11, 12e10, 3.141e-10, 1.23e+4 +- 对象: { "key": "value" } +- 数组: ["Values"] +- 其他: true, false, null ```json { -- cgit v1.2.3 From bec5b2b879c39e82c979a30a46f9ba5f64edfa8c Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Thu, 4 May 2017 12:47:32 -0500 Subject: [go/zh-cn] Update content from English fork (#2722) * A manual diff with latest English version * Translation for updated parts * Translate string literals --- zh-cn/go-cn.html.markdown | 194 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 36 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/go-cn.html.markdown b/zh-cn/go-cn.html.markdown index fa4540a2..75498367 100644 --- a/zh-cn/go-cn.html.markdown +++ b/zh-cn/go-cn.html.markdown @@ -6,7 +6,8 @@ contributors: - ["Sonia Keys", "https://github.com/soniakeys"] - ["pantaovay", "https://github.com/pantaovay"] - ["lidashuang", "https://github.com/lidashuang"] - + - ["Tim Zhang", "https://github.com/ttimasdf"] + --- 发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流,但它却提供了解决现实问题的最新最快的方法。 @@ -27,7 +28,10 @@ package main // Import语句声明了当前文件引用的包。 import ( "fmt" // Go语言标准库中的包 + "io/ioutil" // 包含一些输入输出函数 + m "math" // 数学标准库,在此文件中别名为m "net/http" // 一个web服务器包 + "os" // 系统底层函数,如文件读写 "strconv" // 字符串转换 ) @@ -36,7 +40,7 @@ import ( func main() { // 往标准输出打印一行。 // 用包名fmt限制打印函数。 - fmt.Println("Hello world!") + fmt.Println("天坑欢迎你!") // 调用当前包的另一个函数。 beyondHello() @@ -54,7 +58,11 @@ func beyondHello() { learnTypes() // 少于y分钟,学的更多! } -// 多变量和多返回值的函数 +/* <- 快看快看我是跨行注释_(:з」∠)_ +Go语言的函数可以有多个参数和 *多个* 返回值。 +在这个函数中, `x`、`y` 是参数, +`sum`、`prod` 是返回值的标识符(可以理解为名字)且类型为int +*/ func learnMultiple(x, y int) (sum, prod int) { return x + y, x * y // 返回两个值 } @@ -62,11 +70,11 @@ func learnMultiple(x, y int) (sum, prod int) { // 内置变量类型和关键词 func learnTypes() { // 短声明给你所想。 - s := "Learn Go!" // String类型 + str := "少说话多读书!" // String类型 + + s2 := `这是一个 +可以换行的字符串` // 同样是String类型 - s2 := `A "raw" string literal -can include line breaks.` // 同样是String类型 - // 非ascii字符。Go使用UTF-8编码。 g := 'Σ' // rune类型,int32的别名,使用UTF-8编码 @@ -80,16 +88,29 @@ can include line breaks.` // 同样是String类型 // 字符转换 n := byte('\n') // byte是uint8的别名 - // 数组类型编译的时候大小固定。 + // 数组(Array)类型的大小在编译时即确定 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") // 类型转换的语法 + // Array和slice各有所长,但是slice可以动态的增删,所以更多时候还是使用slice。 + s3 := []int{4, 5, 9} // 回去看看 a3 ,是不是这里没有省略号? + s4 := make([]int, 4) // 分配4个int大小的内存并初始化为0 + var d2 [][]float64 // 这里只是声明,并未分配内存空间 + bs := []byte("a slice") // 进行类型转换 + + // 切片(Slice)的大小是动态的,它的长度可以按需增长 + // 用内置函数 append() 向切片末尾添加元素 + // 要增添到的目标是 append 函数第一个参数, + // 多数时候数组在原内存处顺次增长,如 + s := []int{1, 2, 3} // 这是个长度3的slice + s = append(s, 4, 5, 6) // 再加仨元素,长度变为6了 + fmt.Println(s) // 更新后的数组是 [1 2 3 4 5 6] + + // 除了向append()提供一组原子元素(写死在代码里的)以外,我们 + // 还可以用如下方法传递一个slice常量或变量,并在后面加上省略号, + // 用以表示我们将引用一个slice、解包其中的元素并将其添加到s数组末尾。 + s = append(s, []int{7, 8, 9}...) // 第二个参数是一个slice常量 + fmt.Println(s) // 更新后的数组是 [1 2 3 4 5 6 7 8 9] p, q := learnMemory() // 声明p,q为int型变量的指针 fmt.Println(*p, *q) // * 取值 @@ -100,13 +121,28 @@ can include line breaks.` // 同样是String类型 // 在Go语言中未使用的变量在编译的时候会报错,而不是warning。 // 下划线 _ 可以使你“使用”一个变量,但是丢弃它的值。 - _,_,_,_,_,_,_,_,_ = s2, g, f, u, pi, n, a3, s4, bs + _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs + // 通常的用法是,在调用拥有多个返回值的函数时, + // 用下划线抛弃其中的一个参数。下面的例子就是一个脏套路, + // 调用os.Create并用下划线变量扔掉它的错误代码。 + // 因为我们觉得这个文件一定会成功创建。 + file, _ := os.Create("output.txt") + fmt.Fprint(file, "这句代码还示范了如何写入文件呢") + file.Close() + // 输出变量 fmt.Println(s, c, a4, s3, d2, m) - learnFlowControl() // 回到流程控制 + learnFlowControl() // 回到流程控制 } +// 和其他编程语言不同的是,go支持有名称的变量返回值。 +// 声明返回值时带上一个名字允许我们在函数内的不同位置 +// 只用写return一个词就能将函数内指定名称的变量返回 +func learnNamedReturns(x, y int) (z int) { + z = x * y + return // z is implicit here, because we named it earlier. + // Go全面支持垃圾回收。Go有指针,但是不支持指针运算。 // 你会因为空指针而犯错,但是不会因为增加指针而犯错。 func learnMemory() (p, q *int) { @@ -126,7 +162,7 @@ func expensiveComputation() int { func learnFlowControl() { // If需要花括号,括号就免了 if true { - fmt.Println("told ya") + fmt.Println("这句话肯定被执行") } // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了, // 也不用容忍别人的代码风格。 @@ -146,15 +182,28 @@ func learnFlowControl() { } // 和if一样,for也不用括号 for x := 0; x < 3; x++ { // ++ 自增 - fmt.Println("iteration", x) + fmt.Println("遍历", x) } // x在这里还是1。为什么? // for 是go里唯一的循环关键字,不过它有很多变种 for { // 死循环 - break // 骗你的 + break // 骗你的 continue // 不会运行的 } + + // 用range可以枚举 array、slice、string、map、channel等不同类型 + // 对于channel,range返回一个值, + // array、slice、string、map等其他类型返回一对儿 + for key, value := range map[string]int{"one": 1, "two": 2, "three": 3} { + // 打印map中的每一个键值对 + fmt.Printf("索引:%s, 值为:%d\n", key, value) + } + // 如果你只想要值,那就用前面讲的下划线扔掉没用的 + for _, name := range []string{"Bob", "Bill", "Joe"} { + fmt.Printf("你是。。 %s\n", name) + } + // 和for一样,if中的:=先给y赋值,然后再和x作比较。 if y := expensiveComputation(); y > x { x = y @@ -163,17 +212,55 @@ func learnFlowControl() { xBig := func() bool { return x > 100 // x是上面声明的变量引用 } - fmt.Println("xBig:", xBig()) // true (上面把y赋给x了) + fmt.Println("xBig:", xBig()) // true (上面把y赋给x了) x /= 1e5 // x变成10 fmt.Println("xBig:", xBig()) // 现在是false + // 除此之外,函数体可以在其他函数中定义并调用, + // 满足下列条件时,也可以作为参数传递给其他函数: + // a) 定义的函数被立即调用 + // b) 函数返回值符合调用者对类型的要求 + fmt.Println("两数相加乘二: ", + func(a, b int) int { + return (a + b) * 2 + }(10, 2)) // Called with args 10 and 2 + // => Add + double two numbers: 24 + // 当你需要goto的时候,你会爱死它的! goto love love: + learnFunctionFactory() // 返回函数的函数多棒啊 + learnDefer() // 对defer关键字的简单介绍 learnInterfaces() // 好东西来了! } +func learnFunctionFactory() { + // 空行分割的两个写法是相同的,不过第二个写法比较实用 + fmt.Println(sentenceFactory("原谅")("当然选择", "她!")) + + d := sentenceFactory("原谅") + fmt.Println(d("当然选择", "她!")) + fmt.Println(d("你怎么可以", "她?")) +} + +// Decorator在一些语言中很常见,在go语言中, +// 接受参数作为其定义的一部分的函数是修饰符的替代品 +func sentenceFactory(mystring string) func(before, after string) string { + return func(before, after string) string { + return fmt.Sprintf("%s %s %s", before, mystring, after) // new string + } +} + +func learnDefer() (ok bool) { + // defer表达式在函数返回的前一刻执行 + defer fmt.Println("defer表达式执行顺序为后进先出(LIFO)") + defer fmt.Println("\n这句话比上句话先输出,因为") + // 关于defer的用法,例如用defer关闭一个文件, + // 就可以让关闭操作与打开操作的代码更近一些 + return true +} + // 定义Stringer为一个接口类型,有一个方法String type Stringer interface { String() string @@ -194,9 +281,9 @@ func (p pair) String() string { // p被叫做“接收器” func learnInterfaces() { // 花括号用来定义结构体变量,:=在这里将一个结构体变量赋值给p。 p := pair{3, 4} - fmt.Println(p.String()) // 调用pair类型p的String方法 - var i Stringer // 声明i为Stringer接口类型 - i = p // 有效!因为p实现了Stringer接口(类似java中的塑型) + fmt.Println(p.String()) // 调用pair类型p的String方法 + var i Stringer // 声明i为Stringer接口类型 + i = p // 有效!因为p实现了Stringer接口(类似java中的塑型) // 调用i的String方法,输出和上面一样 fmt.Println(i.String()) @@ -205,14 +292,28 @@ func learnInterfaces() { fmt.Println(p) // 输出和上面一样,自动调用String函数。 fmt.Println(i) // 输出和上面一样。 + learnVariadicParams("great", "learning", "here!") +} + +// 有变长参数列表的函数 +func learnVariadicParams(myStrings ...interface{}) { + // 枚举变长参数列表的每个参数值 + // 下划线在这里用来抛弃枚举时返回的数组索引值 + for _, param := range myStrings { + fmt.Println("param:", param) + } + + // 将可变参数列表作为其他函数的参数列表 + fmt.Println("params:", fmt.Sprintln(myStrings...)) + learnErrorHandling() } func learnErrorHandling() { - // ", ok"用来判断有没有正常工作 + // ", ok"用来判断有没有正常工作 m := map[int]string{3: "three", 4: "four"} if x, ok := m[1]; !ok { // ok 为false,因为m中没有1 - fmt.Println("no one there") + fmt.Println("别找了真没有") } else { fmt.Print(x) // 如果x在map中的话,x就是那个值喽。 } @@ -245,17 +346,17 @@ func learnConcurrency() { cs := make(chan string) // 操作string的channel cc := make(chan chan string) // 操作channel的channel - go func() { c <- 84 }() // 开始一个goroutine来发送一个新的数字 + 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) + fmt.Println("这是……", i) case <-cs: // 或者直接丢弃 - fmt.Println("it's a string") - case <-cc: // 空的,还没作好通讯的准备 - fmt.Println("didn't happen.") + fmt.Println("这是个字符串!") + case <-cc: // 空的,还没作好通讯的准备 + fmt.Println("别瞎想") } // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个一直阻塞。 @@ -265,22 +366,43 @@ func learnConcurrency() { // http包中的一个简单的函数就可以开启web服务器。 func learnWebProgramming() { // ListenAndServe第一个参数指定了监听端口,第二个参数是一个接口,特定是http.Handler。 - err := http.ListenAndServe(":8080", pair{}) - fmt.Println(err) // 不要无视错误。 + go func() { + err := http.ListenAndServe(":8080", pair{}) + fmt.Println(err) // 不要无视错误。 + }() + + requestServer() } // 使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("Y分钟golang速成!")) +} + +func requestServer() { + resp, err := http.Get("http://localhost:8080") + fmt.Println(err) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + fmt.Printf("\n服务器消息: `%s`", string(body)) } ``` ## 更进一步 -Go的根源在[Go官方网站](http://golang.org/)。 -在那里你可以学习入门教程,通过浏览器交互式地学习,而且可以读到很多东西。 +关于Go的一切你都可以在[Go官方网站](http://golang.org/)找到。 +在那里你可以获得教程参考,在线试用,和更多的资料。 +在简单的尝试过后,在[官方文档](https://golang.org/doc/)那里你会得到你所需要的所有资料、关于编写代码的规范、库和命令行工具的文档与Go的版本历史。 -强烈推荐阅读语言定义部分,很简单而且很简洁!(as language definitions go these days.) +强烈推荐阅读语言定义部分,很简单而且很简洁!(赶时髦!) + +你还可以前往[Go在线体验中心](https://play.golang.org/p/tnWMjr16Mm)进,在浏览器里修改并运行这些代码,一定要试一试哦!你可以将[https://play.golang.org](https://play.golang.org)当作一个[REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop),在那里体验语言特性或运行自己的代码,连环境都不用配! 学习Go还要阅读Go[标准库的源代码](http://golang.org/src/),全部文档化了,可读性非常好,可以学到go,go style和go idioms。在[文档](http://golang.org/pkg/)中点击函数名,源代码就出来了! + +[Go by example](https://gobyexample.com/)也是一个学习的好地方。 + + + +Go Mobile添加了对移动平台的支持(Android and iOS)。你可以完全用go语言来创造一个app或编写一个可以从Java或Obj-C调用的函数库,敬请参考[Go Mobile page](https://github.com/golang/go/wiki/Mobile)。 -- cgit v1.2.3 From 6a1ce1d98aa50a29415959302e6a9c3521d8c7d2 Mon Sep 17 00:00:00 2001 From: zxyqwe Date: Wed, 17 May 2017 23:16:31 +0800 Subject: add zh-cn for html (#2725) --- zh-cn/html-cn.html.markdown | 121 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 zh-cn/html-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/html-cn.html.markdown b/zh-cn/html-cn.html.markdown new file mode 100644 index 00000000..6f60d5ce --- /dev/null +++ b/zh-cn/html-cn.html.markdown @@ -0,0 +1,121 @@ +--- +language: html +filename: learnhtml-cn.html +contributors: + - ["Christophe THOMAS", "https://github.com/WinChris"] +translators: + - ["zxyqwe", "https://github.com/zxyqwe"] +lang: zh-cn +--- + +HTML是超文本标记语言的缩写。 +这门语言可以让我们为万维网创造页面。 +这是一门标记语言,它允许我们用代码来指示网页上文字和数据应该如何显示。 +实际上html文件是简单的文本文件。 +什么是标记?标记是通过使用开始和结束标签包围数据的方法,来组织管理页面上的数据。 +这些标记对它们环绕的文本有重要的意义。 +和其它计算机语言意义,HTML有很多版本。这里我们将讨论HTML5。 + +**注意:** 你可以在类似[codepen](http://codepen.io/pen/)的网站上的教程中,尝试不同的标签和元素带来的效果,理解它们如何起效,并且逐渐熟悉这门语言。 +本文主要关注HTML的语法和一些有用的小窍门。 + + +```html + + + + + + + + + + 我的网站 + + +

Hello, world!

+ 来看看这里有什么 +

这是一个段落。

+

这是另外一个段落。

+
    +
  • 这是一个非计数列表的一项(项目符合列表)
  • +
  • 这是另一项
  • +
  • 这是列表中的最后一项
  • +
+ + + + + + + + + + + + + + + + + + + + + 我的网站 + + + + + + + +

Hello, world!

+ + 来看看这里有什么 +

这是一个段落。

+

这是另外一个段落。

+
    + +
  • 这是一个非计数列表的一项(项目符合列表)
  • +
  • 这是另一项
  • +
  • 这是列表中的最后一项
  • +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
第一个表头 第二个表头
第一行第一列 第一行第二列
第二行第一列第二行第二列
+ +``` + +## 使用 + +HTML文件使用`.html`后缀。 + +## 扩展阅读 + +* [维基百科](https://en.wikipedia.org/wiki/HTML) +* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML) +* [W3School](http://www.w3schools.com/html/html_intro.asp) -- cgit v1.2.3 From b2cb3528857b673a8c9921bae8f5f6c9cb6efd69 Mon Sep 17 00:00:00 2001 From: zhysan Date: Thu, 18 May 2017 15:56:40 +0800 Subject: Update vim-cn.html.markdown (#2726) fix description error of "w", "b", "e" --- zh-cn/vim-cn.html.markdown | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/vim-cn.html.markdown b/zh-cn/vim-cn.html.markdown index 5298351a..569ad1bf 100644 --- a/zh-cn/vim-cn.html.markdown +++ b/zh-cn/vim-cn.html.markdown @@ -56,14 +56,16 @@ lang: zh-cn t<字符> # 向前跳移到 <字符> 的左侧 # 例如, - f< # 向前跣到 < 上 +    f<               # 向前跳移到 < 上 t< # 向前跳移到 < 的左侧 # 按词移动 + # 默认一个单词由字母,数字和下划线组成 - w # 前移一个词 - b # 后移一个词 - e # 移到当前词的末尾 +    w               # 移动到下一个词首 +    b               # 移动到前一个词首 +    e               # 移动到下一个词尾 + # 移动的其它命令 -- cgit v1.2.3 From 18aea748812d857e0b840fdb885e876f5d06dc30 Mon Sep 17 00:00:00 2001 From: zxyqwe Date: Thu, 18 May 2017 21:31:22 +0800 Subject: [jquery/zh-cn]add zh-cn for jquery (#2728) * add zh-cn header for jquery * add zh-cn desc for jquery * add zh-cn detail for jquery --- zh-cn/jquery-cn.html.markdown | 131 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 zh-cn/jquery-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/jquery-cn.html.markdown b/zh-cn/jquery-cn.html.markdown new file mode 100644 index 00000000..4b23274e --- /dev/null +++ b/zh-cn/jquery-cn.html.markdown @@ -0,0 +1,131 @@ +--- +category: tool +tool: jquery +contributors: + - ["Sawyer Charles", "https://github.com/xssc"] +translators: + - ["zxyqwe", "https://github.com/zxyqwe"] +lang: zh-cn +filename: jquery-cn.js +--- + +jQuery是JavaScript的一个函数库,它可以帮你“写更少,做更多”。它集成了很多常见的JavaScript任务并且很容易调用。jQuery被世界各地的很多的大公司和开发者使用。它包括了AJAX,事件处理,文档操作以及很多其它功能,并且更加简单和快速。 + +正因为jQuery是JavaScript的一个函数库,所以你需要[首先学习JavaScript](https://learnxinyminutes.com/docs/javascript/) + +```js + + +/////////////////////////////////// +// 1. 选择器 + +// jQuery中的选择器被用来选择一个元素 +var page = $(window); // 选择整个视窗 + +// 选择器可以作为CSS选择器使用 +var paragraph = $('p'); // 选择所有段落元素 +var table1 = $('#table1'); // 选择id为table1的元素 +var squares = $('.square'); // 选择所有类是square的元素 +var square_p = $('p.square') // 选择具有square类的所有段落 + + +/////////////////////////////////// +// 2. 事件和效果 +// jQuery非常善于处理当事件触发的时候应该做什么 +// 一个非常常见的事件就是文档的就绪事件 +// 你可以用ready方法,在所有元素完成加载的时候执行 +$(document).ready(function(){ + // 只有文档加载完成以后代码才会执行 +}); +// 你也可以用定义了的函数 +function onAction() { + // 本函数在事件触发的时候被执行 +} +$('#btn').click(onAction); // 当点击的时候调用onAction函数 + +// 其它常见的事件: +$('#btn').dblclick(onAction); // 双击 +$('#btn').hover(onAction); // 划过 +$('#btn').focus(onAction); // 聚焦 +$('#btn').blur(onAction); // 失焦 +$('#btn').submit(onAction); // 提交 +$('#btn').select(onAction); // 当元素被选中 +$('#btn').keydown(onAction); // 当一个按键被按下 +$('#btn').keyup(onAction); // 当一个按键被抬起 +$('#btn').keypress(onAction); // 当一个按键被按住 +$('#btn').mousemove(onAction); // 当鼠标在移动 +$('#btn').mouseenter(onAction); // 鼠标移入元素 +$('#btn').mouseleave(onAction); // 鼠标离开元素 + + +// 如果不提供任何参数的话,那么这些方法可以触发事件 +// 而不是定义处理事件的方法 +$('#btn').dblclick(); // 触发元素上的双击 + +// 你可以只用选择器一次而处理多个事件 +$('#btn').on( + {dblclick: myFunction1} // 双击的时候触发 + {blur: myFunction1} // 失焦的时候触发 +); + +// 你可以用一些效果函数来移动或隐藏元素 +$('.table').hide(); // 隐藏元素 + +// 注意:在这些方法中调用函数会仍然隐藏元素 +$('.table').hide(function(){ + // 元素先隐藏然后函数被执行 +}); + +// 你可以在变量中储存选择器 +var tables = $('.table'); + +// 一些基本的文档操作方法有: +tables.hide(); // 隐藏元素 +tables.show(); // 显示元素 +tables.toggle(); // 对被选元素进行隐藏和显示的切换 +tables.fadeOut(); // 淡出 +tables.fadeIn(); // 淡入 +tables.fadeToggle(); // 对被选元素进行淡入和淡出显示的切换 +tables.fadeTo(0.5); // 把被选元素逐渐改变至给定的不透明度(0和1之间) +tables.slideUp(); // 通过调整高度来滑动隐藏被选元素 +tables.slideDown(); // 对被选元素进行滑动隐藏和滑动显示的切换 +tables.slideToggle(); // 对被选元素进行滑动隐藏和滑动显示的切换 + +// 上面所有的方法接受速度参数(毫秒)和一个回调函数 +tables.hide(1000, myFunction); // 持续一秒的隐藏动画然后执行函数 + +// fadeTo要求提供透明度参数作为第二个参数 +tables.fadeTo(2000, 0.1, myFunction); // 通过2秒钟将透明度变为0.1然后执行函数 + +// 你可以用animate方法实现一些略微高级的效果 +tables.animate({margin-top:"+=50", height: "100px"}, 500, myFunction); +// animate方法接受一个包含CSS和值的对象作为目标, +// 其次是可选的速度参数, +// 以及最后的回调函数 + +/////////////////////////////////// +// 3. 操作 + +// 这些类似效果函数但是可以做更多 +$('div').addClass('taming-slim-20'); // 给所有div添加类taming-slim-20 + +// 常见操作方法 +$('p').append('Hello world'); // 添加到元素末尾 +$('p').attr('class'); // 获取属性 +$('p').attr('class', 'content'); // 设置属性 +$('p').hasClass('taming-slim-20'); // 如果有类则为真 +$('p').height(); // 获取和设置元素的高度 + + +// 对于很多的操作函数来说,获取元素的信息 +// 仅仅是第一个符合元素的 +$('p').height(); // 仅仅获取第一个p标签的高度 + +// 你可以用each来迭代所有元素 +var heights = []; +$('p').each(function() { + heights.push($(this).height()); // 把所有p标签的高度加入数组 +}); + + +``` -- cgit v1.2.3 From d30d6f69ca07014efdce95d771403a43423d2143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Verona=20Biazibetti?= Date: Fri, 19 May 2017 18:34:27 -0300 Subject: [Kotlin/all] Remove whitespace in variable declaration with explicit type (#2733) * Removed whitespace in variable declaration with explicit type (Kotlin) * Removed whitespace in variable declaration with explicit type (Kotlin translations) --- zh-cn/kotlin-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown index eb69156b..5d655029 100644 --- a/zh-cn/kotlin-cn.html.markdown +++ b/zh-cn/kotlin-cn.html.markdown @@ -38,7 +38,7 @@ fun main(args: Array) { 所以我们不必要每次都去明确指定它。 我们可以像这样明确地声明一个变量的类型: */ - val foo : Int = 7 + val foo: Int = 7 /* 可以采取和Java类似的方法来表示一个字符串。 -- cgit v1.2.3 From a2ee84b2010691d7a54a9b335b12e92b366c7eae Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 1 Jun 2017 17:31:14 +0800 Subject: fix broken URL for vim (#2744) --- zh-cn/vim-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/vim-cn.html.markdown b/zh-cn/vim-cn.html.markdown index 569ad1bf..22dbace6 100644 --- a/zh-cn/vim-cn.html.markdown +++ b/zh-cn/vim-cn.html.markdown @@ -10,7 +10,7 @@ lang: zh-cn --- -[Vim](www.vim.org) +[Vim](http://www.vim.org) (Vi IMproved) 是 Unix 上的流行编辑器 vi 的克隆版本。这个文本编辑器 是为性能和提升效率而设计的,并且在大多数基于 unix 的系统上普遍存在。 它有大量的快捷键可用来快速导航到文件的特定位置,以便进行快速编辑。 -- cgit v1.2.3 From 0567c0776420429ed978781a8a9dce0f112d859f Mon Sep 17 00:00:00 2001 From: zhengbuqian Date: Fri, 9 Jun 2017 01:37:27 +0800 Subject: [sml/zh-cn] Chinese Translation for Standard ML (#2750) * translate sml to chinese to line 105 * finish translation of sml into chinese --- zh-cn/standard-ml-cn.html.markdown | 438 +++++++++++++++++++++++++++++++++++++ 1 file changed, 438 insertions(+) create mode 100644 zh-cn/standard-ml-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/standard-ml-cn.html.markdown b/zh-cn/standard-ml-cn.html.markdown new file mode 100644 index 00000000..269d6bc3 --- /dev/null +++ b/zh-cn/standard-ml-cn.html.markdown @@ -0,0 +1,438 @@ +--- +language: "Standard ML" +contributors: + - ["Simon Shine", "http://shine.eu.org/"] + - ["David Pedersen", "http://lonelyproton.com/"] + - ["James Baker", "http://www.jbaker.io/"] + - ["Leo Zovic", "http://langnostic.inaimathi.ca/"] +filename: standard-ml-cn.html +translators: + - ["Buqian Zheng", "https://github.com/zhengbuqian"] +lang: zh-cn +--- + +Standard ML是一门拥有类型推断和一些副作用的函数式编程语言。学习Standard ML的一些 +难点在于:递归、模式匹配和类型推断(猜测正确的类型但是决不允许隐式类型转换)。与Haskell的 +不同之处在于Standard ML拥有引用,允许对变量进行更新。 + +```ocaml +(* Standard ML的注释以 (* 开头,以 *) 结束。注释可以嵌套,也就意味着所有的 (* 标签都 + 需要由一个 *) 结束。这条注释就是两个嵌套的注释的例子。*) + +(* 一个Standard ML程序包括声明,例如值声明: *) +val rent = 1200 +val phone_no = 5551337 +val pi = 3.14159 +val negative_number = ~15 (* 是的,一元运算符用波浪符号`~`表示 *) + +(* 你当然也可以显示的声明类型,但这并不是必须的,因为ML会自动推断出值的类型。*) +val diameter = 7926 : int +val e = 2.718 : real +val name = "Bobby" : string + +(* 同样重要的还有函数: *) +fun is_large(x : int) = if x > 37 then true else false + +(* 浮点数被叫做实数: "real". *) +val tau = 2.0 * pi (* 两个real可以相乘 *) +val twice_rent = 2 * rent (* 两个int也可以相乘 *) +(* val meh = 1.25 * 10 *) (* 但是你不能让int和real相乘。 *) +val yeh = 1.25 * (Real.fromInt 10) (* ...除非你显示的把一个转换为另一个*) + +(* +, - 和 * 被重载过,所以可以作用于int和real。*) +(* 但是除法有单独的运算符: *) +val real_division = 14.0 / 4.0 (* 结果是 3.5 *) +val int_division = 14 div 4 (* 结果是 3, 向下取整 *) +val int_remainder = 14 mod 4 (* 结果是 2, 因为 3*4 = 12 *) + +(* ~ 有时其实是函数 (比如被放在变量前面的时候) *) +val negative_rent = ~(rent) (* 即使rent是"real"也正确 *) + +(* 当然也有布尔值和相关的运算符 *) +val got_milk = true +val got_bread = false +val has_breakfast = got_milk andalso got_bread (* 'andalso' 是运算符 *) +val has_something = got_milk orelse got_bread (* 'orelse' 是运算符 *) +val is_sad = not(has_something) (* not 是一个函数 *) + +(* 很多值都可以用判等性运算符进行比较: = 和 <> *) +val pays_same_rent = (rent = 1300) (* false *) +val is_wrong_phone_no = (phone_no <> 5551337) (* false *) + +(* <> 运算符就是其他大部分语言里的 != 。 *) +(* 'andalso' 和 'orelse' 在很多其他语言里被叫做 && 和 || 。 *) + +(* 实际上,上面大部分的圆括号都是不需要的。比如表达上面内容的一些不同的方式: *) +fun is_large x = x > 37 +val is_sad = not has_something +val pays_same_rent = rent = 1300 (* 看起来很奇怪,但是就是这样的。 *) +val is_wrong_phone_no = phone_no <> 5551337 +val negative_rent = ~rent (* ~ rent (注意空格) 也正确 *) + +(* 圆括号大部分时候用来把东西们组合在一起 *) +val some_answer = is_large (5 + 5) (* 没有圆括号的话会出错! *) +(* val some_answer = is_large 5 + 5 *) (* 会被理解为: (is_large 5) + 5. 错了! *) + +(* 除了boolean, int, real,Standard ML也有char和string *) +val foo = "Hello, World!\n" (* \n是换行的转移字符 *) +val one_letter = #"a" (* 这种酷炫的语法表示一个字符a *) + +val combined = "Hello " ^ "there, " ^ "fellow!\n" (* 拼接字符串 *) + +val _ = print foo (* 你可以打印一些东西,这儿我们队打印的结果并不感兴趣,因此 *) +val _ = print combined (* 用 _ 把结果丢掉了 *) +(* val _ = print one_letter *) (* 只有字符串才能被这样打印 *) + + +val bar = [ #"H", #"e", #"l", #"l", #"o" ] (* SML 也有列表! *) +(* val _ = print bar *) (* 然而列表和string是不同的 *) + +(* 当然这二者可以相互转换。String是一个库,implode和size是这个库里接受string作为 + 参数的函数。*) +val bob = String.implode bar (* 结果是 "Hello" *) +val bob_char_count = String.size bob (* 结果是 5 *) +val _ = print (bob ^ "\n") (* 为了好看加了个换行符 *) + +(* 列表可以包含任意类型的元素 *) +val numbers = [1, 3, 3, 7, 229, 230, 248] (* : int list *) +val names = [ "Fred", "Jane", "Alice" ] (* : string list *) + +(* 列表甚至可以包含列表! *) +val groups = [ [ "Alice", "Bob" ], + [ "Huey", "Dewey", "Louie" ], + [ "Bonnie", "Clyde" ] ] (* : string list list *) + +val number_count = List.length numbers (* 结果是 7 *) + +(* 你可以使用 :: 操作符把单个值放到同样类型列表的最前面。 + :: 叫做con操作符(名字来自Lisp) *) +val more_numbers = 13 :: numbers (* 结果是 [13, 1, 3, 3, 7, ...] *) +val more_groups = ["Batman","Superman"] :: groups + +(* 拥有同样类型元素的列表可以使用 @ 操作符连接起来 *) +val guest_list = [ "Mom", "Dad" ] @ [ "Aunt", "Uncle" ] + +(* 使用 :: 操作符也能完成这项工作。但是这有点绕,因为左手边必须是单个元素 + 而右边必须是这种元素的列表 *) +val guest_list = "Mom" :: "Dad" :: [ "Aunt", "Uncle" ] +val guest_list = "Mom" :: ("Dad" :: ("Aunt" :: ("Uncle" :: []))) + +(* 如果你有很多同样类型的列表,也可以整个拼接成一个。 *) +val everyone = List.concat groups (* [ "Alice", "Bob", "Huey", ... ] *) + +(* 列表可以包含任意(无限)数量的元素 *) +val lots = [ 5, 5, 5, 6, 4, 5, 6, 5, 4, 5, 7, 3 ] (* still just an int list *) + +(* 但是列表只能包含一种类型的元素 *) +(* val bad_list = [ 1, "Hello", 3.14159 ] : ??? list *) + +(* 而元组Tuples则可以包含有限固定数量的不同类型的元素 *) +val person1 = ("Simon", 28, 3.14159) (* : string * int * real *) + +(* 你甚至可以让列表和元组相互嵌套 *) +val likes = [ ("Alice", "ice cream"), + ("Bob", "hot dogs"), + ("Bob", "Alice") ] (* : (string * string) list *) + +val mixup = [ ("Alice", 39), + ("Bob", 37), + ("Eve", 41) ] (* : (string * int) list *) + +val good_bad_stuff = + (["ice cream", "hot dogs", "chocolate"], + ["liver", "paying the rent" ]) (* : string list * string list *) + +(* 记录Record是每个位置带名字的元组 *) + +val rgb = { r=0.23, g=0.56, b=0.91 } (* : {b:real, g:real, r:real} *) + +(* 使用Record时不需要提前声明每个位置的名字。 有不同名字的Record属于不同的类型 + 即使他们的值的类型是相同的。比如说:*) +val Hsl = { H=310.3, s=0.51, l=0.23 } (* : {H:real, l:real, s:real} *) +val Hsv = { H=310.3, s=0.51, v=0.23 } (* : {H:real, s:real, v:real} *) + +(* ...如果你想判断 `Hsv = Hsl` 或者 `rgb = Hsl` 的话,会得到一个类型错误。虽然他们都包含3个 + real,但是由于名字不同,其类型也不同。 *) + +(* 可以使用 # 符号取出元组的值 *) + +val H = #H Hsv (* : real *) +val s = #s Hsl (* : real *) + +(* 函数! *) +fun add_them (a, b) = a + b (* 一个简单的加法函数 *) +val test_it = add_them (3, 4) (* 结果是 7 *) + +(* 复杂函数通常会为了可读性写成多行 *) +fun thermometer temp = + if temp < 37 + then "Cold" + else if temp > 37 + then "Warm" + else "Normal" + +val test_thermo = thermometer 40 (* 结果是 "Warm" *) + +(* if 实际上是表达式而不是声明。一个函数体只可以包含一个表达式。但是还是有一些小技巧 + 让一个函数做更多的事。 *) + +(* 函数也可以使用调用自己的结果 (递归!) *) +fun fibonacci n = + if n = 0 then 0 else (* 终止条件 *) + if n = 1 then 1 else (* 终止条件 *) + fibonacci (n - 1) + fibonacci (n - 2) (* 递归 *) + +(* 有的时候,手写出递归函数的执行过程能帮助理解递归概念: + + fibonacci 4 + ~> fibonacci (4 - 1) + fibonacci (4 - 2) + ~> fibonacci 3 + fibonacci 2 + ~> (fibonacci (3 - 1) + fibonacci (3 - 2)) + fibonacci 2 + ~> (fibonacci 2 + fibonacci 1) + fibonacci 2 + ~> ((fibonacci (2 - 1) + fibonacci (2 - 2)) + fibonacci 1) + fibonacci 2 + ~> ((fibonacci 1 + fibonacci 0) + fibonacci 1) + fibonacci 2 + ~> ((1 + fibonacci 0) + fibonacci 1) + fibonacci 2 + ~> ((1 + 0) + fibonacci 1) + fibonacci 2 + ~> (1 + fibonacci 1) + fibonacci 2 + ~> (1 + 1) + fibonacci 2 + ~> 2 + fibonacci 2 + ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2)) + ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2)) + ~> 2 + (fibonacci 1 + fibonacci 0) + ~> 2 + (1 + fibonacci 0) + ~> 2 + (1 + 0) + ~> 2 + 1 + ~> 3 第四个斐波那契数 + + *) + +(* 函数不能改变它引用的值。它只能暂时的使用同名的新变量来覆盖这个值。也就是说,变量其实是 + 常数,只有在递归的时候才表现的比较像变量。因此,变量也被叫做值绑定。举个例子: *) + +val x = 42 +fun answer(question) = + if question = "What is the meaning of life, the universe and everything?" + then x + else raise Fail "I'm an exception. Also, I don't know what the answer is." +val x = 43 +val hmm = answer "What is the meaning of life, the universe and everything?" +(* 现在 hmm 的值是 42。 这是因为函数 answer 引用的x是函数定义之前的x。 *) + +(* 函数通过接受一个元组来接受多个参数。 *) +fun solve2 (a : real, b : real, c : real) = + ((~b + Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a), + (~b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a)) + +(* 有时候同样的计算会被计算多次,因此把结果保存下来以重复使用是很有必要的。 + 这时可以使用 let 绑定。 *) +fun solve2 (a : real, b : real, c : real) = + let val discr = b * b - 4.0 * a * c + val sqr = Math.sqrt discr + val denom = 2.0 * a + in ((~b + sqr) / denom, + (~b - sqr) / denom) + end + +(* 模式匹配是函数式编程的一个精巧的部分,它是实现 if 的另一种方式。 + 斐波那契函数可以被重写为如下方式: *) +fun fibonacci 0 = 0 (* 终止条件 *) + | fibonacci 1 = 1 (* 终止条件 *) + | fibonacci n = fibonacci (n - 1) + fibonacci (n - 2) (* 递归 *) + +(* 模式匹配也可以用于比如元组、列表和记录的复合类型。"fun solve2 (a, b, c) = ..." + 的写法实际上也是对于一个三元素元组的模式匹配。类似但是比较不直观的是你也可以从列表的开头 + 对列表元素进行匹配。 *) +fun first_elem (x::xs) = x +fun second_elem (x::y::xs) = y +fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs + | evenly_positioned_elems [odd] = [] (* 终止条件:丢弃结果 *) + | evenly_positioned_elems [] = [] (* 终止条件 *) + +(* 匹配记录的时候,比如使用每个位置的名字,每个位置的值都需要绑定,但是顺序并不重要。 *) + +fun rgbToTup {r, g, b} = (r, g, b) (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *) +fun mixRgbToTup {g, b, r} = (r, g, b) (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *) + +(* 如果传入参数 {r=0.1, g=0.2, b=0.3},上面的两个函数都会返回 (0.1, 0.2, 0.3)。 + 但是传入参数 {r=0.1, g=0.2, b=0.3, a=0.4} 的话则会得到类型错误 *) + +(* 高阶函数: 可以接受其他函数作为参数的函数 + 函数只不过是另一种类型的值,不需要依附与一个名字而存在。 + 没有名字的函数被叫做匿名函数或者lambda表达式或者闭包(因为匿名函数也依赖于词法作用域)*) +val is_large = (fn x => x > 37) +val add_them = fn (a,b) => a + b +val thermometer = + fn temp => if temp < 37 + then "Cold" + else if temp > 37 + then "Warm" + else "Normal" + +(* 下面的代码就是用了匿名函数,结果是 "ColdWarm" *) +val some_result = (fn x => thermometer (x - 5) ^ thermometer (x + 5)) 37 + +(* 这是一个作用于列表的高阶函数 *) +(* map f l + 把f从左至右作用于l的每一个元素,并返回结果组成的列表。 *) +val readings = [ 34, 39, 37, 38, 35, 36, 37, 37, 37 ] (* 先定义一个列表 *) +val opinions = List.map thermometer readings (* 结果是 [ "Cold", "Warm", ... ] *) + +(* filter 函数用于筛选列表 *) +val warm_readings = List.filter is_large readings (* 结果是 [39, 38] *) + +(* 你也可以创建自己的高阶函数。函数也可以通过 curry 来接受多个参数。 + 从语法上来说,curry就是使用空格来分隔参数,而不是逗号和括号。 *) +fun map f [] = [] + | map f (x::xs) = f(x) :: map f xs + +(* map 的类型是 ('a -> 'b) -> 'a list -> 'b list ,这就是多态。 *) +(* 'a 被叫做类型变量 *) + + +(* 函数可以被声明为中缀的。 *) +val plus = add_them (* plus 现在和 add_them 是同一个函数。 *) +infix plus (* plus 现在是一个中缀操作符。 *) +val seven = 2 plus 5 (* seven 现在被绑定上了 7 *) + +(* 函数也可以在声明之前就声明为中缀 *) +infix minus +fun x minus y = x - y (* 这样有点不容易判断哪个是参数。 *) +val four = 8 minus 4 (* four 现在被绑定上了 4 *) + +(* 中缀函数/操作符也可以使用 'op' 函数变回前缀函数。 *) +val n = op + (5, 5) (* n is now 10 *) + +(* 'op' 在结合高阶函数的时候非常有用,因为高阶函数接受的是函数而不是操作符作为参数。 + 大部分的操作符其实都是中缀函数。 *) +(* foldl f init [x1, x2, ..., xn] + 返回 + f(xn, ...f(x2, f(x1, init))...) + 或者如果列表为空时返回 init *) +val sum_of_numbers = foldl op+ 0 [1, 2, 3, 4, 5] + + +(* 可以很方便的使用 datatype 定义或简单或复杂的数据结构。 *) +datatype color = Red | Green | Blue + +(* 这个函数接受 color 之一作为参数。 *) +fun say(col) = + if col = Red then "You are red!" else + if col = Green then "You are green!" else + if col = Blue then "You are blue!" else + raise Fail "Unknown color" + +val _ = print (say(Red) ^ "\n") + +(* datatype 经常和模式匹配一起使用。 *) +fun say Red = "You are red!" + | say Green = "You are green!" + | say Blue = "You are blue!" + | say _ = raise Fail "Unknown color" + + +(* 一个二叉树 datatype *) +datatype 'a btree = Leaf of 'a + | Node of 'a btree * 'a * 'a btree (* 三个参数的构造器 *) + +(* 一颗二叉树: *) +val myTree = Node (Leaf 9, 8, Node (Leaf 3, 5, Leaf 7)) + +(* 画出来应该是这个样子: + + 8 + / \ + leaf -> 9 5 + / \ + leaf -> 3 7 <- leaf + *) + +(* 这个函数计算所有节点值的和。 *) +fun count (Leaf n) = n + | count (Node (leftTree, n, rightTree)) = count leftTree + n + count rightTree + +val myTreeCount = count myTree (* myTreeCount is now bound to 32 *) + + +(* 异常! *) +(* 使用关键字 'raise' 来抛出异常: *) +fun calculate_interest(n) = if n < 0.0 + then raise Domain + else n * 1.04 + +(* 使用 "handle" 关键字来处理异常 *) +val balance = calculate_interest ~180.0 + handle Domain => ~180.0 (* x 现在的值是 ~180.0 *) + +(* 某些异常还包含额外信息 *) +(* 一些内建异常的例子: *) +fun failing_function [] = raise Empty (* 空列表异常 *) + | failing_function [x] = raise Fail "This list is too short!" + | failing_function [x,y] = raise Overflow (* 用作计算 *) + | failing_function xs = raise Fail "This list is too long!" + +(* 使用 'handle' 时也可以使用模式匹配来保证异常都被处理。 *) +val err_msg = failing_function [1,2] handle Fail _ => "Fail was raised" + | Domain => "Domain was raised" + | Empty => "Empty was raised" + | _ => "Unknown exception" + +(* err_msg 的值会是 "Unknown exception" + 因为 Overflow 没有在模式中列出,因此匹配到了通配符_。 *) + +(* 我们也可以定义自己的异常 *) +exception MyException +exception MyExceptionWithMessage of string +exception SyntaxError of string * (int * int) + +(* 文件读写! *) +(* 把一首诗写进文件: *) +fun writePoem(filename) = + let val file = TextIO.openOut(filename) + val _ = TextIO.output(file, "Roses are red,\nViolets are blue.\n") + val _ = TextIO.output(file, "I have a gun.\nGet in the van.\n") + in TextIO.closeOut(file) + end + +(* 把一首诗读进一个字符串列表: *) +fun readPoem(filename) = + let val file = TextIO.openIn filename + val poem = TextIO.inputAll file + val _ = TextIO.closeIn file + in String.tokens (fn c => c = #"\n") poem + end + +val _ = writePoem "roses.txt" +val test_poem = readPoem "roses.txt" (* gives [ "Roses are red,", + "Violets are blue.", + "I have a gun.", + "Get in the van." ] *) + +(* 我们还可以创建指向值的引用,引用可以被更新。 *) +val counter = ref 0 (* 使用 ref 函数创建一个引用。 *) + +(* 使用赋值运算符给引用复制 *) +fun set_five reference = reference := 5 + +(* 使用解引用运算符得到引用的值 *) +fun equals_five reference = !reference = 5 + +(* 递归很复杂的时候,也可以使用 while 循环 *) +fun decrement_to_zero r = if !r < 0 + then r := 0 + else while !r >= 0 do r := !r - 1 + +(* 这将会返回 unit (也就是什么都没有,一个0元素的元组) *) + +(* 要返回值,可以使用分号来分开表达式。 *) +fun decrement_ret x y = (x := !x - 1; y) +``` + +## 阅读更多 + +* 安装交互式编译器 (REPL),如: + [Poly/ML](http://www.polyml.org/), + [Moscow ML](http://mosml.org), + [SML/NJ](http://smlnj.org/). +* 上Coursera上的课程 [Programming Languages](https://www.coursera.org/course/proglang). +* 购买 Larry C. Paulson 写的 *ML for the Working Programmer* 书。 +* 使用 [StackOverflow's sml 标签](http://stackoverflow.com/questions/tagged/sml). -- cgit v1.2.3 From 341d7b3c6636b4e66dc1eaf92a7b80497cf551f5 Mon Sep 17 00:00:00 2001 From: bugsnail Date: Thu, 13 Jul 2017 15:50:58 +0800 Subject: fix wrongly written (#2797) --- zh-cn/java-cn.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/java-cn.html.markdown b/zh-cn/java-cn.html.markdown index 1e9c38f6..27003f3e 100644 --- a/zh-cn/java-cn.html.markdown +++ b/zh-cn/java-cn.html.markdown @@ -108,7 +108,7 @@ public class LearnJava { boolean [] booleanArray = new boolean[100]; // 声明并初始化数组也可以这样: - int [] y = {9000, 1000, 1337}; + int [] intArray = {9000, 1000, 1337}; // 随机访问数组中的元素 System.out.println("intArray @ 0: " + intArray[0]); @@ -309,7 +309,7 @@ class Bicycle { name = "Bontrager"; } - // 一下是一个含有参数的构造函数 + // 以下是一个含有参数的构造函数 public Bicycle(int startCadence, int startSpeed, int startGear, String name) { this.gear = startGear; this.cadence = startCadence; -- cgit v1.2.3