Skip to content

Instantly share code, notes, and snippets.

View garazdawi's full-sized avatar

Lukas Backström (FKA Larsson) garazdawi

View GitHub Profile
@garazdawi
garazdawi / rewrite-seealso.escript
Last active March 12, 2020 15:28
rewrite-seealso
#!/usr/bin/env escript
%% -*- erlang -*-
%%! +A 1 +SDio 1 +S 1 -mode minimal
%% This script can be used to convert <seealso> to <see*>
%% Invoked like this:
%% find lib/*/doc/src system/doc/*/ erts/doc/src/ '(' -name *.xml -o -name *.xmlsrc ')' -exec ./rewrite-seealso.escript {} \;
%% You need a OTP with built documentation for this to work
main([File]) ->
io:format("Parse: ~s~n",[File]),
@garazdawi
garazdawi / gcount.erl
Created September 6, 2019 14:20
Global Counters in Erlang
-module(gcount).
-export([bench/0,bench/1]).
-export([ets_init/0,ets_new/1,ets_incr/1,ets_read/1]).
-export([cnt_init/0,cnt_new/1,cnt_incr/1,cnt_read/1]).
-export([cnt_pt_init/0,cnt_pt_new/1,cnt_pt_incr/1,cnt_pt_read/1]).
ets_init() ->
ets:new(?MODULE, [{write_concurrency,true},named_table,public]).
ets_new(Counter) ->
@garazdawi
garazdawi / bit_vector.erl
Last active June 9, 2023 17:42
A shared mutable bit-vector in Erlang
-module(bit_vector).
-export([new/1, get/2, set/2, clear/2, flip/2, print/1]).
new(Size) ->
Words = (Size + 63) div 64,
{?MODULE, Size, atomics:new(Words, [{signed, false}])}.
get({?MODULE, _Size, Aref}, Bix) ->
@garazdawi
garazdawi / tlsv1.3.sh
Created May 10, 2019 13:07
How to compile openssl and Erlang/OTP with TLSv1.3
#!/bin/bash
git clone https://github.com/openssl/openssl
cd openssl
git checkout OpenSSL_1_1_1b
./Configure shared --prefix=$HOME/apps/openssl/1.1.1b --openssldir=$HOME/apps/openssl/1.1.1b linux-x86_64
make
make install
cd ..
-module(gen_tcp2).
-export([listen/2, accept/1, accept/2,
connect/3, connect/4,
send/2, recv/1, recv/2, recv/3,
close/1]).
-export([run/0, run/2]).
%% Naive gen_tcp shim over the socket API
@garazdawi
garazdawi / decode.erl
Created August 3, 2018 09:53
Decode of partial erlang external term format
-module(decode).
-export([term/1]).
term(<<131,Rest/binary>>) ->
element(1,term(Rest));
%% List
term(<<108,Sz:32,Rest/binary>>) ->
{Lst, R} = lists:foldl(
fun(_, {Acc, B}) ->
@garazdawi
garazdawi / trace_issue.escript
Created August 1, 2018 11:33 — forked from devonestes/trace_issue.escript
Reproduction escript for trace/3 wierdness
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname wtf
main(_) ->
process_flag(trap_exit, true),
ToMap = lists:seq(1, 50),
MapFun = fun() ->
lists:map(fun(N) -> N end, ToMap)
end,
start_runner(MapFun),
%% Introducing the new `apply` pattern match operand.
%% `apply` is used to delegate the handling of a match
%% to some other function including the action to be taken
%% when a successful match happens. The `apply` statement
%% replaces the "-> body" part of a function/case/receive.
%%
%% Simple example:
%% example() ->
%% Pat = fun(A) when is_atom(A) ->
%% {ok, A}
> infocmp xterm
# Reconstructed via infocmp from file: /lib/terminfo/x/xterm
xterm|xterm-debian|X11 terminal emulator,
am, bce, km, mc5i, mir, msgr, npc, xenl,
colors#8, cols#80, it#8, lines#24, pairs#64,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=^M,
csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
keyfind(Key,Value,[H|T]) ->
case maps:find(Key,H) of
{ok, Value} ->
H;
_ ->
keyfind(Key,Value,T)
end;
keyfind(_,_,[]) ->
false.