如何使我的函数通用

发布于 2024-10-02 19:19:37 字数 845 浏览 7 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-10-09 19:19:37

您的 listIntoJsonArray 和 listIntoJsonObject 是值,而不是函数,因此您遇到了 价值限制怪物。您可以通过添加显式参数将它们转换为函数,也可以将它们转换为类型函数。

// function case

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 listIntoJsonObject x = csvList "{" "}" x

let ints = [1;2;4]

let i = listIntoJsonObject [1;2;4] string 
let y = listIntoJsonObject ["123"] id


// type function case

let csvList<'T> startDelim endDelim (lst : 'T list) memF = 
    let listIter (listStr: string) item = 
        if listStr.Length > 0 then 
            listStr + "," + (memF item)
        else 
            memF item                
    startDelim + (List.fold listIter "" lst) + endDelim

[<GeneralizableValue>]
let listIntoJsonObject<'T> = csvList<'T> "{" "}"

let ints = [1;2;4]

let i = listIntoJsonObject [1;2;4] string 
let y = listIntoJsonObject ["123"] id

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.

// function case

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 listIntoJsonObject x = csvList "{" "}" x

let ints = [1;2;4]

let i = listIntoJsonObject [1;2;4] string 
let y = listIntoJsonObject ["123"] id


// type function case

let csvList<'T> startDelim endDelim (lst : 'T list) memF = 
    let listIter (listStr: string) item = 
        if listStr.Length > 0 then 
            listStr + "," + (memF item)
        else 
            memF item                
    startDelim + (List.fold listIter "" lst) + endDelim

[<GeneralizableValue>]
let listIntoJsonObject<'T> = csvList<'T> "{" "}"

let ints = [1;2;4]

let i = listIntoJsonObject [1;2;4] string 
let y = listIntoJsonObject ["123"] id
小伙你站住 2024-10-09 19:19:37

我还没有尝试过这些代码,但一般来说,eta-conversion(例如更改

let myFunVal = partialApplication toSomeArgs

为)

let myFun rest = partialApplication toSomeArgs rest

可能会解决此问题。

I haven't tried the code, but in general, eta-conversion, e.g. changing

let myFunVal = partialApplication toSomeArgs

to

let myFun rest = partialApplication toSomeArgs rest

is likely to fix this.

静若繁花 2024-10-09 19:19:37

csvList 看起来足够通用:string ->字符串 -> '列表-> ('a -> 字符串) -> string

但您在尝试部分应用时遇到了值限制listIntoJsonArraylistIntoJsonObject;您需要添加显式参数:

let listIntoJsonArray lst memF = csvList "[" "]" lst memF
let listIntoJsonObject lst memF = csvList "{" "}" lst memF

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 and listIntoJsonObject; you need to add explicit parameters:

let listIntoJsonArray lst memF = csvList "[" "]" lst memF
let listIntoJsonObject lst memF = csvList "{" "}" lst memF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文