Skip to content

Instantly share code, notes, and snippets.

@pichi
pichi / results
Created October 29, 2017 15:59
Tail vs Dirct recursion in Erlang
$ erl
Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.1 (abort with ^G)
1> c(tail_vs_direct).
{ok,tail_vs_direct}
2> tail_vs_direct:compare().
[{5000,
{tail_c,{812,{total_heap_size,35462}}},
{direct,{519,{total_heap_size,28689}}}},
@pichi
pichi / rps.erl
Last active March 24, 2017 12:24
Rock, paper, scissors game definition
-module(rps).
-export([beats/1]).
-export([result/2]).
-export([tournament/2]).
beats(rock) -> scissors;
beats(paper) -> rock;
@pichi
pichi / test.erl
Last active July 27, 2016 08:17 — forked from redink/test.erl
Find top n items in a unordered list
-module(test).
-compile(export_all).
%% API
-compile({inline, [ insert/2
, merge/2
]}).
insert(E, []) -> [E];
insert(E, [E2|_] = H) when E =< E2 -> [E, H];
@pichi
pichi / test.erl
Last active July 27, 2016 03:25 — forked from zhongwencool/test.erl
Find top n items in a unordered list
-module(test).
-compile(export_all).
%% API
-compile({inline, [ insert/2
, merge/2
]}).
insert(E, []) -> [E];
insert(E, [E2|_] = H) when E =< E2 -> [E, H];
@pichi
pichi / bignum_root.erl
Last active July 24, 2016 20:17 — forked from jj1bdx/bignum_root.erl
Power function (X ^ Y) and root function (X ^ (1/Y)) for integers in Erlang
%% Power function (X ^ Y) and root function (X ^ (1/Y)) for
%% integers in Erlang
%% by Kenji Rikitake <kenji.rikitake@acm.org> 26-JAN-2010
%% modified by Hynek Vychodil <vychodil.hynek@gmail.com> 2-FEB-2010
%% modified by Kenji Rikitake <kenji.rikitake@acm.org> 3-FEB-2010
%% modified by Hynek Vychodil <vychodil.hynek@gmail.com> 24-JUL-2016
%% Distributed under MIT license at the end of the source code.
-module(bignum_root).
@pichi
pichi / Result
Last active March 30, 2016 17:59
Stopwords Benchmark
$ erl -pa eministat/ebin
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V7.3 (abort with ^G)
1> {ok, Bin} = file:read_file("/home/hynek/Downloads/words.txt"), L = string:tokens(binary_to_list(Bin), "\s\r\n"), length(L).
113809
2> length(lists:filter(fun stopwords_clause:is_stopword/1, L)).
122
3> length(lists:filter(fun stopwords_map:is_stopword/1, L)).
122
@pichi
pichi / test.erl
Created August 18, 2015 15:12
StackOverflow question: Best clock or number generator function for concurrency/scalability on Erlang OTP 18?
-module(test).
-export([bench_all/1, bench/2, bench/3]).
-export([ unique_monotonic_integer/0
, update_counter/0
]).
iters() -> 1000000.
@pichi
pichi / search_setperms.c
Last active August 29, 2015 14:04
Fast finding of all permutations of given set of characters in string
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
// fast int
#if __WORDSIZE == 64
typedef unsigned long int uint_fast_t;
#define UINT_FAST(c) c ## UL
#else
typedef unsigned int uint_fast_t;
@pichi
pichi / fib.erl
Last active March 5, 2017 21:25
Big Fibonacci number generator in Erlang
%% Fibonacci number generator in Erlang
%% by Hynek Vychodil <vychodil.hynek@gmail.com> 3-JAN-2014
%% Distributed under MIT license at the end of the source code.
-module(fib).
-export([fib/1]).
% NOTE: Native compilation (HiPE) doesn't improve efficiency due heavy integer
% bignum computations as observed in R16
@pichi
pichi / vertical_tree.erl
Last active December 31, 2015 02:09
Module for getting "vertical lines" (see http://stackoverflow.com/q/20521098/49197) It also contain tree pretty printer (far more sophisticated then getting vertical lines BTW)
-module(vertical_tree).
-export([draw/1, get_verical_lines/1]).
-record(node, {
value,
left = nil,
right = nil
}).