[英]Access objects in a list, perform a foreach using their properties with F#
I've got a list of objects, to make this question generic lets say I have the object of type 'myType'. 我有一个对象列表,为了使这个问题通用,可以说我有'myType'类型的对象。 myType has two properties x and y, they are both integers.
myType具有x和y两个属性,它们都是整数。 I also have a list of 'myType' objects called 'myList'.
我还有一个名为“ myList”的“ myType”对象列表。
I want to perform something on each object in this list, I'll give an example. 我想对这个列表中的每个对象执行一些操作,下面举一个例子。
override form.OnPaint e =
let g = e.Graphics in
for myType in myList do
g.FillRectangle(Brushes.Black, x, x, x, x) // instead of x I want to use the values from myType's properties.
So in C# I would simply do a 因此,在C#中,我只需做一个
foreach (myType x in myList)
{
g.FillRectangle(Brushes.Black, x.x, x.y ...etc)
}
Whereas now in F# the foreach syntax is different and I'm not sure it can access the specific objects properties like the given example in C#. 现在,在F#中,foreach语法是不同的,并且我不确定它是否可以像C#中的给定示例那样访问特定的对象属性。
Any ideas on how to do this will be greatly appreciated, keep in mind I'm new to F# and still have alot to learn so the syntax is still a bit iffy for me. 任何有关如何执行此操作的想法将不胜感激,请记住,我是F#的新手,并且仍然有很多知识需要学习,因此语法对我来说仍然有些昧。
我想你要
mylist |> Seq.iter (fun x -> g.FillRectangle(Brushes.Black,x.x,x.y,...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.