Skip to content

Commit

Permalink
allow for loops and yielding collections
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Feb 18, 2022
1 parent ba9f96a commit 50fbcf3
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/LitXml/ElementBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ type ElementBuilder(name: string) =

member inline _.Yield(n: #Element) = n

member inline this.Yield(b: ElementBuilder) =
member inline _.YieldFrom(ns: seq<#Element>) =
Element(fun tb ->
Seq.iter (fun (n: #Element) -> n.Invoke(tb)) ns
)

member inline _.YieldFrom(bs: seq<ElementBuilder>) =
Element(fun tb ->
bs
|> Seq.iter (fun b ->
tb.WriteStartElement(b.Name)
tb.WriteEndElement()
)
)

member inline _.Yield(b: ElementBuilder) =
Element(fun tb ->
tb.WriteStartElement(b.Name)
tb.WriteEndElement()
Expand All @@ -46,6 +60,11 @@ type ElementBuilder(name: string) =

member inline _.Delay(n: unit -> Element) = n()

member inline _.For(ns: 'T seq, ex: 'T -> Element) =
Element(fun tb ->
Seq.iter (fun n -> (ex n).Invoke(tb)) ns
)

static member WriteTo(stream : System.IO.MemoryStream, element : Element) =
let writer = XmlWriter.Create(stream)
element.Invoke(writer) |> ignore
Expand Down

0 comments on commit 50fbcf3

Please sign in to comment.