简体   繁体   English

F# - 将100个对象创建为List - 最具功能性和惯用性的方式

[英]F# - creating 100 objects into a List - most functional and idiomatic way

In F# what is the most functional and idiomatic way of creating or "newing up" 100 new objects into a List. 在F#中,创建或“新建”100个新对象到List中的最有用和最常用的方法是什么。

I guess for an example we could use DateTime as an example object. 我猜一个例子我们可以使用DateTime作为示例对象。

List.init 100 (fun x -> x * 2)

Alternatively, as a list expression: 或者,作为列表表达式:

[for i in 1..100 -> new System.DateTime()]

But I think this is less idiomatic. 但我认为这不太习惯。

I would consider using 我会考虑使用

[|for i in 1..100 -> new System.DateTime() |]

since you are working with mutable data. 因为您正在使用可变数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM