Created
May 24, 2013 07:15
-
-
Save ptrelford/5641811 to your computer and use it in GitHub Desktop.
Survey CSV exercise
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
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