Skip to content

Instantly share code, notes, and snippets.

View liamwhite's full-sized avatar

liamwhite

View GitHub Profile
#!/usr/bin/env ruby
# Task: Fill in the lines commented 'HERE' to create a working calculator.
# Check: Check your work by executing the file after filling in the lines.
class Lexer
def initialize(input)
@input = input.gsub(/\s/, '')
@tokens = []
end
class BinaryTree
attr_accessor :data, :left, :right
def initialize(data, left, right)
@data = data
@left = left
@right = right
end
# Return a new tree with all the nodes reversed.
@liamwhite
liamwhite / gist:8bbaafc2ded5147c7890065c67f89635
Created January 24, 2019 01:38
Dump SASS/SCSS variables from a Rails app
class Sass::Tree::Visitors::Perform
def visit_variable(node)
env = @environment
env = env.global_env if node.global
if node.guarded
var = env.var(node.name)
return [] if var && !var.null?
end
val = node.expr.perform(@environment)
@liamwhite
liamwhite / 0_dcj.c
Last active September 15, 2018 00:57
Vectorized De Casteljau's algorithm
/*
* Fast Cubic Bezier evaluation.
* Vectorized C.
*/
#include <math.h>
typedef float float4 __attribute__((vector_size(16)));
typedef float float2 __attribute__((vector_size(8)));
@liamwhite
liamwhite / authentication_with_bcrypt_in_rails_4.md
Last active August 2, 2016 01:53 — forked from thebucknerlife/authentication_with_bcrypt_in_rails_4.md
Simple Authentication in Rail 4 Using Bcrypt

#Simple Authentication with Bcrypt

This tutorial is for adding authentication to a vanilla Ruby on Rails app using Bcrypt and has_secure_password.

The steps below are based on Ryan Bates's approach from Railscast #250 Authentication from Scratch (revised).

You can see the final source code here: repo. I began with a stock rails app using rails new gif_vault

##Steps

# writing to a file
f = File.open("/tmp/profile", "w")
f.truncate(0)
for file in profile.keys
f.puts "\n#{file}"
File.readlines(file).each_with_index do |line, num|
if (sample = profile[file][num+1]) and sample[0] > 0
f.printf "% 8.1fms | %s", sample[0]/1000.0, line
else
f.printf " | %s", line
module Elasticsearch
module Model
module ClassMethods
def msearch!(payload)
# Extract the search body.
body = payload.map do |source|
__index_name = source.klass.index_name
__document_type = source.klass.document_type
# Extract the actual body from each search source.
@liamwhite
liamwhite / dedupe.py
Created August 25, 2015 23:57
OpenCV-based image similarity analyzer
import cv
from optparse import OptionParser
class Simple2DAnalyser(object):
def __init__(self, imagename):
im = cv.LoadImageM(imagename, cv.CV_LOAD_IMAGE_COLOR)
cv.Smooth(im, im, cv.CV_GAUSSIAN, 3)
# Now let's get four quadrants and our total
halfX = int(round(im.cols/2))
halfY = int(round(im.rows/2))