Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created May 24, 2013 06:31
Show Gist options
  • Save liammclennan/5641661 to your computer and use it in GitHub Desktop.
Save liammclennan/5641661 to your computer and use it in GitHub Desktop.
David's quiz
_ = 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