F# má tiež parádne riešenie.
Akonáhle človek raz vyskúša expresívne jazyky ako F#, Clojure, či Groovy, tak už viac nechce ísť naspäť do tuctových jazykov.
#r "nuget: FSharp.Data" open FSharp.Data open System [<Literal>] let data = """<?xml version="1.0" encoding="utf-8"?> <products> <product> <id>1</id> <name>Product A</name> <price>780</price> </product> <product> <id>2</id> <name>Product B</name> <price>1100</price> </product> <product> <id>3</id> <name>Product C</name> <price>1050</price> </product> <product> <id>4</id> <name>Product D</name> <price>950</price> </product> </products> """ type xml = XmlProvider<data> let d = xml.GetSample().Products d |> Seq.iter (fun p -> Console.WriteLine($"{p.Name} {p.Price}")) let res = d |> Seq.filter (fun p -> p.Price < 1000) for v in res do Console.WriteLine v printfn "%A" d[..2]
Výstup:
$ fsx parse.fsx Product A 780 Product B 1100 Product C 1050 Product D 950 <product> <id>1</id> <name>Product A</name> <price>780</price> </product> <product> <id>4</id> <name>Product D</name> <price>950</price> </product> [|<product> <id>1</id> <name>Product A</name> <price>780</price> </product>; <product> <id>2</id> <name>Product B</name> <price>1100</price> </product>; <product> <id>3</id> <name>Product C</name> <price>1050</price> </product>|]