Skip to content

Instantly share code, notes, and snippets.

View am-kantox's full-sized avatar
🎩
constantly matching the patterns

Aleksei Matiushkin am-kantox

🎩
constantly matching the patterns
View GitHub Profile
@am-kantox
am-kantox / .pryrc
Created October 27, 2018 05:37
.pryrc
# === EDITOR ===
Pry.editor = 'vi'
# === PROMPT ===
Pry.prompt = [ ->(obj, nest_level, _) { "✎ " }, ->(obj, nest_level, _) { "#{' ' * nest_level} " } ]
# === COLORS ===
unless ENV['PRY_BW']
Pry.color = true
Pry.config.theme = "railscasts"
@am-kantox
am-kantox / dry_struct_match.ex
Last active January 8, 2018 10:18
Explicitly matching structs of listed types in function clauses
defmodule DryStructMatch do
defmacrop clause!(name, mod, fun) do
quote bind_quoted: [name: name, mod: mod, fun: fun] do
quote do
def unquote(name)(
unquote({:%, [], [{:__aliases__, [alias: false], [mod]}, {:%{}, [], []}]}) = struct
) do
# |> ...
result = struct
unquote(:"#{name}_callback")(unquote(fun), result)
@am-kantox
am-kantox / elixir-snippets.json
Created November 26, 2017 07:37
Elixir snippets for VSCode
{
/*
// Place your snippets for Elixir here.
*/
"GenServer": {
"prefix": "genserver",
"body": [
"defmodule $1 do",
"@moduledoc \"\"\"",
" The `$1` server implementation",
@am-kantox
am-kantox / swipe-almost-everything.rb
Last active November 11, 2017 06:26
Ruby interview task
## Given a file, containing 1_000_000 lines, remove all duplicates, save for lines, starting with `#`
require 'digest/sha1' # to generate uniqs
FILE_NAME = 'input.txt'
File.readlines(FILE_NAME).each_with_index.uniq do |s, idx|
s.start_with?('#') ? Digest::SHA1.hexdigest(idx.to_s) : s
end.join("\n")
@am-kantox
am-kantox / yum_install_rabbitmq.sh
Created August 10, 2017 14:54
Install RabbitMQ on Amazon Linux
wget https://github.com/rabbitmq/erlang-rpm/releases/download/v19.3.6.2/erlang-19.3.6.2-1.el6.x86_64.rpm
sudo rpm -Uvh erlang-19.3.6.2-1.el6.x86_64.rpm
rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-3.6.10-1.el6.noarch.rpm
rpm -Uvh rabbitmq-server-3.6.10-1.el6.noarch.rpm
chkconfig rabbitmq-server on
/sbin/service rabbitmq-server start
rabbitmqctl status
rabbitmq-plugins enable rabbitmq_management
@am-kantox
am-kantox / elixir-as-a-service.md
Last active August 10, 2017 11:01
deploy elixir to use systemctl as service manager

Here we assume we have a dedicated user myuser created.

The application is supposed to be in /vol/release/$USER

Preparation

Editor

$ sudo update-alternatives --config editor

choose whatever you want

@am-kantox
am-kantox / finally_ensuring.rb
Last active May 24, 2017 09:43
Don’t ask this on interviews!
def a1
:normal
ensure
:ensure
end
def a2
return :normal
ensure
return :ensure
@am-kantox
am-kantox / log.ex
Created April 24, 2017 15:55
Making a sibling for lazy Elixir `Logger`
defmodule Eventory.Helpers.Log do
@moduledoc false
Enum.each(~w|error info warn debug|a, fn level ->
Module.register_attribute(__MODULE__, :level, persist: false)
Module.put_attribute(__MODULE__, :level, level)
defmacro unquote(Module.get_attribute(__MODULE__, :level))(chardata_or_fun, metadata \\ []) do
{{:., [], [
{:__aliases__, [alias: false], [:Logger]},
unquote(Module.get_attribute(__MODULE__, :level))]},
@am-kantox
am-kantox / digest_benchmark.rb
Created March 29, 2017 14:28
Ruby :: benchmark of different digests
require 'benchmark/ips'
n = 500000
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
require 'digest/sha3' # https://github.com/phusion/digest-sha3-ruby
INPUT = [5, "Hello, world", nil]
@am-kantox
am-kantox / .iex.exs
Created March 3, 2017 06:29
Fancy IEx config
# IEx.configure colors: [enabled: true]
# IEx.configure colors: [eval_result: [:cyan, :bright]]
{fortune, 0} = System.cmd "fortune", [] # displays the fortune prompt
IO.puts IO.ANSI.red_background() <> IO.ANSI.white() <> "▷ #{fortune}" <> IO.ANSI.reset
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [
eval_result: [:green, :bright] ,
eval_error: [[:red, :bright, "\n▶▶▶\n"]],
eval_info: [:yellow, :bright ],