I posted the following to stackoverflow yesterday:
No answers so far, so I thought I'd check here so see if anyone has any suggestions. :-)
Update 1 shows an approach which works, however there's some code duplication. Any suggestions on making it more idiomatic are welcome.