在F#数组初始化 [英] Array initialization in F#
本文介绍了在F#数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建和初始化基于给定的记录类型F#中的数组?
假设我要创建100 RECORD1记录的数组。
例如
类型RECORD1 = {
数值1:字符串;
值2:字符串
}让myArray的= Array.init 100?
但它出现在Array.init不允许这个,有没有办法做到这一点?
编辑补充:
当然,我可以做这样的事情:
让myArray的= [|因为我在0..99 - > {值1 =X;值2 =Y} |]
解决方案
这应该做你所需要的。希望它帮助。
键入RECORD1 = {
数值1:字符串;
值2:字符串
}让myArray的= Array.init 100(好玩点¯x - > {值1 =X;值2 =Y})
或使用泛型
让myArray的= Array.init<&RECORD1 GT; 100(好玩点¯x - > {值1 =X;值2 =Y})
How do I create and initialize an array in F# based on a given record type? Suppose I want to create an Array of 100 record1 records.
e.g.
type record1 = { value1:string; value2:string } let myArray = Array.init 100 ?
But it appears the Array.init does not allow for this, is there a way to do this?
Edited to add:
Of course I could do something like this:
let myArray = [|for i in 0..99 -> { value1="x"; value2="y" }|]
解决方案
This should do what you need. Hope it helps.
type record1 = {
value1:string;
value2:string
}
let myArray = Array.init 100 (fun x -> {value1 = "x"; value2 = "y"})
or using Generics
let myArray = Array.init<record1> 100 (fun x -> {value1 = "x"; value2 = "y"})
这篇关于在F#数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文