小编Свя*_*лав的帖子

F#如何避免两次投射

情况1

  1. 从序列中获取价值
  2. 打印一些值
let a = seq { yield Some 1; yield Some 2; yield Some 3; yield None }

a
|> Seq.takeWhile Option.isSome // cast 1
|> Seq.map Option.get          // cast 2
|> Seq.iter (printfn "%A")
Run Code Online (Sandbox Code Playgroud)

情况二

  1. 一些值的过滤序列
  2. 打印某些值
a
|> Seq.filter Option.isSome    // cast 1
|> Seq.map Option.get          // cast 2
|> Seq.iter (printfn "%A")
Run Code Online (Sandbox Code Playgroud)

情况3

  1. 按类型按元素分组
  2. 打印每组的值
type AB =
    | A of a : int
    | B of b : string

let a = seq{
    yield …
Run Code Online (Sandbox Code Playgroud)

f# casting pattern-matching seq

0
推荐指数
1
解决办法
110
查看次数

标签 统计

casting ×1

f# ×1

pattern-matching ×1

seq ×1