Skip to content

Instantly share code, notes, and snippets.

@bjhaid
bjhaid / bst.erl
Created July 9, 2014 04:27
Binary Search Tree in Erlang
-module(bst).
-behaviour(gen_server).
-export([start_link/0, insert/1, search/1, traverse/1]).
-export([init/1, code_change/3, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-record(bstNode, {key = nil, value=nil, leftNode = nil, rightNode = nil}).
-define(Name, bst).
init(_Arg) -> {ok,#bstNode{}}.
code_change(_, _, _) -> ok.
@bjhaid
bjhaid / gist:2df85cd15c447b8ee013
Created June 19, 2014 01:32
Fifa world cup results parsed into JSON
require 'nokogiri'
require 'open-uri'
require 'json'
doc = Nokogiri::HTML(open('http://www.fifa.com/worldcup/matches/index.html'))
matches = doc.css('.mu').map do |row|
match = {}
match[:home_team_name] = row.css('.t.home').css('.t-nText').first.content
match[:home_team_code] = row.css('.t.home').css('.t-nTri').first.content
class Proc
#This defines a method that can allow the case equality === http://www.ruby-doc.org/core-2.1.1/Proc.html#method-i-3D-3D-3D be called on 2 proc objects
def &o
->(a) { call(a) && o[a] }
end
end
def multiple_of(x)
#A partial function http://en.wikipedia.org/wiki/Partial_function
->(a) { a % x == 0 }
@bjhaid
bjhaid / sudoku.rb
Last active August 29, 2015 14:01
Sudoku board, with validator logic in 267 characters reduce to 208 characters
A=Array;d=A.new(9){A.new(9,"_")};loop{a,b,c=gets.split(":").map(&:to_i);next(p"invalid")if(d.map{|x|x[b]}+d[a-a%3,3].flat_map{|x|x[b-b%3,3]}+d[a]).include?c;d[a][b]=c;puts d.map{|x|x.join" "}}

tmux cheatsheet

As configured in my dotfiles.

start new:

tmux

start new with session name:

@bjhaid
bjhaid / fact.clj
Created April 7, 2014 01:12
tail call optimization (factorial)
(defn fact ([n] (fact n 1))
([n mul] (if (< n 1) mul (fact (dec n) (*' n mul)))))
def valid?(tic)
tac = *tic
3.times { |i| tac.push((0..2).map { |x| tic[x][i] }) }
tac.push((0..2).map { |x| tic[x][x] }).push((0..2).map { |x| tic[tic.size - 1 - x][x] })
(tac.select { |x| x.uniq.size == 1 && x.first != :e }).first
end
@bjhaid
bjhaid / tic_tac.clj
Created March 15, 2014 02:37
tic_tac solve in clojure
(fn [tic_tac]
(first (first (filter #(and (apply = %) (not (.contains % :e)))
((fn [tic_tac] (conj (apply conj tic_tac
((fn [tic_tac] (vec (map (fn[x] (vec (map #((tic_tac %) x) (range 3)))) (range 3)))) tic_tac))
((fn [tic_tac] (vec (map #((tic_tac %) %) (range 3)))) tic_tac)
((fn [tic_tac] (vec (map #((tic_tac (- 2 %)) %) (range 3)))) tic_tac))) tic_tac))))
)
@bjhaid
bjhaid / fizzbuzz.clj
Last active August 29, 2015 13:56
fizzbuzz in clojure
(map #(cond
(= 0 (mod % 15)) "FizzBuzz"
(= 0 (mod % 5)) "Buzz"
(= 0 (mod % 3)) "Fizz"
:else %)
(range 1 101))
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }