Created
October 20, 2011 09:21
-
-
Save mharju/1300745 to your computer and use it in GitHub Desktop.
Playing around with functional features of C# to get concise code. This style does not feel so idiomatic, though.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OpenReadCompletedEventHandler GetGenericOpenHandler<T>(ObservableCollection<T> destination, Func<XElement, IEnumerable<T>> elementQuery) | |
{ | |
return (object sender, OpenReadCompletedEventArgs e) => | |
{ | |
XElement xmlResult = this.GetXmlResult(e); | |
foreach (T result in elementQuery(xmlResult)) | |
{ | |
destination.Add(result); | |
} | |
}; | |
} | |
private XElement GetXmlResult(OpenReadCompletedEventArgs e) | |
{ | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
settings.DtdProcessing = DtdProcessing.Ignore; | |
XmlReader reader = XmlReader.Create(e.Result, settings); | |
return XElement.Load(reader); | |
} | |
// Ja sitten voi tehdä jokaiselle feedille mitä on: | |
this.LoadFeedByName("results", | |
this.GetGenericOpenHandler(Results, | |
(XElement xmlResult) => | |
{ | |
return from result in xmlResult.Descendants("game") | |
select new ResultModel | |
{ | |
Away = result.Element("team_home").Value.Trim(), | |
Home = result.Element("team_away").Value.Trim(), | |
Scores = result.Element("scores").Value.Trim(), | |
GameDate = DateTime.Parse( | |
new Func<string>(() => | |
{ | |
string[] date_parts = result.Parent.Attribute("date").Value.Trim().Split('.'); | |
return date_parts[2] + "/" + date_parts[1] + "/" + date_parts[0]; | |
})()) | |
}; | |
})); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment