`fun` lambda 表达式有简写语法吗?

问题描述 投票:0回答:5

我想确保我没有错过可读但简洁的 F# 语法:我使用下面的

fun
是否太冗长了?

let toCamelCase word indexes =
    let mapping i c =
        match (indexes |> List.contains i) with
        | true                      -> Char.ToUpper(c)
        | _ when Char.IsUpper(c)    -> Char.ToLower(c)
        | _                         -> c

    word |> String.mapi mapping

[
    ("fsharP", [0; 1])
    ("nAtiveinterop", [0; 6])
    ("taskbuildereXtensions", [0; 4; 11])
    ("microsoftword", [0; 9])
]
|> List.map (fun (word, indexes) -> (word, indexes) ||> toCamelCase)

另外,请告诉我上面代码中的其他地方是否可以改进

lambda f#
5个回答
5
投票

在某些函数式语言中,

uncurry
函数非常常见:

let uncurry f (a,b) = f a b

然后你可以写

|> List.map (uncurry toCamelCase)
来代替。

或者,您可以稍微简化一下您现在所拥有的:

|> List.map (fun pair -> pair ||> toCamelCase)

3
投票

想要添加一个不太关注柯里化/非柯里化的答案。这触及了其他人已经提出的一些观点,但希望更多细节会有所帮助。

关于你的问题,如果你想传入

lambda 函数
,你需要 fun 关键字。您可以通过更改函数签名来避免这种情况:

let toCamelCase (word, indexes) =
    let mapping i c =
        match (indexes |> List.contains i) with
        | true                      -> Char.ToUpper(c)
        | _ when Char.IsUpper(c)    -> Char.ToLower(c)
        | _                         -> c

    word |> String.mapi mapping

[
    ("fsharP", [0; 1])
    ("nAtiveinterop", [0; 6])
    ("taskbuildereXtensions", [0; 4; 11])
    ("microsoftword", [0; 9])
]
|> List.map toCamelCase

如果您正在使用某个函数,无论出于何种原因您都无法进行此更改,您可以创建一个中间辅助函数(这实际上是手动取消柯里化):

let toCamelCase word indexes =
    let mapping i c =
        match (indexes |> List.contains i) with
        | true                      -> Char.ToUpper(c)
        | _ when Char.IsUpper(c)    -> Char.ToLower(c)
        | _                         -> c

    word |> String.mapi mapping

let toCCHelper (word, indexes) =
    toCamelCase word indexes

[
    ("fsharP", [0; 1])
    ("nAtiveinterop", [0; 6])
    ("taskbuildereXtensions", [0; 4; 11])
    ("microsoftword", [0; 9])
]
|> List.map toCCHelper

您可以选择稍微简化您的 lambda 函数,并且不更改任何其他内容。这是有效的,因为双管道 (||>) 将为您解构元组输入:

let toCamelCase word indexes =
    let mapping i c =
        match (indexes |> List.contains i) with
        | true                      -> Char.ToUpper(c)
        | _ when Char.IsUpper(c)    -> Char.ToLower(c)
        | _                         -> c

    word |> String.mapi mapping

[
    ("fsharP", [0; 1])
    ("nAtiveinterop", [0; 6])
    ("taskbuildereXtensions", [0; 4; 11])
    ("microsoftword", [0; 9])
]
|> List.map (fun x -> x ||> toCamelCase)

还有一些括号是不需要的,就看你的喜好了。这里有一些清理以及一些逻辑更改供您考虑:

let toCamelCase (word, indexes) =
    let mapping i c =
        // You can omit parens here  if you want:
        match indexes |> List.contains i with
        // This logic might be easier to maintain, no parens needed here:
        | true  -> Char.ToUpper c
        | false -> Char.ToLower c

    word |> String.mapi mapping

// The parens here are also optional when you're putting 1 entry per line
// (1 tuple being 1 entry in this case):
[
    "fsharP", [0; 1]
    "nAtiveinterop", [0; 6]
    "taskbuildereXtensions", [0; 4; 11]
    "microsoftword", [0; 9]
]
|> List.map toCamelCase

2
投票

这是我想出的版本。我更喜欢

xs4
并且只做内联的所有事情,根本没有
map
。如果函数的名称很长,也许
xs5

在顶部,为了便于阅读,我会添加空格来创建表格视图。但这可能是一个关心可读性的老 Perl 程序员的老习惯,并且在任何其他语言中都没有广泛使用。也许所有其他语言都认为它们已经可读了?

(* Original *)
let xs1 =
    [
        "fsharP"               , [0; 1]
        "nAtiveinterop"        , [0; 6]
        "taskbuildereXtensions", [0; 4; 11]
        "microsoftword"        , [0; 9]
    ]
    |> List.map (fun (word, indexes) -> (word, indexes) ||> toCamelCase)

(* Remove useless piping *)
let xs2 =
    [
        "fsharP"               , [0; 1]
        "nAtiveinterop"        , [0; 6]
        "taskbuildereXtensions", [0; 4; 11]
        "microsoftword"        , [0; 9]
    ]
    |> List.map (fun (word, indexes) -> toCamelCase word indexes)

(* If you use piping, then like this *)
let xs3 =
    [
        "fsharP"               , [0; 1]
        "nAtiveinterop"        , [0; 6]
        "taskbuildereXtensions", [0; 4; 11]
        "microsoftword"        , [0; 9]
    ]
    |> List.map (fun wi -> wi ||> toCamelCase)

(* toCamelCase part of list *)
let xs4 = [
    toCamelCase "fsharP"                [0; 1]
    toCamelCase "nAtiveinterop"         [0; 6]
    toCamelCase "taskbuildereXtensions" [0; 4; 11]
    toCamelCase "microsoftword"         [0; 9]
]

(* you can create a shortcut for the function *)
let xs5 =
    let f = toCamelCase
    [
        f "fsharP"                [0; 1]
        f "nAtiveinterop"         [0; 6]
        f "taskbuildereXtensions" [0; 4; 11]
        f "microsoftword"         [0; 9]
    ]

(* Use map2 *)
let xs6 =
    List.map2
        toCamelCase
        ["fsharP";"nAtiveinterop";"taskbuildereXtensions";"microsoftword"]
        [[0;1];   [0;6];          [0;4;11];               [0;9]]

(* still map2, but extracted arguments *)
let xs7 =
    let args1 = ["fsharP";"nAtiveinterop";"taskbuildereXtensions";"microsoftword"]
    let args2 = [[0;1];   [0;6];          [0;4;11];               [0;9]]
    List.map2 toCamelCase args1 args2

0
投票

您可以使用柯里化来应用函数,而无需显式声明 lambda:

let inline flip f x y = f y x
...
List.map (flip (||>) toCamelCase)

或者更改

toCamelCase
或输入列表中参数的顺序,这样您就不需要定义
flip
:

List.map ((||>) toCamelCase)

但我认为最好的选择就是避免

||>
:

List.map (fun (word, indexes) -> toCamelCase word indexes)

0
投票

F# 8 具有简写 lambda 语法:

[ { x = 5}; {x = 6} ] |> List.map _.x

相关 GitHub 问题:https://github.com/fsharp/fslang-suggestions/issues/506

© www.soinside.com 2019 - 2024. All rights reserved.