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 / 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 / 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 / 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 / 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 / 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 / .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 / validator.ex
Last active October 31, 2018 09:08
Updating guard module
defmodule Validator do
def instance!(rules, merge \\ true) do
mod = Module.concat(["Validator", "Instance"])
current_rules =
if Code.ensure_compiled?(mod) do
rules = if merge, do: apply(mod, :rules, []), else: %{}
:code.purge(mod)
:code.delete(mod)
rules
@am-kantox
am-kantox / smart-commit.sh
Last active February 20, 2021 12:47
Bash script that prefixes commits with a JIRA (CO-400)
#!/usr/bin/env bash
# based on https://github.com/sbimochan/smart-commit/blob/master/commit
# am@aleksei  feature/CO-400-some-branch-name$ ./smart-commit.sh "Foo bar."
# → [feature/CO-400-some-branch-name f70ebbf167] CO-400: Foo bar.
set -euo pipefail
if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then

The only reason is to make it easier to embed them into the pipeline built with Kernal.|>/2.

As one might easily see, String.replace/4 delegates to Regex.replace/4.

When one starts with a string, they might pipe String.replace/4 without a necessity to leave a pipeline. When one starts with a regex, the same applies to Regex.replace/4.

@am-kantox
am-kantox / map_access.ex
Created November 1, 2019 11:48
`MapAccess.filter/1` → the same as `Access.filter/1`, but works with maps.
defmodule MapAccess do
@spec filter((term -> boolean)) :: Access.access_fun(data :: map, get_value :: map)
def filter(func) when is_function(func, 1) do
fn op, data, next -> filter(op, data, func, next) end
end
defp filter(:get, %{} = data, func, next) do
data |> Enum.filter(func) |> Enum.map(next) |> Map.new()
end