Skip to content

Instantly share code, notes, and snippets.

@Frost
Frost / and_and_and.rb
Created February 14, 2013 10:32
The difference between `&&` and `and` -- Valentines version
1.9.2-p320 :001 > roses = "red" and "blue"
=> "blue"
1.9.2-p320 :002 > violets = "red" && "blue"
=> "blue"
1.9.2-p320 :003 > roses
=> "red"
1.9.2-p320 :004 > violets
=> "blue"
@Frost
Frost / cowsay.zsh
Created April 8, 2013 12:19
Pipe terminal output (in zsh) through cowsay -n
function cowsay_everything {
if [[ ! "$BUFFER" =~ "vim|fg|%\d|clear" ]]; then
BUFFER="$BUFFER | cowsay -n"
fi
zle accept-line
}
zle -N cowsay_everything_widget cowsay_everything
function cowsay_on {
bindkey '^J' cowsay_everything_widget
@Frost
Frost / Spiral print a matrix.md
Last active January 18, 2018 16:39
Spiral print

Given a matrix

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20

Unroll it as a spiral by printing first the top row, then the last column, then the bottom row backwards, etc.

@Frost
Frost / foo.py
Created May 6, 2013 11:40
Python __slots__
class Foo(object):
__slots__ = ( "foo", "bar" )
def __init__(self):
pass
f = Foo()
f.foo = "foo"
f.bar = "bar"
f.baz = "baz"
@Frost
Frost / sum_zero.hs
Last active December 20, 2015 03:19
Check if at least two elements in a list can be summed to zero.
import Data.List
sum_zero :: (Integral a) => [a] -> Bool
sum_zero = any (==0) . map sum . filter ((>1) . length) . subsequences
@Frost
Frost / markdown.vim
Last active December 22, 2015 05:29 — forked from natesilva/markdown.vim
"
" While editing a Markdown document in Vim, preview it in the
" default browser.
"
" Author: Nate Silva
"
" To install: Place markdown.vim in ~/.vim/ftplugin or
" %USERPROFILE%\vimfiles\ftplugin.
"
" To use: While editing a Markdown file, press ',p' (comma p)
@Frost
Frost / luhn_check.rb
Created September 13, 2013 15:16
One-liner for performing luhn-check in ruby... not the prettiest code...
def check_luhn(personal_number)
personal_number.gsub(/\D/,'')[-10,10].split('').map.with_index {|c,i| i % 2 == 0 ? c.to_i * 2 : c.to_i }.join('').split('').reduce(0) {|memo,c| c.to_i + memo } % 10 == 0
end
@Frost
Frost / npm_module_crap.sh
Last active December 29, 2015 13:19
Find out what node modules among your dependencies that publish their tests to npm, and how much disk space that takes up.
# find modules that don't .npmignore their test (or tests) directories
# and how much disk space those tests take up
find node_modules -type d -name "test*" | xargs du -csh
@Frost
Frost / hash_default_values.rb
Created January 8, 2014 10:06
Default values for hashes in ruby
# So... hash.new takes an optional parameter with a default value, that's neat.
> hash = Hash.new(0)
> hash["foo"]
=> 0
> hash["bar"] += 5
=> 5
> hash
=> {"bar" => 5}

Express.js nested apps and routing

So... Say we are building this API in express.js, and it's supposed to have these routes:

/v1
   /ping
   /foo
       /bar
       /baz