Skip to content

Commit 5326001

Browse files
committed
fix tables and use @repl
1 parent 92a9531 commit 5326001

26 files changed

Lines changed: 193 additions & 360 deletions

docs/advanced/ccall.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
原型是 `ccall((function_name, library), returntype, (argtype1, ...), argvalue1, ...)``ccall(function_name, returntype, (argtype1, ...), argvalue1, ...)``ccall(function_pointer, returntype, (argtype1, ...), argvalue1, ...)`,其中 library 是库的路径;每个 `argvalue` 会通过 `unsafe_convert(argtype, cconvert(argtype, argvalue))` 转化为 `argtype` 类型实例。
88

99
调用 C 标准库的示例(需注意,这些函数大多在 `Libc` 模块中已有,无需自己 ccall)
10-
```julia-repl
11-
julia> ccall(:srand, Cvoid, (Cint,), 0)
12-
13-
julia> ccall(:rand, Cint, ())
14-
38
10+
```@repl
11+
ccall(:srand, Cvoid, (Cint,), 0)
12+
ccall(:rand, Cint, ())
1513
```
1614

1715
一个调用 [windows api](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos) 获取鼠标指针位置的示例:
@@ -33,11 +31,9 @@ Point(681, 404)
3331

3432
## @ccall
3533
原型是 `@ccall library.function_name(argvalue1::argtype1, ...)::returntype``@ccall function_name(argvalue1::argtype1, ...)::returntype``@ccall $function_pointer(argvalue1::argtype1, ...)::returntype`
36-
```julia-repl
37-
julia> @ccall srand(0::Cint)::Cvoid
38-
39-
julia> @ccall rand()::Cint
40-
38
34+
```@repl
35+
@ccall srand(0::Cint)::Cvoid
36+
@ccall rand()::Cint
4137
```
4238

4339
## 参阅

docs/advanced/expr.md

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
第一:一个标识表达式类型的 `head` 类型为 `Symbol`,是一个 [interned string](https://en.wikipedia.org/wiki/String_interning) 标识符(下面会有更多讨论)
55

6-
```julia-repl
7-
julia> ex1 = Meta.parse("1 + 1")
8-
:(1 + 1)
9-
10-
julia> typeof(ex1)
11-
Expr
12-
13-
julia> ex1.head
14-
:call
6+
```@repl
7+
ex1 = Meta.parse("1 + 1")
8+
typeof(ex1)
9+
ex1.head
1510
```
1611

1712
第二:参数 `args`,可能是符号、其他表达式或字面量:
@@ -25,9 +20,8 @@ julia> ex1.args
2520
```
2621

2722
表达式也可能直接用前置表达式形式构造:
28-
```julia-repl
29-
julia> ex2 = Expr(:call, :+, 1, 1)
30-
:(1 + 1)
23+
```@repl
24+
ex2 = Expr(:call, :+, 1, 1)
3125
```
3226

3327
上面构造的两个表达式————一个通过解析构造一个通过直接构造————是等价的:
@@ -48,9 +42,8 @@ Expr
4842
```
4943

5044
`Expr` 对象也可以嵌套:
51-
```julia-repl
52-
julia> ex3 = Meta.parse("(4 + 4) / 2")
53-
:((4 + 4) / 2)
45+
```@repl
46+
ex3 = Meta.parse("(4 + 4) / 2")
5447
```
5548

5649
另外一个查看表达式的方法是使用 `Meta.show_sexpr`,它能显示给定 `Expr`[S-expression](https://en.wikipedia.org/wiki/S-expression),对 Lisp 用户来说,这看着很熟悉。下面是一个示例,阐释了如何显示嵌套的 `Expr`
@@ -219,33 +212,19 @@ end
219212

220213
### QuoteNode
221214
`quote` 形式在 AST 中通常表示为一个 head 为 `:quote``Expr`
222-
```julia-repl
223-
julia> dump(Meta.parse(":(1+2)"))
224-
Expr
225-
head: Symbol quote
226-
args: Array{Any}((1,))
227-
1: Expr
228-
head: Symbol call
229-
args: Array{Any}((3,))
230-
1: Symbol +
231-
2: Int64 1
232-
3: Int64 2
215+
```@repl
216+
dump(Meta.parse(":(1+2)"))
233217
```
234218

235219
正如我们所看到的,这些表达式支持插值符号 `$`。但是,在某些情况下,需要在*不执行*插值的情况下引用代码。这种引用还没有语法,但在内部表示为 `QuoteNode` 类型的对象:
236-
```julia-repl
237-
julia> eval(Meta.quot(Expr(:$, :(1+2))))
238-
3
239-
240-
julia> eval(QuoteNode(Expr(:$, :(1+2))))
241-
:($(Expr(:$, :(1 + 2))))
220+
```@repl
221+
eval(Meta.quot(Expr(:$, :(1+2))))
222+
eval(QuoteNode(Expr(:$, :(1+2))))
242223
```
243224

244225
解析器为简单的引用项(如符号)生成 `QuoteNode`
245-
```julia-repl
246-
julia> dump(Meta.parse(":x"))
247-
QuoteNode
248-
value: Symbol x
226+
```@repl
227+
dump(Meta.parse(":x"))
249228
```
250229

251-
`QuoteNode` 也可用于某些高级的元编程任务
230+
`QuoteNode` 也可用于某些高级的元编程任务

docs/advanced/function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ julia> f = +; f(1, 2, 3)
1515
## 具有特殊名称的操作符
1616
有一些特殊的表达式对应的函数调用没有显示的函数名称
1717
| 表达式 | 函数调用 |
18-
| --- | --- |
18+
| :-: | :-: |
1919
| `[A B C ...]` | `hcat` |
2020
| `[A; B; C; ...]` | `vcat` |
2121
| `[A B; C D; ...]` | `hvcat` |

docs/advanced/io.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
## IO缓冲区
1919
`Base.GenericIOBuffer` 可以将数组作为缓冲区进行包裹。
2020
`IOBuffer` 可以用于快速生成一个 `Base.GenericIOBuffer{Vector{UInt8}}` 实例,这可以方便地将常用数据包裹成 I/O
21-
```julia-repl
22-
julia> buf = IOBuffer()
23-
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)
24-
25-
julia> write(buf, "foo")
26-
3
27-
28-
julia> String(take!(buf))
29-
"foo"
21+
```@repl
22+
buf = IOBuffer()
23+
write(buf, "foo")
24+
String(take!(buf))
3025
```
3126

3227
## 管道
@@ -40,7 +35,7 @@ julia> String(take!(buf))
4035

4136
## 通用函数
4237
| 名称 | 描述 |
43-
| --- | --- |
38+
| :-: | :-: |
4439
| `read(io::IO, T)` |`io` 中读取 `T` 类型的单个值,使用标准二进制表示,需手动使用 `ntoh``ltoh` 调整[大小端](https://www.ruanyifeng.com/blog/2022/06/endianness-analysis.html) |
4540
| `read(io::IO, String)` | 将整个 `io` 作为字符串读入 |
4641
| ` read(s::IO, nb=typemax(Int))` |`s` 中读入最多 `nb` 个字符,返回 `Vector{UInt8}` 实例 |

docs/advanced/iteration.md

Lines changed: 36 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,40 @@
44

55
`eachindex` 通常用于得到数组索引的合适遍历工具。它比使用 `1:length(A)` 兼容性更强,因为 `1:length(A)` 不适用于多维数组、数组 `view` 及其它包可能出现的数组(如索引不从 1 开始的数组)。
66

7-
```julia-repl
8-
julia> A = [10 20; 30 40];
9-
10-
julia> for i in eachindex(A)
11-
println("A[", i, "] == ", A[i])
12-
end
13-
A[1] == 10
14-
A[2] == 30
15-
A[3] == 20
16-
A[4] == 40
7+
```@repl
8+
A = [10 20; 30 40];
9+
10+
for i in eachindex(A)
11+
println("A[", i, "] == ", A[i])
12+
end
1713
```
1814

1915
使用 `enumerate` 遍历物品时,得到的值是一对 `(i, v)`,其中 `i` 是从 1 开始的计数器,`v` 是正常遍历物品得到的值。
2016

21-
```julia-repl
22-
julia> a = ["a", "b", "c"];
23-
24-
julia> for (index, balue) in enumerate(a)
25-
println("a[$index] = $value")
26-
end
27-
ERROR: UndefVarError: `value` not defined in `Main`
28-
Suggestion: check for spelling errors or missing imports.
29-
Stacktrace:
30-
[1] top-level scope
31-
@ .\REPL[30]:2
32-
33-
julia> for (index, value) in enumerate(a)
34-
println("a[$index] = $value")
35-
end
36-
a[1] = a
37-
a[2] = b
38-
a[3] = c
39-
```
40-
41-
`zip` 可用于合并多个可遍历的物品,一直遍历直到其中一个到达末尾。
42-
```julia-repl
43-
julia> z = zip(1:3, "abcde")
44-
zip(1:3, "abcde")
17+
```@repl
18+
a = ["a", "b", "c"];
4519
46-
julia> length(z)
47-
3
20+
for (index, balue) in enumerate(a)
21+
println("a[$index] = $value")
22+
end
4823
49-
julia> first(z)
50-
(1, 'a')
24+
for (index, value) in enumerate(a)
25+
println("a[$index] = $value")
26+
end
27+
```
5128

52-
julia> collect(z)
53-
3-element Vector{Tuple{Int64, Char}}:
54-
(1, 'a')
55-
(2, 'b')
56-
(3, 'c')
29+
`zip` 可用于合并多个可遍历的物品,一直遍历直到其中一个到达末尾。
30+
```@repl
31+
z = zip(1:3, "abcde")
32+
length(z)
33+
first(z)
34+
collect(z)
5735
```
5836

5937
`filter` 用于生成满足特定要求的迭代器,需注意在使用时不与导出的另一个 `filter` 混淆
60-
```julia-repl
61-
julia> f = Iterators.filter(isodd, [1, 2, 3, 4, 5])
62-
Base.Iterators.Filter{typeof(isodd), Vector{Int64}}(isodd, [1, 2, 3, 4, 5])
63-
64-
julia> collect(f)
65-
3-element Vector{Int64}:
66-
1
67-
3
68-
5
38+
```@repl
39+
f = Iterators.filter(isodd, [1, 2, 3, 4, 5])
40+
collect(f)
6941
```
7042

7143
## 自定义
@@ -91,57 +63,24 @@ end
9163
```
9264

9365
这是一个自定义的样例:
94-
```julia
66+
```@repl
9567
struct n3
96-
v::Int
68+
v::Int
9769
end
9870
9971
function Base.iterate(i::n3, n::Int=i.v) # 第一次调用时不会有第二个参数
100-
if n==1
101-
return nothing # 表示结束
102-
end
103-
v= n&1==0 ? n>>1 : n*3+1
104-
return (v, v) # (返回值,下一个状态(作为第二个参数))
72+
if n == 1
73+
return nothing # 表示结束
74+
end
75+
v= n&1==0 ? n>>1 : n*3+1
76+
return (v, v) # (返回值, 下一个状态)
77+
end
78+
79+
for i in n3(10)
80+
println(i, ' ')
10581
end
10682
107-
julia> for i in n3(10)
108-
println(i, ' ')
109-
end
110-
5
111-
16
112-
8
113-
4
114-
2
115-
1
116-
117-
julia> [n3(27)...]
118-
111-element Vector{Int64}:
119-
82
120-
41
121-
124
122-
62
123-
31
124-
94
125-
47
126-
142
127-
71
128-
214
129-
107
130-
322
131-
161
132-
133-
53
134-
160
135-
80
136-
40
137-
20
138-
10
139-
5
140-
16
141-
8
142-
4
143-
2
144-
1
83+
[n3(27)...]
14584
```
14685

14786
[^1]: https://docs.juliacn.com/latest/manual/interfaces

docs/advanced/mime.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,19 @@ MIME 的通用格式是 `type/subtype`
1212

1313
## 类型
1414
Julia 中有两种方式分别创建 MIME 类型和 MIME 类型的值。
15-
```julia-repl
16-
julia> type = MIME"text/plain"
17-
MIME{Symbol("text/plain")}
18-
19-
julia> value = MIME("text/plain")
20-
MIME type text/plain
21-
22-
julia> isa(value, type)
23-
true
15+
```@repl
16+
type = MIME"text/plain"
17+
value = MIME("text/plain")
18+
isa(value, type)
2419
```
2520

2621
## 多媒体显示
2722
MIME 被允许作为 [`show`](typesystem.md#自定义显示) 的第二个参数,用于控制输出格式。
2823

2924
例如,在纯文本环境下可以选择不同的文本格式类型
30-
```julia-repl
31-
julia> show(stdout, MIME("text/plain"), [1 2;3 4])
32-
2×2 Matrix{Int64}:
33-
1 2
34-
3 4
35-
julia> show(stdout, MIME("text/csv"), [1 2;3 4])
36-
1,2
37-
3,4
25+
```@repl
26+
show(stdout, MIME("text/plain"), [1 2;3 4])
27+
show(stdout, MIME("text/csv"), [1 2;3 4])
3828
```
3929

4030
## 参阅

0 commit comments

Comments
 (0)