以下是我在标准库中找到的内容吗?
split transformer1 ([], []) [("Foo", 1); ("Bar", 2); ("FooBar", 3)];;
> val it : string list * int list = (["FooBar"; "Bar"; "Foo"], [3; 2; 1])
这是我的实现
let split transformer defval stream =
let rec split' s acc =
match s with
| [] -> acc
| x::xs -> split' xs (transformer x acc)
split' stream defval
let transformer1 (key, item) (akey, aitem) = (key::akey, item::aitem)
let transformer2 (key, item) (akey) = (key::akey)
let transformer3 (key, item) (a1, a2, a3) = (key::a1, item::a2, (item + 1)::a3)
split transformer1 ([], []) [("Foo", 1); ("Bar", 2); ("FooBar", 3)];;
split transformer2 ([]) [("Foo", 1); ("Bar", 2); ("FooBar", 3)];;
split transformer3 ([], [], []) [("Foo", 1); ("Bar", 2); ("FooBar", 3)];;
答案 0 :(得分:5)
据我所知,你已经彻底改造了List.fold。以下是fold
:
[("Foo", 1); ("Bar", 2); ("FooBar", 3)]
|> List.fold (fun (akey, aitem) (key, item) -> key::akey, item::aitem) ([], [])
[("Foo", 1); ("Bar", 2); ("FooBar", 3)]
|> List.fold (fun (akey) (key, item) -> key::akey) []
[("Foo", 1); ("Bar", 2); ("FooBar", 3)]
|> List.fold (fun (a1, a2, a3) (key, item) -> key::a1, item::a2, (item + 1)::a3) ([], [], [])
还有Seq.fold函数(可能是懒惰评估的)序列。