Skip to content

Instantly share code, notes, and snippets.

View hamidreza-s's full-sized avatar

Hamidreza Soleimani hamidreza-s

View GitHub Profile
@hamidreza-s
hamidreza-s / file_mode_calc.erl
Created August 11, 2017 14:11
A module for calculating access mode of unix files based on the mode_t bit-bucket value.
-module(file_mode_calc).
-export([run/0]).
-include_lib("kernel/include/file.hrl").
%% source: https://jameshfisher.github.io/2017/02/24/what-is-mode_t.html
-define(S_IRWXU, 8#0000700).
-define(S_IRUSR, 8#0000400).
@hamidreza-s
hamidreza-s / calc.erl
Last active June 23, 2023 19:42
Sieve of Eratosthenes algorithm for finding all prime numbers up to N written in Erlang.
-module(calc).
-export([prime_numbers/1]).
% Source: https://wbear.wordpress.com/2011/12/08/prime-numbers-with-erlang/
% Sieve of Eratosthenes algorithm for finding all prime numbers up to N.
% http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
% Generate list of prime numbers up to N.
prime_numbers(N) when is_number(N) ->
prime_numbers(N, generate(N)).
@hamidreza-s
hamidreza-s / RecoverEpmd.txt
Last active April 16, 2018 08:39
How to recover the connection between a distributed Erlang node and EPMD.
## node 1
$ erl
1> erl_epmd:start().
2> erl_node:register_node(ejabberd, 23456).
## Emergency 'epmd' Recovery
## node 2
$ erl -sname repair
1> net_adm:ping(ejabberd@hostname).
@hamidreza-s
hamidreza-s / UnixCommands.txt
Created January 11, 2016 13:14
Super simple Unix/Linux "Display" commands.
/*****************************************************************/
/* Super Simple Unix/Linux "Display" commands. */
/* */
/* file : unixcommands.txt */
/* Date : 02/02/2013 */
/* Version: 0.3 */
/* By : Albert van der Sel */
/* Remarks: The Commands will only display information and not */
/* change anything at all. */
/*****************************************************************/
@hamidreza-s
hamidreza-s / FromSourceToByteCode
Last active August 11, 2017 14:17
There are lots of intermediate code generating between Erlang source code and byte code. These steps are as follows.
* Erlang Source Code
* Scanning
* Parsing
* Parse Trasform
* Linter
* Erlang AST ('P')
* Erlang Expanded AST ('E')
* Core Erlang
* Core Erlang AST
* Kernel Erlang
@hamidreza-s
hamidreza-s / utility.erl
Last active August 29, 2015 14:16
An Erlang module for counting a word occurrence inside a given directory files.
%% part 1: utility module
%% ======================
-module(utility).
-export([count/2]).
%% part 2: count function
%% ======================
count(Dir, Word) ->
Accumulator = 0,
CounterPid = spawn(fun() -> counter(Accumulator) end),
@hamidreza-s
hamidreza-s / direct_msg_to_rabbit.erl
Last active August 11, 2017 14:16
How to send direct message to RabbitMQ through Erlang console instead of AMQP protocol.
-module(direct_msg_to_rabbit).
%% Define Record
-record('P_basic', {
content_type,
content_encoding,
headers,
delivery_mode, priority,
correlation_id,
reply_to,
@hamidreza-s
hamidreza-s / main.erl
Last active April 1, 2021 07:26
How to use Parse Transform feature in Erlang for meta-programming.
-module(main).
-export([hi/0, bye/0, hey/1]).
-compile({parse_transform, main_pt}).
hi() -> io:format("Hi there!~n").
bye() -> io:format("Bye there!~n").
hey(foo) -> io:format("Hey foo!~n");
hey(bar) -> io:format("Hey bar!~n").
@hamidreza-s
hamidreza-s / VimCheatSheet.txt
Last active August 11, 2017 14:15
Vim cheat sheet
# in command mode
Ctrl+E (move screen up)
Ctrl+Y (move screen down)
H (move cursor to head of screen)
M (move cursor to middle of screen)
L (move cursor to end of screen)
gg (move cursor to head of file)
@hamidreza-s
hamidreza-s / Erlang - ClearConsole.erl
Created April 12, 2014 05:25
How to clear the Erlang shell.
%% move cursor to beginning of the line
io:format("\e[H").
%% clear the console
io:format("\e[J").
%% both
io:format("\e[H\e[J").