我需要一个可以接受任意数量参数的函数,每个参数可以是'T或seq<'T> 。 在函数内部,我需要将其作为单个seq<'T> ,所有输入的组合顺序与它们提供的顺序相同。
显而易见的方法是:
module Test = let flatten ([<ParamArray>] args) = let flat = seq { for a in args do match box a with | :? int as x -> yield x | :? seq<int> as sq -> for s in sq do yield s | _ -> failwith "wrong input type" } flat // this should be seq<int>但即使是最简单的情况,我也无法在FSI中发挥作用
let fl = Test.flatten 1;; ----------------------^ ...: error FS0001: The type 'int' is not compatible with the type 'seq<'a>'这里有什么问题以及如何根据需要使其工作? 可能这可以用一些完全不同的方式完成吗?
I need a function that could take an arbitrary number of arguments, each could be either of type 'T or seq<'T>. Inside the function I need to process it as a single seq<'T> with all inputs combined in the same order as they sere supplied.
The obvious way was to have something like:
module Test = let flatten ([<ParamArray>] args) = let flat = seq { for a in args do match box a with | :? int as x -> yield x | :? seq<int> as sq -> for s in sq do yield s | _ -> failwith "wrong input type" } flat // this should be seq<int>but I cannot make it work in FSI even with the simplest case
let fl = Test.flatten 1;; ----------------------^ ...: error FS0001: The type 'int' is not compatible with the type 'seq<'a>'What is wrong here and how to get it work as needed? Probably this could be done in some completely different way?
最满意答案
来自msdn :
在F#中,参数数组只能在方法中定义。 它们不能用于模块中定义的独立功能或功能。
因此,使用静态方法声明类型而不是模块。
open System type Test() = static member flatten ([<ParamArray>] args: obj[]) = let flat = seq { for a in args do match box a with | :? int as x -> yield x | :? seq<int> as sq -> for s in sq do yield s | _ -> failwith "wrong input type" } flat如果您有其他let绑定,您仍然可以声明具有相同名称的模块。 另请注意,在比赛的第二个后卫中,您可以通过执行以下操作来避免for循环:
| :? seq<int> as sq -> yield! sq并且不需要box 。
From msdn :
In F#, parameter arrays can only be defined in methods. They cannot be used in standalone functions or functions that are defined in modules.
So instead of a module, declare a type with a static method.
open System type Test() = static member flatten ([<ParamArray>] args: obj[]) = let flat = seq { for a in args do match box a with | :? int as x -> yield x | :? seq<int> as sq -> for s in sq do yield s | _ -> failwith "wrong input type" } flatIf you have other let bindings you can still declare a module with the same name. Also note that in the second guard of the match you can avoid the for loop by doing:
| :? seq<int> as sq -> yield! sqAnd box is not required.
更多推荐
发布评论