计算表达式
This commit is contained in:
parent
12cce6d8b4
commit
ee20268ce2
|
@ -2,6 +2,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<LangVersion>6.0</LangVersion>
|
||||||
<RootNamespace>Basic_practice_of_FSharp</RootNamespace>
|
<RootNamespace>Basic_practice_of_FSharp</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -16,5 +17,6 @@
|
||||||
<Compile Include="Loop.fs" />
|
<Compile Include="Loop.fs" />
|
||||||
<Compile Include="TypeCreating.fs" />
|
<Compile Include="TypeCreating.fs" />
|
||||||
<Compile Include="PatternMatching.fs" />
|
<Compile Include="PatternMatching.fs" />
|
||||||
|
<Compile Include="ComputationExpressions.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -0,0 +1,104 @@
|
||||||
|
module ComputationExpressions
|
||||||
|
// 计算表达式
|
||||||
|
|
||||||
|
// 语法
|
||||||
|
// builder-expr { cexpr }
|
||||||
|
|
||||||
|
// 计算表达式类似于一种monads,monoid, monad的通用型容器
|
||||||
|
// 并且它们并不绑定到单个抽先,也不依赖于宏或其他形式的元编程来实现上下文相关语法
|
||||||
|
|
||||||
|
// F#内置计算表达式
|
||||||
|
|
||||||
|
// 序列表达式
|
||||||
|
|
||||||
|
// seq { cexpr }
|
||||||
|
|
||||||
|
// yield表示从计算表达式中返回值,以便作用于IEnumerable<T>
|
||||||
|
seq {
|
||||||
|
for i = 0 to 10 do yield i * i
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
// 也可以存在条件
|
||||||
|
|
||||||
|
let weekdays includeWeekdays =
|
||||||
|
seq {
|
||||||
|
"礼拜一"
|
||||||
|
"礼拜二"
|
||||||
|
"礼拜三"
|
||||||
|
"礼拜四"
|
||||||
|
"礼拜五"
|
||||||
|
if includeWeekdays then
|
||||||
|
"礼拜六"
|
||||||
|
"礼拜七"
|
||||||
|
}
|
||||||
|
|
||||||
|
weekdays true |> printfn "%A"
|
||||||
|
|
||||||
|
// yield!将计算平展
|
||||||
|
seq {
|
||||||
|
yield! seq { for i = 0 to 3 do yield i * i }
|
||||||
|
yield! seq { for i = 0 to 3 do yield i * i * i }
|
||||||
|
}
|
||||||
|
|> Seq.iter (printf "%A ")
|
||||||
|
printf "\n"
|
||||||
|
|
||||||
|
// 查询表达式
|
||||||
|
|
||||||
|
// query { cexpr }
|
||||||
|
|
||||||
|
query {
|
||||||
|
for i in 1..10 do
|
||||||
|
select i
|
||||||
|
contains 5 // 查询列表是否包含5,如果是返回true,反之为false
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
query {
|
||||||
|
for i in 1..10 do
|
||||||
|
select i
|
||||||
|
count // 计数
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
query {
|
||||||
|
for i in 1..10 do
|
||||||
|
last // 查询列表中最后一个元素
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
query {
|
||||||
|
for i in 1..10 do
|
||||||
|
lastOrDefault // 查询列表中最后一个元素,如果不存在,则发挥元素默认值
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
query {
|
||||||
|
for i in 1..10 do
|
||||||
|
where (i = 4) // 以指定谓词剔除元素,谓词必须使用括号
|
||||||
|
select i
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// 异步表达式
|
||||||
|
|
||||||
|
// async { cexpr }
|
||||||
|
|
||||||
|
// 任务表达式
|
||||||
|
|
||||||
|
// task { cexpr }
|
||||||
|
|
||||||
|
// 自定义计算表达式
|
||||||
|
// 所有的计算表达式都必须实现Yield方法
|
||||||
|
// 其他自定义计算使用[<CustomOpertion>]特性标记
|
||||||
|
type MyComputationBuilder() =
|
||||||
|
member this.Yield(x) = x
|
||||||
|
|
||||||
|
let mycom = MyComputationBuilder()
|
||||||
|
|
||||||
|
mycom {
|
||||||
|
yield 1
|
||||||
|
}
|
||||||
|
|> printfn "%A"
|
Loading…
Reference in New Issue