Skip to content

Instantly share code, notes, and snippets.

@austintaylor
Forked from aiwilliams/date_select.js.coffee
Created April 19, 2011 19:26
Show Gist options
  • Save austintaylor/929362 to your computer and use it in GitHub Desktop.
Save austintaylor/929362 to your computer and use it in GitHub Desktop.
CoffeeScript vs. Ruby/Haml vs. Haskell
month_names = [null,'January','February','March','April','May','June','July','August','September','October','November','December']
date_select = "<select name='born_on_month'>"
for number in [1..12]
date_select += "<option value='#{number}'>#{month_names[number]}</option>"
date_select += "</select>"
date_select += "<select name='born_on_day'>"
for number in [1..31]
date_select += "<option value='#{number}'>#{number}</option>"
date_select += "</select>"
date_select += "<select name='born_on_year'>"
y = (new Date()).get('Year')
for number in [(y-80)..y]
date_select += "<option value='#{number}'>#{number}</option>"
date_select += "</select>"
%select(name="born_on_month")
- (1..12).each do |number|
%option(value=number)= Date::MONTHNAMES[number]
%select(name="born_on_day")
- (1..31).each do |number|
%option(value=number)= number
%select(name="born_on_year")
- y = Date.today.year
- ((y-80)..y).each do |number|
%option(value=number)= number
import Text.Html
dateSelect year = dateSelect "born_on_month" [1..12] monthName
+++ dateSelect "born_on_day" [1..31] show
+++ dateSelect "born_on_year" [year-80..year] show
where monthName x = words "January February March April May June July August September October November December" !! (x - 1)
dateSelect n xs f = (select << map (dateOption f) xs) ! [name n]
dateOption f x = (option << f x) ! [value $ show x]
{-# LANGUAGE OverloadedStrings #-}
import Text.Blaze.Html5
import Text.Blaze.Html5.Attributes
import Text.Blaze.Renderer.String
dateSelect :: Int -> String
dateSelect year = renderHtml $ do
dateSelect "born_on_month" [1..12] monthName
dateSelect "born_on_day" [1..31] show
dateSelect "born_on_year" [year-80..year] show
where monthName x = words "January February March April May June July August September October November December" !! (x - 1)
dateSelect n xs f = select ! name n $ mapM_ (dateOption f) xs
dateOption f x = option ! value (toValue $ show x) $ toHtml $ f x
@aiwilliams
Copy link

Whoa, that's cool.

@austintaylor
Copy link
Author

The BlazeHtml version has a bit more to import, but I like the monadic design better, and it's quite a bit faster than Text.Html.

@aiwilliams
Copy link

Did you see the shorter Coffeescript version here: https://gist.github.com/929272/999fd467f39e45d8d0c17dd22d89dde90ed66d0d Of course, that's just a refactoring :)

@austintaylor
Copy link
Author

austintaylor commented May 2, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment