Skip to content

Instantly share code, notes, and snippets.

@bjhaid
bjhaid / all_elixir_auto_complete.bash
Last active March 25, 2017 10:29
Bash Auto Completion for elixir
_mix()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ! -f /tmp/__mix_completion__ ]]; then
opts=$(for i in `mix help | grep -ve "current:" | grep -ve "iex" | awk '{ print $2" " }'`; do echo $i; done);
echo $opts > /tmp/__mix_completion__;
else
# split windows like vim
# vim's definition of a horizontal/vertical split is reversed from tmux's
bind s split-window -v -c "#{pane_current_path}"
bind v split-window -h -c '#{pane_current_path}'
bind ^s split-window -v -c "#{pane_current_path}"
bind ^v split-window -h -c "#{pane_current_path}"
# move around panes with hjkl, as one would in vim after pressing ctrl-w
bind h select-pane -L

works in erlang

1> Bbsl = fun(Bin,Shift) -> <<_:Shift,Rest/bits>> = Bin, <<Rest/bits,0:Shift>> end.
#Fun<erl_eval.12.50752066>

fails in elixir (1.3.4)

@bjhaid
bjhaid / service-checklist.md
Created September 23, 2016 15:11 — forked from acolyer/service-checklist.md
Internet Scale Services Checklist

Internet Scale Services Checklist

A checklist for designing and developing internet scale services, inspired by James Hamilton's 2007 paper "On Desgining and Deploying Internet-Scale Services."

Basic tenets

  • Does the design expect failures to happen regularly and handle them gracefully?
  • Have we kept things as simple as possible?
KafkaEx.create_worker(:foo, uris: Application.get_env(:kafka_ex, :brokers), consumer_group: "bar")
Enum.each(1..1000, fn x -> KafkaEx.produce("foo", 0, "bar#{x}") end)
KafkaEx.fetch("foo", 0, worker_name: :foo, max_bytes: 100)
# output
"""
[%KafkaEx.Protocol.Fetch.Response{partitions: [%{error_code: 0,
hw_mark_offset: 1002, last_offset: 2,
message_set: [%KafkaEx.Protocol.Fetch.Message{attributes: 0,
crc: 2491399380, key: "", offset: 0, value: "bar"},
%KafkaEx.Protocol.Fetch.Message{attributes: 0, crc: 702483308, key: "",
CSV.open('/tmp/sub.csv').each_with_object(Hash.new(0)) { |x,hash| hash[x[1]] += 1 }
get '/twitter/:name' do
content_type :json
sorted_influencers(params[:name])
end
def sorted_influencers(name)
# get first 2 tweets
tweets = client.user_timeline(name)[0,1]
# for each tweet, get the retweeters
arr = tweets.map do |tweet|
@bjhaid
bjhaid / gist:8306312
Last active January 2, 2016 12:49
Roman Numerals
class RomanNumerals
NUM_TO_ROMAN = { 1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD', 100 => 'C', 90 => 'XC', 50 => 'L', 40 => 'XL', 10 => 'X' , 9 => 'IX', 5 => 'V', 4 => 'IV', 1 => 'I' }
class << self
def to_roman(num)
NUM_TO_ROMAN.each_with_object("") { |(k,v), mem| mem << (v * (num / k)); num = num % k }
end
def from_roman(roman)
def fib(n)
(0..n).inject([1,0]) { |(a,b), _| [b, a+b] }[0]
end