Function 如何在Elm中部分应用具有所需顺序的函数?

Function 如何在Elm中部分应用具有所需顺序的函数?,function,elm,partial-application,Function,Elm,Partial Application,假设我有一个以3个参数作为输入的函数。如何在Elm中部分应用此函数,以获取第一个和最后一个参数,并等待第二个参数返回最终结果 这可以通过名为placeholer的R.\uuuuuu来完成,您只需将其包装在一个具有所需形状的lambda函数中即可,这是通过任何其他方式生成的: \y -> f "x" y "z" 在一种咖喱语中,我发现很少有必要这样做,因此没有必要专门为此用例添加语法糖。您可以从核心基础模块使用 例如: > append3 x y z = x ++ y ++ z &l

假设我有一个以3个参数作为输入的函数。如何在Elm中部分应用此函数,以获取第一个和最后一个参数,并等待第二个参数返回最终结果


这可以通过名为
placeholer
R.\uuuuuu
来完成,您只需将其包装在一个具有所需形状的lambda函数中即可,这是通过任何其他方式生成的:

\y -> f "x" y "z"
在一种咖喱语中,我发现很少有必要这样做,因此没有必要专门为此用例添加语法糖。

您可以从核心基础模块使用

例如:

> append3 x y z = x ++ y ++ z
<function> : appendable -> appendable -> appendable -> appendable
> hello = flip (append3 "Hello, ") "!"
<function> : String -> String
> hello "world"
"Hello, world!" : String
>附录3 x y z=x++y++z
:可追加->可追加->可追加->可追加
>hello=翻转(附录3“hello,”)“!”
:字符串->字符串
>你好,“世界”
“你好,世界!”:字符串

正如glennsl所说,您可以使用所需的参数顺序将函数包装到另一个函数中。他的回答假设你静态地知道第一个和第三个参数是什么,如果你不知道,但是只想部分地应用第一个和第三个参数,然后应用第二个,你可以取一个函数,比如

joinThree : String -> String -> String -> String
joinThree first second third =
        first ++ second ++ third
welcomeToNeverland : String -> String
welcomeToNeverland name =
    let
        myGreeting = joinThreeWrapper "Welcome " " to Neverland"
    in
        myGreeting name
并将其封装在调用第一个函数的新函数中,但参数顺序不同

joinThreeWrapper : String -> String -> String -> String
joinThreeWrapper first third second =
    joinThree first second third
这允许你调用这个函数,比如

joinThree : String -> String -> String -> String
joinThree first second third =
        first ++ second ++ third
welcomeToNeverland : String -> String
welcomeToNeverland name =
    let
        myGreeting = joinThreeWrapper "Welcome " " to Neverland"
    in
        myGreeting name
然后你可以像这样使用它

text (welcomeToNeverland "Wendy")
-- Welcome Wendy to Neverland
这样编写
jointhereewrapper
可以更容易地将函数映射到如下列表中:

greetMany : List String -> List String
greetMany names =
    List.map (joinThreeWrapper "Welcome " ", this is our town. ") names
这样你就可以

text (List.map (++) (greetMany ["Jesse", "Carl"]))
-- Welcome Jesse, this is our town. Welcome Carl, this is our town. 

这是一个很好的解决方案,参数很少。@kaskelotti你是对的。支持命名的辅助函数。