有没有类似用Transformer解压的List Module Function
Is there a List Module Function that is like unzip with a Transformer
我会在标准库中找到以下内容吗?
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)];;
据我所知,您重新发明了 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 函数用于(可能延迟计算的)序列。
我会在标准库中找到以下内容吗?
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)];;
据我所知,您重新发明了 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 函数用于(可能延迟计算的)序列。