unk*_*656 6 f# function pattern-matching
我有一个可能不寻常的问题,但是如何使用模式匹配匹配F#中的函数?
想象一下:
我有多个功能签名,将多次使用,如:
binary function: int -> int -> int
unary function: int -> int
boolean function: int -> int -> bool
...
Run Code Online (Sandbox Code Playgroud)
现在想象一下这个函数evaluate
,它本身就是一个函数f
.签名f
必须是上面列出的一个.我如何匹配这种情况?
我尝试了以下方法:
测试No.1:使用代表和工会:
type UnaryFunction = delegate of int -> int
type BinaryFunction = delegate of (int -> int) -> int
type BooleanFunction = delegate of (int -> int) -> bool
type Functions =
| Unary of UnaryFunction
| Binary of BinaryFunction
| Boolean of BooleanFunction
// ...
let evaluate f = // signature: Functions -> string
match f with
| Unary u ->
let test_result = u.Invoke 3
sprintf "the result of the unary function is %d" test_result
| Binary b ->
let test_result = b.Invoke 315 42
sprintf "the result of the binary function is %d" test_result
| Boolean o ->
let test_result = o.Invoke 315 42
if test_result then "yeah" else "nope"
Run Code Online (Sandbox Code Playgroud)
测试2:使用类型模式匹配和委托:
type UnaryFunction = delegate of int -> int
type BinaryFunction = delegate of (int -> int) -> int
type BooleanFunction = delegate of (int -> int) -> bool
let evaluate f =
match f with
| ?: UnaryFunction as u ->
let test_result = u.Invoke 3
sprintf "the result of the unary function is %d" test_result
| ?: BinaryFunction as b ->
let test_result = b.Invoke 315 42
sprintf "the result of the binary function is %d" test_result
| ?: BooleanFunction as o ->
let test_result = o.Invoke 315 42
if test_result then "yeah" else "nope"
| _ -> "invalid function type"
Run Code Online (Sandbox Code Playgroud)
let evaluate f =
match f with
| ?: (int -> int) as u ->
let test_result = u 3
sprintf "the result of the unary function is %d" test_result
| ?: ((int -> int) -> int) as b ->
let test_result = b 315 42
sprintf "the result of the binary function is %d" test_result
| ?: ((int -> int) -> bool) as o ->
let test_result = o 315 42
if test_result then "yeah" else "nope"
| _ -> "invalid function type"
Run Code Online (Sandbox Code Playgroud)
F#是否有特殊的函数模式匹配语法?
如果没有,为什么呢?我是否遗漏了某些东西,或者是否能够像其他任何东西一样匹配函数也很重要,因为这是一种函数式语言?
Ree*_*sey 10
而不是使用委托,只需直接使用函数定义工作:
type UnaryFunction = int -> int
type BinaryFunction = int -> int -> int
type BooleanFunction = int -> int -> bool
type Functions =
| Unary of UnaryFunction
| Binary of BinaryFunction
| Boolean of BooleanFunction
// ...
let evaluate f = // signature: Functions -> string
match f with
| Unary u ->
let test_result = u 3
sprintf "the result of the unary function is %d" test_result
| Binary b ->
let test_result = b 315 42
sprintf "the result of the binary function is %d" test_result
| Boolean o ->
let test_result = o 315 42
if test_result then "yeah" else "nope"
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您可以根据需要调用它们(如下所示,显示FSI输出):
> evaluate (Unary (fun x -> x + 3));;
val it : string = "the result of the unary function is 6"
> let someBinaryFunction x y = x * y;;
val someBinaryFunction : x:int -> y:int -> int
> Binary someBinaryFunction |> evaluate;;
val it : string = "the result of the binary function is 13230"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
501 次 |
最近记录: |