在F#中将字符串列表转换为浮点数/整数

栏目: ASP.NET · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/20825237/converting-a-list-of-strings-into-floats-ints-in-f

是否有一种快速简单的方法将整个字符串列表转换为浮点数或整数

并在F#中将它们添加到一起?

foreach(string s in list)
{
    sum += int.Parse(s);
}

这样的东西应该具有相同的效果:

let sum = list |> Seq.map System.Int32.Parse |> Seq.sum

F#似乎不支持引用int上的方法,所以我不得不使用System.Int32.

在F#中,seq类型是.NET IEnumerable的别名,因此此代码适用于数组,列表等.

注意在“无点”样式中使用Parse – 没有参数的函数可以直接用作期望该类型的另一个函数的参数.在这种情况下,Seq.map具有以下类型:

('a -> 'b) -> seq<'a> -> seq<'b>

因为System.Int32.Parse有类型字符串 – > int,Seq.map System.Int32.Parse的类型为seq<string> – > SEQ< INT取代.

翻译自:https://stackoverflow.com/questions/20825237/converting-a-list-of-strings-into-floats-ints-in-f


以上所述就是小编给大家介绍的《在F#中将字符串列表转换为浮点数/整数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Coming of Age in Second Life

Coming of Age in Second Life

Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95

The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具