Skip to content

Instantly share code, notes, and snippets.

@jmnsf
jmnsf / dynamic_supervisor.exs
Created February 26, 2021 13:58
DynamicSupervisor: always creating a child vs checking first benchmark
import ExUnit.Assertions
defmodule TestChild do
use GenServer
def start_link(child_id) do
GenServer.start_link(__MODULE__, child_id, name: {:via, Registry, {TestRegistry, child_id}})
end
@impl true
@jmnsf
jmnsf / retry.sh
Created August 15, 2018 12:09
Rerun any command that exists with non-zero status.
#!/bin/sh
# Reruns any command when it exits with non-zero status.
while :
do
if eval $@ ; then
break
fi
echo "Died... Re-trying in 1 second."
@jmnsf
jmnsf / forever.sh
Created April 22, 2017 12:30
Keep any command running forever
#!/bin/sh
# Reruns any command when it exits.
while :
do
eval $@
echo "Died... Re-running in 1 second."
sleep 1
done
@jmnsf
jmnsf / ssh-tunnel-setup.md
Created April 21, 2017 04:55
SSH Tunnel Setup

SSH Tunnel Setup

Use this for protected networks where port 22 isn't available, or for general security reasons where your traffic will be routed through an external server via SSH tunnel.

Server Setup

An EC2 micro instance or a tiny DigitalOcean droplet will work for this. For the initial configuration, access to port 22 is required, as you'll have to SSH onto your host.

In the host, edit /etc/ssh/sshd_config and add the line Port 443. Reboot. From now on you can SSH into the server through port 443 (ssh -p 443 user@server).

Tunnel Setup

@jmnsf
jmnsf / marvin-annotations.rb
Last active March 19, 2017 03:43
Extract annotations from Marvin 2.0
require 'nokogiri'
if ARGV.length != 1
puts "Usage:\n$ ruby marvin-annotations.rb <path-to-library.mrvi>"
exit
end
def extract_highlights(book)
min_ts = 3000000000 # very large
max_ts = 0
@jmnsf
jmnsf / clean-git.rb
Last active March 2, 2020 12:31
Clean Git Branches
# @author jmnsf
#
# This script attempts to clean the local repository of stale branches. This
# goes in a few steps:
#
# 0. Fetch with `-p` is run to remove references to deleted remote branches.
# 1. Merged branches are removed (except for `master`).
# 2. Branches with no conflicts or changes vs `master` are removed.
# 3. Branches that have the same head as a merged PR in GitHub will be merged.
#
@jmnsf
jmnsf / 2016_table.txt
Last active April 1, 2016 13:44
Calculate Portuguese TAX in Google Sheets
Echelon Tax Overtax
€7,000.00 0.145 0
€20,000.00 0.285 0.01
€40,000.00 0.37 0.0175
€80,000.00 0.45 0.03
€99,999,999,999.00 0.48 0.035
@jmnsf
jmnsf / AfterEachEs6.sublime-snippet
Last active February 10, 2016 15:46
Sublime Text 2 Snippets
<snippet>
<content><![CDATA[
afterEach((${1:args}) => ${2:{}});
]]></content>
<tabTrigger>aea</tabTrigger>
<scope>source.js</scope>
<description>ES 6 afterEach </description>
</snippet>
ptSalary :: (Ord a, Fractional a) => a -> a
ptSalary yearly =
let levels = [((0, 7000), 0.145), ((7001, 20000), 0.285), ((20001, 40000), 0.37), ((40001, 80000), 0.45), ((80001, 250000), 0.505)]
leftVal el = fst . fst $ el
rightVal el = snd . fst $ el
pct el = snd el
reducer sal el = sal - (min (rightVal el - leftVal el) (yearly - leftVal el)) * pct el
validLevels levels = takeWhile ((< yearly) . leftVal) levels
in foldl reducer yearly (validLevels levels)
@jmnsf
jmnsf / mongo_daily_retention.rb
Created September 28, 2015 15:04
Second and Third day retention calculator from seen_at and first_seen_at in MongoDB Aggregation FW
User.disk_aggregate([
{ '$match' => { '$and' => [
{ 'attrs.first_seen_at' => { '$gte' => DateTime.parse('2015-06-01') } },
{ 'attrs.first_seen_at' => { '$lte' => DateTime.parse('2015-06-30 23:59') } }] } },
{ '$project' => {
days_seen_at: { '$map'=> {
input: '$attrs.seen_at',
as: 'date',
in: { '$dayOfYear' => '$$date' } } },
fsa: { '$dayOfYear' => '$attrs.first_seen_at' },