Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
egonSchiele / dining.rb
Last active September 22, 2018 12:23
Dining philosophers in Ruby with Celluloid. Modified from https://gist.github.com/bugant/4984042
require 'rubygems'
require 'celluloid'
class Waiter
include Celluloid
FORK_FREE = 0
FORK_USED = 1
attr_reader :philosophers
attr_reader :forks
attr_reader :eating
@egonSchiele
egonSchiele / why.markdown
Last active July 20, 2018 01:40
Why read Grokking Algorithms?

If you have already taken a course in algorithms, why read Grokking Algorithms (manning.com/bhargava)?

If you were learning graph algorithms, which approach would you prefer:

  1. Imagine you have to take public transit from your home to your office. How do you figure out the fastest route? Use graph algorithms! OR

  2. We can choose between two standard ways to represent a graph G = (V, E): as a collection of adjacency lists or as an adjacency matrix. Either way applies to both directed and undirected graphs.

I prefer the first way: lead with lots of examples, and clear writing. The second way is an excerpt from "Introduction to Algorithms"...that's how they start their section on graph algorithms.

@egonSchiele
egonSchiele / Mario.hs
Created July 15, 2013 01:41
Lens example using Mario
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Point = Point {
_x :: Double,
_y :: Double
} deriving (Show)
data Mario = Mario { _location :: Point } deriving (Show)
@egonSchiele
egonSchiele / bench.rb
Last active November 11, 2017 10:09
Benchmarking OpenStruct alternatives
require 'benchmark'
require 'ostruct'
require 'rubygems'
require 'deep_struct'
require 'classy_struct'
require 'structure'
require 'deepopenstruct'
require 'recursive-open-struct'
require './fast_struct/lib/fast_struct'
@egonSchiele
egonSchiele / non_linear_classification.m
Created March 10, 2016 04:10
Non-linear classification example
% my training data.
% so if x > 3 || x < 7, y = 1, otherwise y = 0.
x = 1:100;
y = [0, 0, 0, 1, 1, 1, 1, zeros(1, 93)];
% instead of theta' * x, I'm trying to create
% a non-linear decision boundary.
% So instead of y = theta_0 + theta_1 * x, I use:
function result = h(x, theta)
result = sigmoid(theta(1) + theta(2) * x + theta(3) * ((x - theta(4))^2));
@egonSchiele
egonSchiele / hfsslower.d
Created March 10, 2012 19:20
copy of Brenden Gregg's hfsslower.d
#!/usr/sbin/dtrace -s
#pragma D option quiet
#pragma D option defaultargs
#pragma D option switchrate=10hz
/* from http://dtrace.org/blogs/brendan/2011/10/10/top-10-dtrace-scripts-for-mac-os-x/ */
dtrace:::BEGIN
{
@egonSchiele
egonSchiele / phan_conditional.diff
Created March 11, 2016 02:15
Add a closure for conditionals to phan
diff --git a/src/Phan/Analysis/PreOrderAnalysisVisitor.php b/src/Phan/Analysis/PreOrderAnalysisVisitor.php
index 8650cd3..feaaee4 100644
--- a/src/Phan/Analysis/PreOrderAnalysisVisitor.php
+++ b/src/Phan/Analysis/PreOrderAnalysisVisitor.php
@@ -508,11 +508,16 @@ class PreOrderAnalysisVisitor extends ScopeVisitor
*/
public function visitIfElem(Node $node) : Context
{
+ $closure_fqsen =
+ FullyQualifiedFunctionName::fromClosureInContext(
@egonSchiele
egonSchiele / linear_regression_with_fminunc.m
Created March 9, 2016 23:48
Linear regression with fminunc
x = [1000, 2000, 4000];
y = [200000, 250000, 300000];
% given a theta_0 and theta_1, this function calculates
% their cost. We don't need this function, strictly speaking...
% but it is nice to print out the costs as gradient descent iterates.
% We should see the cost go down every time the values of theta get updated.
function distance = cost(theta)
theta_0 = theta(1);
theta_1 = theta(2);
% my training data.
% so if x > 3 || x < 7, y = 1, otherwise y = 0.
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
y = [0, 0, 0, 1, 1, 1, 0, 0, 0, 0];
% instead of theta' * x, I'm trying to create
% a non-linear decision boundary.
function result = h(x, theta)
result = sigmoid(theta(1) + theta(2) * x + theta(3) * ((x - theta(4))^2));
end
@egonSchiele
egonSchiele / pretty_hive.rb
Created September 6, 2013 00:46
Display a bunch of data from a hive table in a pretty HTML table
#!/usr/bin/env ruby
require 'tempfile'
def make_row cols, style=""
"<tr style='#{style}'><td>" + cols.join("</td><td>") + "</td></tr>"
end
if ARGV.empty?
puts %{
Usage: #{$0} [file] where file has col list for a hive table, then a blank line, then some sample rows