Created
May 24, 2013 06:31
-
-
Save liammclennan/5641661 to your computer and use it in GitHub Desktop.
David's quiz
This file contains hidden or 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
_ = require 'underscore' | |
class SurveyResponse | |
constructor: (@site, @results) -> | |
default_result = | |
'csharp': 0 | |
'fsharp': 0 | |
'haskell': 0 | |
'ruby': 0 | |
'javascript': 0 | |
data = [ | |
new SurveyResponse('site1', {'csharp':3, 'fsharp': 1, 'haskell': 0}), | |
new SurveyResponse('site2', {'csharp':3, 'ruby':5}), | |
new SurveyResponse('site3', {'ruby':7, 'javascript': 4}) | |
] | |
survey_to_csv = (d) -> | |
normalize_surveys d | |
format d | |
normalize_surveys = (d) -> | |
for survey in data | |
survey.results = _.extend {}, default_result, survey.results | |
format = (d) -> | |
""" | |
site,#{(key for key,value of default_result).join()} | |
#{format_sites d} | |
""" | |
format_sites = (ss) -> | |
("#{s.site},#{value for key,value of s.results}\n" for s in ss).join('') | |
console.log survey_to_csv data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment