- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经为代码练习创建了一个功能性的 TypeScript 解决方案,该解决方案基于同一练习的 Haskell 解决方案。练习是取一个非常大的整数,并找出一组长度为 n 的相邻数字的最大乘积。
我编写了一个柯里化(Currying)函数 groups
,它接受一个数字数组并返回一个数组数组,每个嵌套数组由许多数字组成。
例如:
groups([1,2,3,4,5,6,7,8,9])(3)
将返回:
[[1,2,3], [4,5,6], [7,8,9]]
groups
函数:
type groups = <A>(xs: A[]) => (n: number) => A[][];
const groups: groups = xs => n => xs.length < n ? [] : [take(n)(xs)].concat(groups(drop(1)(xs))(n));
// groups uses the functions 'take' and 'drop':
type take = <A>(n: number) => (xs: A[]) => A[];
const take: take = n => xs => xs.slice(0, n);
type drop = <A>(n: number) => (xs: A[]) => A[];
const drop: drop = n => xs => xs.slice(n);
但是 groups
给出了错误:
TS2322: Type '{}[][]' is not assignable to type 'A[][]'.
但是,当我添加 as []
时,错误消失了:
const groups: groups = xs => n => xs.length < n ? [] : [take(n)(xs)].concat(groups(drop(1)(xs))(n)) as [];
我的第一个问题是:这是为什么?
我声明 groups
的返回类型是 A[][]
并且参数 xs
的类型是 A[]
那么为什么要将其解析为 {}[][]
?我不完全理解这是如何工作的。
然后,当我在 Webstorm 中使用 Quokka 运行最终解决方案时,我得到了正确的答案,但又出现了另一个错误。
一、最终解决方案:
const largestProduct = (count: number): number[] => {
const num = '73167176531330624919225119674426574742355349194934969835203127745063262395';
const digits = num.split('').map((x: string) => parseInt(x, 10));
return Math.max(...map(product)(groups(digits)(count)));
};
// the functions 'map' and 'product' used:
type map = <A, B>(f: (a: A) => B) => (xs: A[]) => B[];
const map: map = f => xs => xs.map(f);
type product = (xs: number[]) => number;
const product: product = xs => xs.reduce((acc, x) => acc * x, 1);
带有return
语句的行给出了错误:
TS2322: Type 'number' is not assignable to type 'number[]'
map
函数确实返回 number[]
但我在这里使用展开运算符。例如 Math.max(...[1,2,3])
不会给出此错误。
我在这里做错了什么?
最佳答案
问题是你的 take
和 drop
函数在外部函数上有类型参数 A
,而这个参数的推理站点(以 xs
参数的形式)在内部函数上。 Typescript 无法真正处理这个问题,它希望在检查第一次调用时确定所有类型参数(例如 take(1)
),因为它无处推断 A
从它只是将它推断为 {}
导致您的问题。
最简单的解决方案是在内部函数上移动类型参数:
type groups = <A>(xs: A[]) => (n: number) => A[][];
const groups: groups = xs => n => xs.length < n ? [] : [take(n)(xs)].concat(groups(drop(1)(xs))(n));
type take = (n: number) => <A>(xs: A[]) => A[];
const take: take = n => xs => xs.slice(0, n);
type drop = (n: number) => <A>(xs: A[]) => A[];
const drop: drop = n => xs => xs.slice(n);
顺便说一句,我不是 100% 相信你之前定义函数类型的方法。您可以完全注释箭头函数(包括返回类型):
const groups = <A>(xs: A[]) => (n: number) : A[][] => xs.length < n ? [] : [take(n)(xs)].concat(groups(drop(1)(xs))(n));
const take = (n: number) => <A>(xs: A[]) : A[]=> xs.slice(0, n);
const drop = (n: number) => <A>(xs: A[]): A[] => xs.slice(n);
尽管在这种情况下,返回类型对于take
和drop
不是严格必需的,并且可以根据返回类型推断(对于group
因为函数的递归性质)
const groups = <A>(xs: A[]) => (n: number) : A[][] => xs.length < n ? [] : [take(n)(xs)].concat(groups(drop(1)(xs))(n));
const take = (n: number) => <A>(xs: A[]) => xs.slice(0, n);
const drop = (n: number) => <A>(xs: A[]) => xs.slice(n);
我的偏好是不要编写编译器可以识别的类型。那几次我实际上想要符号的完整类型我只需要将鼠标悬停在它上面就可以知道 TS 为省略的类型推断出什么。但正如我所说,这只是一种偏好,你的方式也很好 :)
关于typescript - 函数式 TypeScript 和泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045888/
我刚刚更新了 Ruby,现在我在尝试启动 compass 时遇到以下错误: Encoding::CompatibilityError on line ["28"] of /usr/local/Cell
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在尝试在我的 iOS 应用程序中开发可折叠/ Accordion 式的功能。这将是您可以在网站上找到的典型 FAQ 类型功能。我想点击标题,然后显示详细信息。 因为这是帮助部分,只有几个项目,我认
我正在尝试设计一个基于 REST 的 Web 服务来与我正在开发的农场动物管理系统进行交互。 为了详细说明问题,我收藏了动物 属于一个农场。每只动物都有自己的信息——例如姓名、身份证号、品种年龄等。因
我有 3 种不同的表单,其中复选框数量不同,每个部分基本上代表一个表单,因此当用户选择该部分中的复选框时,它会显示他们在该部分的总金额中 checkout 了多少 HTML
我有一份 32 页的 PDF 版家谱。与其将家谱全部放在一个非常大的 PDF 页面上(这是我想要的),不如将其格式化为一组 8 个单独的美国信纸大小的页面应该在整个宽度上缝合; 4 行这样就完成了树。
指SASS implementation for Java? : 在 Maven 目标编译包中自动编译 compass-style.org 样式表的最佳方法是什么? 我不想发送太多的自编译库,也不想通
鉴于以下 XAML... 我正在寻找一种绑定(bind) ComboBox、Button 和 Command 的方法,以便当 ComboBox 的值更改时,在 Command 上调用 CanExe
在玩具应用程序中,我有一个显示所有帖子标题的“帖子”模板。当您单击每个标题时,我不想直接进入“显示” View ,而是直接内联展开该帖子的其余内容。 我考虑过让 postRoute 重用 postsR
我需要一些使用 Twitter Bootstrap 或其他响应式框架的自定义 Swagger-UI 实现。需要在我的移动设备上使用这样的 UI 测试我的 API,但 swagger-ui 不能很好地扩
我正在做一个项目,我真的在尝试编写面向对象的 JavaScript 代码。我刚刚开始阅读Douglas Crockford's JavaScript: The Good Parts我很快开始意识到用
在 C# 中,我通过执行以下操作来加密文本数据(请注意我正在以 block ( block )的形式加密数据): public string EncryptData(string pu
我正在构建一个社交网站,该网站将向全世界公开 REST API (WCF WebAPI),以便任何开发人员都能够为该网站创建客户端应用程序、将其与其他服务集成等。 我想为 API 实现 Faceboo
我是一名优秀的程序员,十分优秀!