Dan*_*iel 10
您所做的只是分组 - 每次遇到值时都会创建一个新组.
let splitBy f input =
let i = ref 0
input
|> Seq.map (fun x ->
if f x then incr i
!i, x)
|> Seq.groupBy fst
|> Seq.map (fun (_, b) -> Seq.map snd b)
Run Code Online (Sandbox Code Playgroud)
let items = seq [1;2;3;4;1;5;6;7;1;9]
items |> splitBy ((=) 1)
Run Code Online (Sandbox Code Playgroud)
同样,更短,斯蒂芬的改进很好:
let splitBy f input =
let i = ref 0
input
|> Seq.groupBy (fun x ->
if f x then incr i
!i)
|> Seq.map snd
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2219 次 |
最近记录: |