如何使我的函数通用
let csvList startDelim endDelim lst memF =
let listIter (listStr: string) item =
if listStr.Length > 0 then
listStr + "," + (memF item)
else
memF item
startDelim + (List.fold listIter "" lst) + endDelim
let listIntoJsonArray = csvList "[" "]"
let listIntoJsonObject = csvList "{" "}"
let intConverter (item:int) : string =
item.ToString()
let objectConverter (item:SomeObject) : string =
item.value.ToString()
let objects = [{vaue: 12.3}; {vaule: 33.2}]
let ints = [1;2;4]
let o = listIntoJsonObject objects objectConverter
let i = listIntoJsonObject ints intConverter
我似乎找不到使 csvList 或其部分应用的帮助器 listIntoJsonArray 或 listIntoJsonObject 通用的神奇方法。
感谢您的帮助。
let csvList startDelim endDelim lst memF =
let listIter (listStr: string) item =
if listStr.Length > 0 then
listStr + "," + (memF item)
else
memF item
startDelim + (List.fold listIter "" lst) + endDelim
let listIntoJsonArray = csvList "[" "]"
let listIntoJsonObject = csvList "{" "}"
let intConverter (item:int) : string =
item.ToString()
let objectConverter (item:SomeObject) : string =
item.value.ToString()
let objects = [{vaue: 12.3}; {vaule: 33.2}]
let ints = [1;2;4]
let o = listIntoJsonObject objects objectConverter
let i = listIntoJsonObject ints intConverter
I can't seem to find the magic sauce to make the csvList or its partially applied helpers listIntoJsonArray or listIntoJsonObject generic.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 listIntoJsonArray 和 listIntoJsonObject 是值,而不是函数,因此您遇到了 价值限制怪物。您可以通过添加显式参数将它们转换为函数,也可以将它们转换为类型函数。
Your listIntoJsonArray and listIntoJsonObject are values, not functions, so you met value restriction monster. You can either convert them to functions by adding explicit argument or to type functions.
我还没有尝试过这些代码,但一般来说,eta-conversion(例如更改
为)
可能会解决此问题。
I haven't tried the code, but in general, eta-conversion, e.g. changing
to
is likely to fix this.
csvList
看起来足够通用:string ->字符串 -> '列表-> ('a -> 字符串) -> string
但您在尝试部分应用时遇到了值限制与
listIntoJsonArray
和listIntoJsonObject
;您需要添加显式参数:csvList
looks generic enough:string -> string -> 'a list -> ('a -> string) -> string
But you've encountered Value Restriction in your attempt at partial application with
listIntoJsonArray
andlistIntoJsonObject
; you need to add explicit parameters: