Skip to content

Instantly share code, notes, and snippets.

View austintaylor's full-sized avatar

Austin Taylor austintaylor

View GitHub Profile
@austintaylor
austintaylor / EvalMath.hs
Created May 13, 2011 14:28
Haskell module example
import ShuntingYard
data Result = I Int | B Bool deriving (Eq)
instance Show Result where
show (I x) = show x
show (B x) = show x
evalMath :: String -> Result
evalMath = rpn . shuntingYard . tokenize
@austintaylor
austintaylor / .gitconfig
Created May 20, 2011 19:25
My todo file hack
[core]
excludesfile = /Users/austin/.gitignore
@austintaylor
austintaylor / gist:1173652
Created August 26, 2011 15:23
Bash function to show your local IP address
function ip() {
ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{ print $2 }'
}
@austintaylor
austintaylor / either.hs
Created January 10, 2012 21:57
Needing the `Either a` monad in Ruby
loadStuff :: Map -> Either String (ThingOne, ThingTwo)
loadStuff params = do
thingOne <- loadThingOne params
thingTwo <- loadThingTwo params thingOne
checkSomething thingTwo
return (thingOne, thingTwo)
@austintaylor
austintaylor / For comparison: profile.erb
Created March 15, 2012 15:19
A different approach to HTML templating
<h1><%= _('Profile') %></h1>
<%= form_for @profile, :url => user_profile_path(@profile), :html => {:class => 'profile'} do |f| %>
<%= render 'shared/errors', :model => @profile %>
<dl>
<dt><%= f.label :name, _('Name') %></dt>
<dd><%= f.text_field :name %></dt>
</dl>
<dl>
<dt><%= f.label :email, _('Email') %></dt>
<dd><%= f.text_field :email %></dt>

(from Haskell and Yesod by Michael Snoyman)

However, there is one issue that newcomers are often bothered by: why are IDs and values completely separate? It seems like it would be very logical to embed the ID inside the value. In other words, instead of having:

data Person = Person { name :: String }

have

@austintaylor
austintaylor / gist:2790526
Last active October 5, 2015 10:07
Valid ruby syntax
1.9.3p0 :001 > % % % % %
=> "%"
1.9.3p0 :002 > % % % % % % % % % % %
=> "%"
1.9.3p0 :003 > !?!??!:?!
(irb):70: warning: string literal in condition
=> "!"
1.9.3p0 :004 > % . . * 0
=> ""
1.9.3p0 :005 > 0_0
@austintaylor
austintaylor / readme.md
Created July 5, 2012 21:55
An example of creating a UserVoice SSO token using cryptojs

So I had this situation where I needed to delete an object from the database, if it existed. This is what I wrote:

Ruby Option 1

obj = get_object(1)
obj.destroy if obj

And as I wrote this, it seemed a bit awkward to me. We always said Ruby had a really elegant syntax, but I guess I've come to the place where I find it a little grating. The thing about this example is the asymmetry of it. Conceptually I feel like the if check goes with the assignment, but it's separated from it in a strange way.

Ruby Option 2

class CLI
def process(input)
if input == "q"
puts "Goodbye"
elsif input == "tweet"
puts "tweeting"
elsif input == "dm"
puts "direct messaging"
elsif input == "help"
puts "helping"