By Petr at June 27, 2010 01:47
Filed Under:
Here I want to test how to post code snippets to this community. As an example I put here code from “F# Expert” book. I'm using Live Writer with GeSHi plugin for syntax highlighting.
The sample shows how using higher order functions and Linq we can easily and elegantly create XML documents.
F#, using GeSHi 1.0.8.6
open System.Xml.Linq
let xml =
"<contacts>
<contact>
<name>John Smith</name>
<phone type=\"home\">+1 626-123-4321</phone>
</contact>
</contacts>"
let doc1 = XDocument.Parse xml
let xname n = XName.op_Implicit(n)
let xdoc
(el:
seq<XElement
>) = new XDocument
(Array.
map box (Array.
ofSeq el
)) let xelem s el = new XElement(xname s, box el)
let xatt a b = new XAttribute(xname a, b) |> box
let xstr s = box s
let doc =
xdoc
[xelem "contacts"
[xelem "contact"
[(xelem "name" (xstr "John Smith"))
(xelem "phone"
[xatt "type" "home"
xstr "+1 626-123-4321"])
]
- ]
]
Parsed in 0.022 seconds at 34.35 KB/s
15d051bd-54b2-487e-a031-4db0d62240c3|0|.0