-
-
Save austintaylor/929362 to your computer and use it in GitHub Desktop.
CoffeeScript vs. Ruby/Haml vs. Haskell
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
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>" |
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
%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 |
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
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] |
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
{-# 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 |
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.
Did you see the shorter Coffeescript version here: https://gist.github.com/929272/999fd467f39e45d8d0c17dd22d89dde90ed66d0d Of course, that's just a refactoring :)
I'd be curious to see the same thing done using Coffeekup.
…On May 2, 2011, at 5:49 PM, aiwilliams wrote:
Did you see the shorter Coffeescript version here: https://gist.github.com/929272/999fd467f39e45d8d0c17dd22d89dde90ed66d0d Of course, that's just a refactoring :)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/929362
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Whoa, that's cool.