Function Call Syntactic Sugar
Inspect one language lane at a time so line-level text and code deltas stay readable.
Diff Lane
English
0 modified sections0 code block delta0 anchor delta
Diff Lane
中文
2 modified sections0 code block delta0 anchor delta
modifiedPipeline 表达式text+1 line, -1 line
v1.0.5
Section Text
1
当需要对输入数据做一系列的处理时,可以使用 `pipeline` 表达式来简化描述。`pipeline` 表达式的语法形式如下:`e1 |> e2`。等价于如下形式的语法糖:`let v = e1; e2(v)` 。2
3
其中 `e2` 是函数类型的表达式,`e1` 的类型是 `e2` 的参数类型的子类型。4
5
示例:6
7
<!-- compile -->Code 1 · cangjie
1
func inc(x: Array<Int64>): Array<Int64> { // Increasing the value of each element in the array by '1'2
let s = x.size3
var i = 04
for (e in x where i < s) {5
x[i] = e + 16
i++7
}8
x9
}10
11
func sum(y: Array<Int64>): Int64 { // Get the sum of elements in the array12
var s = 013
for (j in y) {14
s += j15
}16
s17
}18
19
let arr: Array<Int64> = [1, 3, 5]20
let res = arr |> inc |> sum // res = 12v1.1.0
Section Text
1
当需要对输入数据做一系列的处理时,可以使用 `pipeline` 表达式来简化描述。`pipeline` 表达式的语法形式如下:`e1 |> e2`。等价于如下形式的语法糖:`let v = e1; e2(v)`。2
3
其中 `e2` 是函数类型的表达式,`e1` 的类型是 `e2` 的参数类型的子类型。4
5
示例:6
7
<!-- compile -->Code 1 · cangjie
1
func inc(x: Array<Int64>): Array<Int64> { // Increasing the value of each element in the array by '1'2
let s = x.size3
var i = 04
for (e in x where i < s) {5
x[i] = e + 16
i++7
}8
x9
}10
11
func sum(y: Array<Int64>): Int64 { // Get the sum of elements in the array12
var s = 013
for (j in y) {14
s += j15
}16
s17
}18
19
let arr: Array<Int64> = [1, 3, 5]20
let res = arr |> inc |> sum // res = 12modifiedComposition 表达式textcode+3 lines, -3 lines
v1.0.5
Section Text
1
`composition` 表达式表示两个单参函数的组合。`composition` 表达式语法为 `f ~> g`,等价于 `{ x => g(f(x)) }`。2
3
其中 `f` 和 `g` 均为只有一个参数的函数类型的表达式。4
5
`f` 和 `g` 组合,则要求 `f(x)` 的返回类型是 `g(...)` 的参数类型的子类型。6
7
示例 1:8
9
<!-- compile -->10
11
12
示例 2:13
14
<!-- compile -->15
16
17
示例 3:18
19
<!-- compile -->20
21
22
> **注意:**23
>24
> 表达式 f ~> g 中,会先对 f 求值,然后对 g 求值,最后才会进行函数的组合。25
26
另外,流操作符不能与无默认值的命名形参函数直接一同使用,这是因为无默认值的命名形参函数必须给出命名实参才可以调用。例如:27
28
<!-- compile.error -->29
30
31
如果需要使用,开发者可以通过 lambda 表达式传入 `f` 函数的命名实参:32
33
<!-- compile -->34
35
36
由于相同的原因,当 `f` 的参数有默认值时,直接与流运算符一起使用也是错误的,例如:37
38
<!-- compile.error -->39
40
41
但是当命名形参都存在默认值时,不需要给出命名实参也可以调用该函数,函数仅需要传入非命名形参,那么这种函数是可以同流运算符一起使用的,例如:42
43
<!-- compile -->44
45
46
当然,如果想要在调用 `f` 时,为参数 `b` 传入其他参数,那么也需要借助 lambda 表达式:47
48
<!-- compile -->Code 1 · cangjie
1
func f(x: Int64): Float64 {2
Float64(x)3
}4
func g(x: Float64): Float64 {5
x6
}7
8
var fg = f ~> g // The same as { x: Int64 => g(f(x)) }Code 2 · cangjie
1
func f(x: Int64): Float64 {2
Float64(x)3
}4
5
let lambdaComp = {x: Int64 => x} ~> f // The same as { x: Int64 => f({x: Int64 => x}(x)) }Code 3 · cangjie
1
func h1<T>(x: T): T { x }2
func h2<T>(x: T): T { x }3
var hh = h1<Int64> ~> h2<Int64> // The same as { x: Int64 => h2<Int64>(h1<Int64>(x)) }Code 4 · cangjie
1
func f(a!: Int64): Unit {}2
3
var a = 1 |> f // ErrorCode 5 · cangjie
1
func f(a!: Int64): Unit {}2
3
var x = 1 |> { x: Int64 => f(a: x) } // OKCode 6 · cangjie
1
func f(a!: Int64 = 2): Unit {}2
3
var a = 1 |> f // ErrorCode 7 · cangjie
1
func f(a: Int64, b!: Int64 = 2): Unit {}2
3
var a = 1 |> f // OKCode 8 · cangjie
1
func f(a: Int64, b!: Int64 = 2): Unit {}2
3
var a = 1 |> {x: Int64 => f(x, b: 3)} // OKv1.1.0
Section Text
1
`composition` 表达式表示两个单参函数的组合。`composition` 表达式语法为 `f ~> g`,等价于 `{ x => g(f(x)) }`。2
3
其中 `f` 和 `g` 均为只有一个参数的函数类型的表达式。4
5
`f` 和 `g` 组合,则要求 `f(x)` 的返回类型是 `g(...)` 的参数类型的子类型。6
7
示例 1:8
9
<!-- compile -->10
11
12
示例 2:13
14
<!-- compile -->15
16
17
示例 3:18
19
<!-- compile -->20
21
22
> **注意:**23
>24
> 表达式 `f ~> g` 中,会先对 `f` 求值,然后对 `g` 求值,最后才会进行函数的组合。25
26
另外,流操作符不能与无默认值的命名形参函数直接一同使用,这是因为无默认值的命名形参函数必须给出命名实参才可以调用。例如:27
28
<!-- compile.error -->29
30
31
如需使用,开发者可以通过 lambda 表达式传入 `f` 函数的命名实参:32
33
<!-- compile -->34
35
36
出于相同原因,当 `f` 的参数有默认值时,直接与流运算符一起使用也是错误的,例如:37
38
<!-- compile.error -->39
40
41
但是当命名形参都存在默认值时,不需要给出命名实参也可以调用该函数,函数仅需要传入非命名形参,那么这种函数是可以同流运算符一起使用的,例如:42
43
<!-- compile -->44
45
46
当然,如果想要在调用 `f` 时,为参数 `b` 传入其他参数,那么也需要借助 lambda 表达式:47
48
<!-- compile -->Code 1 · cangjie
1
func f(x: Int64): Float64 {2
Float64(x)3
}4
func g(x: Float64): Float64 {5
x6
}7
8
var fg = f ~> g // The same as { x: Int64 => g(f(x)) }Code 2 · cangjie
1
func f(x: Int64): Float64 {2
Float64(x)3
}4
5
let lambdaComp = {x: Int64 => x} ~> f // The same as { x: Int64 => f({x: Int64 => x}(x)) }Code 3 · cangjie
1
func h1<T>(x: T): T { x }2
func h2<T>(x: T): T { x }3
var hh = h1<Int64> ~> h2<Int64> // The same as { x: Int64 => h2<Int64>(h1<Int64>(x)) }Code 4 · cangjie
1
func f(a!: Int64): Unit {}2
3
var a = 1 |> f // ErrorCode 5 · cangjie
1
func f(a!: Int64): Unit {}2
3
var x = 1 |> { x: Int64 => f(a: x) } // OKCode 6 · cangjie
1
func f(a!: Int64 = 2): Unit {}2
3
var a = 1 |> f // ErrorCode 7 · cangjie
1
func f(a: Int64, b!: Int64 = 2): Unit {}2
3
var a = 1 |> f // OKCode 8 · cangjie
1
func f(a: Int64, b!: Int64 = 2): Unit {}2
3
var a = 1 |> {x: Int64 => f(x, b: 3)} // OK