jiangzhen, I think Seq is what you are looking for Seq<T> is an alias for .NET's IEnumerable<T>, so you can use all functions in the Seq module for lists and arrays:

1
2
3
4
5
6
    let ar = [|1;2;3|]
    let ls = [ 4;5;6 ]
    let sq = { 7..9  }


    let all = sq |> Seq.append ls |> Seq.append ar

I'm not sure if this answers your question...

By on 6/1/2010 2:03 AM ()

mau, thank you replied me.

oops,i don't express fully my question,

please seem follow snippet :

let xmlDoc=new System.Xml.XmlDocument()

let root=

xmlDoc.LoadXml(<?xml version=\"1.0\"?> <jz><name>jiangzhen</name><age>22</age> </jz>")

xmlDoc.DocumentElement

root.ChildNodes

|>Seq.map (fun node-> printfn "%A" node.Value)

this made a error:The type 'System.Xml.XmlNodeList' is not compatible with the type 'seq<'a>'

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

in c#

i can typing like :

XmlDocument doc = new XmlDocument();

doc.LoadXml("<?xml version=\"1.0\"?> <jz><name>jiangzhen</name><age>22</age> </jz>");

XmlElement root = doc.DocumentElement;

foreach (XmlNode item in root.ChildNodes)

{

foreach (XmlNode item2 in item.ChildNodes)

{

Console.WriteLine(item2.Value);

}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

it will complie to like :

IEnumerator enumerator=root.ChildNodes.GetEnumerator();

while(enumerator.MoveNext())

{

XmlNode node= enumerator.Current as XmlNode;

IEnumerator enumerator2=root.ChildNodes.GetEnumerator();

while(enumerator2.MoveNext())

{

XmlNode node= enumerator2.Current as XmlNode;

Console.WriteLine(node.Value);

}

}

the XmlNodeList implement the interface IEnumerable ,so the code will running well.

now ,my new question is:

in f# what the keyword is wrapping the calling object's GetEnumerator() method,like the foreach(C#) done?

thanks

-------------------------------

jiangzhen

By on 6/1/2010 8:51 AM ()

sorry formatting...i don't know how it can recognize indentation

By on 6/1/2010 8:55 AM ()

The problem here is that XmlDocument.ChildNodes is an IEnumerable. The type seq is a generic IEnumerable<T>. It doesn't work on the non-generic enumerable.

To convert an IEnumerable to an IEnumerable<T>, you need to use seq.cast, like this:

1
2
3
root.ChildNodes
|> Seq.cast<XmlNode> //converts XmlNodeList to an IEnumerable<XmlNode>
|> Seq.iter (fun node -> printfn "node: %A" node.Name)

If you can use .Net 3.5, you could also look into XLINQ. An XElement, for instance, has a Descendants() method which gives you a strongly typed IEnumerable<>, which means that you can use seq on it without having to use seq.cast.

By on 6/2/2010 3:04 AM ()

wow

cfern,thank you!

in fact , i can use .Net4.0 ...but... i think i must spend more time for FCL.

By on 6/2/2010 3:25 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper