我应该什么时候以咖啡形式写我的功能? [英] When should I write my functions in curried form?

查看:99
本文介绍了我应该什么时候以咖啡形式写我的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习F#并且遇到了一些简单的例子,如下所示:

I've recently started learning F# and come across curried functions for simple examples such as the following:

考虑一个函数,通过乘以价格 p 按单位出售数量 n

Consider a function that calculates sales by multiplying price p by number of units sold n.

let sales (p,n) = p * (float n);;

该函数的类型如下:

The type of this function is given as

val sales : p:float * n:int -> float

即取一对 float int 并返回一个 float

i.e. take a pair of float and int and returns a float.

我们可以把它写成一个咖喱函数

We can instead write this as a curried function

let salesHi p n = p * (float n);;

该函数的类型如下:

The type of this function is given as

val salesHi : p:float -> n:int -> float

即使用 float 并返回 int float的函数

在简单的情况下,这似乎没有任何区别

In simple cases this seems to make no difference

sales (0.99, 100);;
salesHi 0.99 100;;

两者均为

Both give

val it : float = 99.0

然而,使用curried函数我可以输入特定项目的价格以获得新功能。例如:

However with the curried function I can feed in the price for particular items to get new functions. E.g.

let salesBeer =  salesHi 5.99;;
let salesWine =  salesHi 14.99;;

然后 salesBeer 2 给出 11.98 salesWine 2 给出 29.98

另外,我注意到诸如 + 之类的内置运算符被定义为函数,所以我可以编写,例如:

Also, I've noticed that built-in operators such as + are defined as functions, so I can write, for example:

let plus2 = (+) 2;
List.map plus2 [1;3;-1];;

并获得

and get

val it : int list = [3; 5; 1]

这看起来像一件好东西。所以当我想用一个命令式语言来实现一个函数时,它会采用 n> 1 参数,我应该例如总是在F#中使用curried函数(只要参数是独立的)?或者我应该采取简单的路线,并在必要时使用 -n -tuple和咖喱的常规功能?或者别的什么?

This seems like a good thing. So when I want to implement a function in an imperative language that would have taken n > 1 arguments, should I for example always use a curried function in F# (so long as the arguments are independent)? Or should I take the simple route and use regular function with an n-tuple and curry later on if necessary? Or something else?

F#程序员如何决定何时以curry形式创建函数或使用带元组的常规函数​​?

How do F# programmers decide when to make a function in curried form or use a regular function with a tuple?

推荐答案

当您在curried和tupled表单之间进行选择时,需要考虑的主要问题是您将作为参数使用的元组是否意味着 em>任何东西。

When you're choosing between curried and tupled form, the main thing to consider is whether the tuple that you'd take as an argument means anything.

元组形式。例如, float * float

let normalizeRange (lo, hi) = if hi < lo then (hi, lo) else (lo, hi)
let expandRange by (lo, hi) = (lo - by, hi + by)

关于这一点的好处是,您可以编写适用于范围的函数。例如,您可以写下如下内容:

The good thing about this is that you can then compose functions that work on ranges. You can for example write something like:

randomRange() |> normalizeRange |> expandRange 10

咖喱形式另一方面,咖喱形式是如果所有参数的元组不是具有一些有用含义的独立值,则为更好的选择。例如,幂函数 pown 2.0 10 - 这两个参数是指数和指数的数字,但不太可能使用元组你的程序中的其他地方。

Curried form. On the other hand, the curried form is a better choice if the tuple of all arguments is not a stand-alone value with some useful meaning. For example, the power function pown 2.0 10 - the two arguments are the number to power and the exponent, but it is unlikely that you'd ever use the tuple (2.0, 10) somewhere else in your program.

当你有一个更重要的参数,因为那样你可以使用流水线。例如, List.map 必须被curry以允许:

The curried form is also useful when you have one "more important" argument, because then you can use pipelining. For example, List.map has to be curried to allow this:

[1 .. 10] |> List.map (fun n -> n + 1)

这篇关于我应该什么时候以咖啡形式写我的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆