Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
jadeallenx / ngram.erl
Created June 26, 2017 22:41
ngram builder for Erlang
-module(ngram).
-compile([export_all]).
ngram(N, Str) ->
make_ngram(N, Str, 1, orddict:new()).
make_ngram(_N, Str, Pos, Acc) when Pos >= length(Str) -> Acc;
make_ngram(N, Str, Pos, Acc) ->
make_ngram(N, Str, Pos+1, orddict:update_counter(lists:sublist(Str, Pos, N), 1, Acc)).
@jadeallenx
jadeallenx / build.md
Last active June 10, 2021 06:38
Building Erlang on WSL/Ubuntu 16.04(LTS)

Building Erlang on Windows Subsystem for Linux (WSL)

WSL Overview

WSL is a driver shim for Windows 10 that enables you to run native Linux code on Windows without recompiling or running a Docker/VM. It currently supports Ubuntu 16.04 LTS - but more distribution support has been announced.

Building Erlang

From a "clean" install of Ubuntu such as the kind you would get from a fresh install of WSL, you need to preinstall these debs to enable kerl to compile a working Erlang:

@jadeallenx
jadeallenx / kata.erl
Created February 8, 2017 20:44
Property based testing example
-module(kata).
-compile([export_all]).
-include_lib("proper/include/proper.hrl").
%% Test that calendar:is_leap_year/1 is fully compliant with the rules for computing leap years.
%% https://en.wikipedia.org/wiki/Leap_year#Algorithm
-define(ELSE, true).
gen_year() -> ?SUCHTHAT(Y, integer(), Y > 0 ).
@jadeallenx
jadeallenx / big_set_merge.erl
Created February 6, 2017 22:29
Comparing Erlang set implementations
%% Comparing sets implementations
%%
%% Uses this library for term size: https://github.com/okeuday/erlang_term
%%
%% Compile / run:
%% git clone https://github.com/okeuday/erlang_term
%% cd erlang_term
%% (copy this file into the directory and...)
%% rebar3 shell
%% 1> c(big_set_merge).
@jadeallenx
jadeallenx / t.py
Created January 10, 2017 22:14
Parsing hex protobuf files in python
>>> import charm.proto.hex_pb_signed_pb2
>>> s = charm.proto.hex_pb_signed_pb2.Signed()
>>> import charm.proto.hex_pb_package_pb2
>>> pkg = charm.proto.hex_pb_package_pb2.Package()
>>> pkgbuf = None
>>> with open("test/gisla.unc") as f:
... pkgbuf = f.read()
...
>>> s.ParseFromString(pkgbuf)
>>> s.payload
@jadeallenx
jadeallenx / cpanfile
Last active December 21, 2016 11:29
Complete listing for my 2016 Perl Advent article
requires 'Text::CSV'
requires 'Geo::Calc::XS'
requires 'IO::String'
@jadeallenx
jadeallenx / twitter-zk.md
Last active March 7, 2019 07:59
Using Twitter's ZK client

Using Twitter's ZK client

Introduction

I'm a Scala noob.

I am deploying some Spark jobs which need to talk to various services in our production environment. These services are managed by a ZooKeeper installation that registers their host and ports for service discovery. Hence, our Scala code needs to interact with ZooKeeper to discover the host and ports to use when making HTTPS calls to these internal services.

There is some ScalaDoc for util-zk that describes the classes, objects and function signatures but those don't help me very much since I'm a newb and none of it really makes a ton of sense right now.

So I am writing this document with my discoveries about how the util-zk library behaves empirically.

@jadeallenx
jadeallenx / README.md
Last active July 20, 2016 22:15
Perl regular expression performance

In response to an outage on Stack Overflow Rob Pike tweeted that regex parsing failure was avoidable and pointed toward a great post on regex complexity by Russ Cox. The version of perl in that post (5.8.7) is super ancient and in perl 5.10 the regex engine received a ton of optimization work and that optimization work has continued right through to the most recent release - 5.24.0.

Running the script above on 5.24.0 shows:

shadowfax:~ mallen$ time perl re.pl 
match

real	0m0.099s
user	0m0.026s
sys	0m0.039s
@jadeallenx
jadeallenx / discussion.md
Last active April 21, 2023 17:13
When does terminate/2 get called in a gen_server?

When does terminate/2 get called in a gen_server?

This is what the [official documentation][1] says about the terminate/2 callback for a gen_server:

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Reason is a term denoting the stop reason and State is the internal state of the gen_server.

Reason depends on why the gen_server is terminating. If it is because another callback function has returned a stop tuple {stop,..}, Reason will have the value specified in that tuple. If it is due to a failure, Reason is the error reason.

@jadeallenx
jadeallenx / tour.md
Last active September 18, 2016 00:56
Post Strange Loop BBQ tour

Post Strange Loop BBQ Tour

Like [last year][1] I am planning to visit many of the BBQ joints in Saint Louis, most of which are not in walking distance of the conference venues. Unlike in 2015, the [Q in the Lou BBQ][6] festival will not conveniently overlap dates with the Strange Loop conference- Q in the Lou will occur the weekend following Strange Loop.

Itinerary

This is the itinerary. We will leave Union Station in groups using UberXLs or cabs around 10:30 am. Meet outside the main doors at Union Station Double Tree on Market Street.