Skip to content

Instantly share code, notes, and snippets.

@ptrelford
Created May 24, 2013 07:15
Show Gist options
  • Save ptrelford/5641811 to your computer and use it in GitHub Desktop.
Save ptrelford/5641811 to your computer and use it in GitHub Desktop.
Survey CSV exercise
open Microsoft.FSharp.Reflection
open System.Collections.Generic
type ProgrammingLanguage =
| CSharp
| FSharp
| Haskell
| Ruby
| JavaScript
let siteResponses = [
"site1", dict [ CSharp, 3; FSharp, 1; Haskell, 0 ]
"site2", dict [ CSharp, 3; Ruby, 5 ]
"site3", dict [ Ruby, 7; JavaScript, 4 ]
]
let cases = FSharpType.GetUnionCases(typeof<ProgrammingLanguage>)
let names = [for case in cases -> case.Name]
"Site"::names |> String.concat "," |> printfn "%s"
let languages = [for case in cases -> FSharpValue.MakeUnion(case,[||]) :?> ProgrammingLanguage]
for site, responses in siteResponses do
[for language in languages ->
match responses.TryGetValue(language) with
| (true,n) -> n.ToString()
| (false,_) -> "0"
]
|> fun values ->
site::values |> String.concat "," |> printfn "%s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment