- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这个问题在这里已经有了答案:
Why do I have to specify parameter names in a TS function type?
(3 个回答)
去年关闭。
尝试使用 TypeScript 弄湿我的脚,但我不断遇到 trouble .安 old function resurfaced today作为练习,我很好奇是否可以将其转换为 TypeScript。到目前为止,颈部完全疼痛。
declare type Ord = number | string;
// type signature for f sucks really bad
// (f: Ord => Ord => boolean) would be really nice, if possible
// but instead I have to give names (_) for the parameters? dumb
const arrayCompare = (f: (_: Ord) => (_: Ord) => boolean) => ([x,...xs]: Ord[]) => ([y,...ys]: Ord[]): boolean => {
if (x === undefined && y === undefined)
return true;
else if (! f (x) (y))
return false;
else
return arrayCompare (f) (xs) (ys);
}
// here the names of the parameters are actually used
const eq = (x: Ord) => (y: Ord) : boolean => x === y;
// well at least it works, I guess ...
console.log(arrayCompare (eq) ([1,2,3]) ([1,2,3])); // true
console.log(arrayCompare (eq) (['a','b','c']) (['a','b','c'])); // true
所以这个问题特别是关于(见
粗体 )
const arrayCompare = (f: (_: Ord) => (_: Ord) => boolean) => ...
f
期望类型的高阶函数
Ord => Ord => boolean
但是如果我使用这种类型签名
// danger !! unnamed parameters
(f: (Ord) => (Ord) => boolean)
TypeScript 将假设
Ord
因为参数的名称和隐含的类型是
any
// what TypeScript thinks it means
(f: (Ord: any) => (Ord: any) => boolean)
当然这不是我想要的,但这就是我得到的。为了得到我真正想要的,我必须指定高阶函数的参数名称
// now it's correct
(f: (_: Ord) => (_: Ord) => boolean)
但拜托,这毫无意义。我只能访问
f
在这种情况下,不去参数
f
当我最终调用它时会绑定(bind)......
"as far as names for parameters, consider a function that takes a callback of -> (number -> number -> number) ->, so based solely on the types your options are: add, subtract, multiply, divide, power, compare of which only one makes sense, now if a callback parameter had a name add: (number -> number -> number) the choice would be obvious" – Aleksey Bykov
(number -> number -> number)
命名堆更多的函数签名。
first
, second
, mod
, min
, max
&
, |
, xor
, <<
, 和 >>
(x, y) => sqrt(sq(x) + sq(y))
(x, y) => x + x + y + y + superglobalwhocares
// this
func = (f: (number => number => number)) => ...
// not this
func = (f: (foo: number) => (bar: number) => number)) => ...
为什么?因为
f
不知道我将提供的函数的参数。
// for the record, i would never name parameters like this
// but for those that like to be descriptive, there's nothing wrong with these
const add = (addend: number) => (augend: number) => number ...
const sub = (minuend: number) => (subtrahend: number) => number ...
const divide = (dividend: number) => (divisor: number) => number ...
const mult = (multiplicand: number) => (multiplier: number) => number ...
// I could use any of these with my func
func (add ...)
func (sub ...)
func (divide ...)
func (mult ...)
我无法提供
f
的名称
func
中的参数如果我试过了!因为谁知道我将使用哪个功能?所有这些都是合适的。
// maybe the user thinks only a division function can be specified (?)
func = (f: (dividend: number) => (divisor: number) => number) => ...
dividend
和
divisor
不适合这里,因为上面列出的任何功能都适合。在
最好的我可以这样做
// provide generic name for f's parameters
func = (f: (x: number) => (y: number) => number) => ...
但那又有什么意义呢?不像
x
和
y
成为绑定(bind)标识符。和
x
和
y
不提供额外的描述——我想这让我明白了我的观点:它们并不意味着有名称或描述。
f
对我们可能使用它的方式零知识,但这并不重要;只要它有
(number => number => number)
界面,这就是我们所关心的。这是我们可以提供给
func
用户的最有用的信息。关于
f
范围。
"It would be quite confusing for function like:
foo(cb: (number, number) => (number, string) => boolean)
What does it do?" - unional
(cb: (number, number) => (number, string) => boolean))
是一个设计糟糕的函数(你能说出多少有用的混合类型四元(4元)函数?),这无关紧要。
f
无法假装知道我可以想出的无数函数的任何描述符,这些函数使用这样的签名。
_
有意义的名字?
const apply2 = (f: (_: number) => (_: number) => number) => (x: number) => (y: number): number => {
return f (x) (y)
};
const sqrt = (x: number): number => Math.sqrt(x);
const sq = (x: number): number => x * x;
const add = (addend: number) => (augend: number): number => addend + augend;
const pythag = (side1: number) => (side2: number): number => sqrt(add(sq(side1)) (sq(side2)));
console.log(apply2 (add) (3) (4)); // 7
console.log(apply2 (pythag) (3) (4)); // => 5
如果没有,您能否提出一个令人信服的论点,为什么这些名称必须出现在您的 TypeScript 签名中?
最佳答案
编写柯里化定义很困难,至少以一种可读的方式编写。
我会做的是尽可能多地提取函数声明之外的签名,如下所示:
type Ord = string | number;
type ThirdFunction = (objs: Ord[]) => boolean;
type SecondFunction = (objs: Ord[]) => ThirdFunction;
type FirstFunction = (fn: (o: Ord) => (o: Ord) => boolean) => SecondFunction;
const arrayCompare: FirstFunction = f => ([x,...xs]) => ([y,...ys]) => {
...
}
declare
你之前的
Ord
类型别名,没有必要。您可以为这些类型找到更好的名称。
boolean
这里:
const eq = (x: Ord) => (y: Ord) : boolean => x === y;
const eq = (x: Ord) => (y: Ord) => x === y;
type
来表达该函数宣言。考虑到所有因素,可读性相当不错。
type Ord = number | string;
type arrayCompareFunc = (f: (x: Ord) => (y: Ord) => boolean)
=> (xs: Ord[])
=> (ys: Ord[])
=> boolean;
const arrayCompare: arrayCompareFunc = f => ([x,...xs) => ([y,...ys) => {
...
};
关于typescript - 我是否必须在 TypeScript 中为高阶函数类型指定参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322251/
我正在使用缺少 findall 的高阶 Prolog 变体. 还有一个关于实现我们自己的问题 findall这里:Getting list of solutions in Prolog . 低效的实现
我正在尝试使用 Flow 类型创建高阶组件,但在处理返回的组件类型时遇到了问题。 最小的例子: /* @flow */ import React from 'react'; type Props =
我想抽象化传递到我的数组的 reduce() 函数中的函数,使该函数成为通用的“最强大的 Array reducer”。为此,我想在 reduce() 参数中传入不同的特定函数,以便能够指定比较标准。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
将宏名称作为其他宏的参数来模拟高阶函数是否“安全”? 即我应该注意哪里才不会搬起石头砸自己的脚? 以下是一些片段: #define foreach_even(ii, instr) for(int ii
谁能给我解释一下下面的代码是怎么回事。该函数正在接收 n 作为参数,那么 m 来自哪里?整个代码令人困惑。如果有人可以解释一下? function greaterThan(n) { retur
我有一个 list ,例如: ["Hello", "Goodbye"] 我想使用 map在名单上; 我已经成功使用 map前: f = ("example" ++) 那么: map f ["Hello
我正在尝试通过在线书籍“Learn you a Haskell”来学习一些 Haskell,并且我有一个关于高阶函数的问题。 我看到了some examples我想做一些更高级的功能,但我不知道为什么
我正在学习更深入的 redux,并且在处理高阶 reducer 时遇到一些麻烦。 我试图使用一个简单的分页示例来了解它是如何工作的。 NB:下面的代码只是 Nodejs 上下文中 redux 的一个快
高阶函数是什么呢? 高阶函数英文名叫:Higher Order function ,一个函数可以接收一个或多个函数作为输入,或者输出一个函数,至少满足上述条件之一的函数,叫做高阶函数。 前言
我写了一个小的 R 代码片段来遍历包含马尔可夫链实现的向量,并返回观察到的给定顺序的转换。具体而言,假设我们对状态空间 $\mathcal{S}$ 的 2 次转换感兴趣。最终目标是以方便的形式存储计数
如您所见,我很难表达标题中的问题。 我有一个包含 li 的 ul,它本身包含一个 ul 和它自己的 li。 我希望仅第一个 li 元素而不是第二个 ul 中的元素。 如果你看this fiddle (
我是一名优秀的程序员,十分优秀!