Skip to content

Instantly share code, notes, and snippets.

View tansengming's full-sized avatar
🏠
Working from home

SengMing Tan tansengming

🏠
Working from home
View GitHub Profile
# TODO: Fix this to work with addition and deletion distances
def edit_distance(word_1, word_2)
# transforms 'str' to ['s', 't', 'r']
to_array = -> (word) { word.each_char.map{|c| c} }
# xor(['s', 't', 'r'], ['s', 't', 'r']) = 0
# xor(['s', 't', 'r'], ['a', 't', 'r']) = 1
# xor(['s', 't', 'r'], ['a', 'b', 'c']) = 3
substitution_distance = -> (top, bottom) { top.zip(bottom).map{|top_node, bottom_node| top_node == bottom_node ? 0 : 1 }.inject(:+) }
@tansengming
tansengming / crackle_pop.rb
Created October 13, 2016 13:01
look it crackles and pops
#! /usr/bin/env ruby
sum = ->(top, bottom) { top.zip(bottom).map{|array| array.inject('', :+) } }
mask = ->(top, bottom) { top.zip(bottom).map{|mask_layer, bottom_layer| mask_layer.empty? ? bottom_layer : mask_layer } }
list = (1..100).to_a
crackles = list.map{|num| (num % 3) == 0 ? 'Crackle' : '' }
pops = list.map{|num| (num % 5) == 0 ? 'Pop' : '' }
# ['', '', 'Crackle', '', '' , ...]
@tansengming
tansengming / keybase.md
Created March 29, 2016 07:10
keybase.md

Keybase proof

I hereby claim:

  • I am tansengming on github.
  • I am sengming (https://keybase.io/sengming) on keybase.
  • I have a public key ASC-G2ogHeeBKrtTPbtyJ2tbgv60i6iRlo2mW_ijPCdWnAo

To claim this, I am signing this object:

<snippet>
<content><![CDATA[
binding.pry
]]></content>
<tabTrigger>pry</tabTrigger>
<scope>source.ruby</scope>
</snippet>
<!-- save in ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/ -->
@tansengming
tansengming / gallois-tsm.zsh-theme
Created March 19, 2014 02:38
merging in the the best of the gallois and fishy themes
ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[green]%}["
ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}*%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""
#Customized git status, oh-my-zsh currently does not allow render dirty status before branch
git_custom_status() {
local cb=$(current_branch)
if [ -n "$cb" ]; then
echo "$(parse_git_dirty)%{$fg_bold[yellow]%}$(work_in_progress)%{$reset_color%}$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
@tansengming
tansengming / rspec.sublime-build
Created December 26, 2013 01:24
Sublime Text Build with Spring and Rspec
// Packages/User/rspec.sublime-build
{
"cmd": ["spring", "rspec", "--format", "Fuubar", "$file"],
"working_dir": "$project_path",
"selector": "source_spec.rb"
}
@tansengming
tansengming / shared.md
Last active December 22, 2015 12:39
notes on sharing data between Rails apps

Let's say you have a rails app and you want to build a separate admin app for it. There are at least two ways to go about this:

With a shared gem made of ActiveRecord models

The Good

  • It is relatively simple to set up. You split out all you AR models into a gem. Share the gem between the app and the admin app and voila.

The Bad

require 'rubygems'
require 'active_support'
require 'pathname'
# require 'tapp'
class ToParams
attr_reader :pathname
def initialize(path)
@pathname = Pathname.new path
@tansengming
tansengming / .zshrc
Last active June 7, 2016 19:05
.zshrc
ZSH=$HOME/.oh-my-zsh
ZSH_THEME="gallois"
setopt HIST_FIND_NO_DUPS
setopt histignorealldups
alias x='exit'
alias desk='cd ~/Desktop'
alias rm='grm -v'
@tansengming
tansengming / gup.fish
Created August 28, 2012 02:47
gup.fish
function gup
if git diff-index --quiet HEAD
gup_base
else
echo 'your index is dirty'
echo 'please git stash or commit, then rerun gup'
end
end
function gup_base