Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
jwhiteman / pipe.rb
Created November 18, 2016 07:55
Simple pipeline using Threads and Queues
require "thread"
stage_one = Queue.new
stage_two = Queue.new
stage_three = Queue.new
stage_one_worker = Thread.new do
100.times do |n|
stage_one << n
@jwhiteman
jwhiteman / binary_tree.exs
Created January 22, 2016 02:45
invert a binary tree in Elixir
# http://is.gd/NzZxYT
defmodule BinaryTree do
def invert([n, list]) when is_integer(n) and is_list(list) do
[n, invert(list)]
end
def invert([n, nc, m, mc])
when is_integer(n) and is_integer(m)
and is_list(nc) and is_list(mc) do
@jwhiteman
jwhiteman / gist:7634421
Last active December 29, 2015 07:09
notes on getting hadoop (both single node and a cluster) going on Ubuntu LTS; there are just notes to myself and have some gaps in them. Not meant to be a tutorial or a walkthrough.
sudo apt-get update
sudo apt-get install vim
# generate keys and copy them over
ssh-copy-id -i id_rsa.pub vagrant@hnclient # move node ssh key to the client
# update /etc/hosts on each node to know at least the master; the master about the slave nodes
# make sure master can ssh into localost
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
# re-read: http://stackoverflow.com/questions/8872807/hadoop-datanodes-cannot-find-namenode
@jwhiteman
jwhiteman / example.ex
Last active December 11, 2015 20:29
Elixir vs Ruby (pattern matching vs conditional logic)
# elixir
defmodule Example do
def work(_msg) do
IO.puts "called 1st function"
end
def work(_msg, data=[_a, _b, _c]) do
IO.puts "called 2nd function #{inspect data}"
end
@jwhiteman
jwhiteman / gist:4229ed6ecdea3ea71be8
Last active August 29, 2015 14:26
Exporting PDF with DocRaptor
curl -H "Content-Type:application/json" -d'{"user_credentials":"YOUR_CREDENTIALS_HERE", "doc":{"name":"kitabu.pdf", "document_type":"pdf", "test":"false", "document_url":"https://dl.dropboxusercontent.com/u/123456789/output/kitabu.pdf.html"}}' http://docraptor.com/docs > kitabu.pdf
@jwhiteman
jwhiteman / basics-with-letrec.scm
Last active August 29, 2015 14:23
map & fold explorations
;; letrec practice
;; fold
;; map
;; filter
;; mapcat
(define (fold fold-f acc l)
(letrec
((FOLD (lambda (acc l)
(cond
@jwhiteman
jwhiteman / ring.exs
Created June 18, 2015 20:55
elixir ring
defmodule Ring do
def node(name, next_pid) do
receive do
1 ->
send next_pid, 1
n when is_number(n) ->
send next_pid, n
node(name, next_pid)
end
end
@jwhiteman
jwhiteman / list_parser.yrl
Created June 13, 2015 01:50
List Parser
% Andrea Leopardi's list parser
% http://andrealeopardi.com/posts/tokenizing-and-parsing-in-elixir-using-leex-and-yecc/
Nonterminals list elems elem.
Terminals '[' ']' ',' int atom.
Rootsymbol list.
list ->
'[' ']' : [].
list ->
@jwhiteman
jwhiteman / ch10-ex6.ex
Last active August 29, 2015 14:22
#flatten in Elixir
defmodule MyList do
# version 1: continuation passing style
def flatten(l), do: flatten(l, fn (result) -> result end)
def flatten([], col), do: col.([])
def flatten([h|t], col) when not is_list(h) do
flatten(t, fn (acc) -> col.([h | acc]) end)
end
def flatten([h|t], col) do
col.(
def daemonize_app
if RUBY_VERSION < "1.9"
exit if fork # jettison the terminal
Process.setsid # establish a new group & session leader (can only be done as a child process)
exit if fork # jettison the group leader. now it can't be interrupted from another group or session going down.
Dir.chdir "/" # change dir to root to make sure that the dir won't go away during the process run
# as a demon we won't be needing these...
STDIN.reopen "/dev/null"
STDOUT.reopen "/dev/null", 'a'