Skip to content

Instantly share code, notes, and snippets.

View focused's full-sized avatar

Dmitry Novikov focused

  • Batumi, Georgia
View GitHub Profile
@keathley
keathley / possibly.ex
Created January 7, 2016 20:57
Monads and Maybes in elixir
defprotocol Functor do
def fmap(data, func)
end
defprotocol Applicative do
def pure(data)
def run(wrapped_func, data)
end
defprotocol Monad do
@smaximov
smaximov / README.md
Last active August 22, 2019 09:36
HTTP и WebSocket Reverse Proxy

Background: это было написано для "фронт-энд" сервиса, который должен был проксировать HTTP и WebSocket-запросы на несколько внутренних сервисов.

Для того, чтобы переопределить то, как Phoenix обрабатывает вебсокеты, надо поменять настройки HTTP сервера (Cowboy), а именно настройки роутинга (:dispatch) — см. config.exs. Тут мы указываем, что для любого хоста: а) для пути "/aaa/websocket" вызывается хендлер API.Gateway.WSReverseProxy, б) для любого другого пути вызывается дефолтный хэндлер Phoenix.

API.Gateway.WSReverseProxy — это хэндлер Cowboy, для подробной информации о р

@ostinelli
ostinelli / erlang-release-init.d.md
Last active September 15, 2019 12:18
Erlang Release init.d script

Erlang Release init.d script

If you have packaged your application into a release generated with Rebar, you might want to have the following:

  • The release started on system boot.
  • The VM monitored and restarted it if it crashes.

Use HEART

HEART is the Heartbeat Monitoring of an Erlang Runtime System. The purpose of the heart port program is to check that the Erlang runtime system it is supervising is still running, so that if the VM crashes or becomes unresponsive, it is restarted.

# TODO:
# Runner like https://github.com/elixir-lang/ecto/blob/master/lib/ecto/migration.ex (does it have state?)
defmodule Ecto.Runner do
def start_command({:create, table}) do
IO.puts "create table: #{table}"
end
def subcommand({:add, column, type}) do
IO.puts "add column: #{column}(#{type})"
@henrik
henrik / poolboy_demo.ex
Last active December 16, 2020 13:56 — forked from sasa1977/poolboy_demo.ex
Example of using Poolboy in Elixir to limit concurrency (e.g. of HTTP requests).
defmodule HttpRequester do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, nil, [])
end
def fetch(server, url) do
# Don't use cast: http://blog.elixirsips.com/2014/07/16/errata-dont-use-cast-in-a-poolboy-transaction/
timeout_ms = 10_000
@juanpabloaj
juanpabloaj / logs_with_color.exs
Created June 25, 2021 23:41
elixir, colorful log lines
require Logger
Logger.configure_backend(:console, format: "$time $metadata[$level] $levelpad$message\n")
# more colores in
# https://hexdocs.pm/elixir/1.12/IO.ANSI.html
Logger.info("colorful log line", ansi_color: :black)
Logger.info("colorful log line", ansi_color: :blue)
Logger.info("colorful log line", ansi_color: :cyan)
@chenzhan
chenzhan / redshift_admin_queries.sql
Last active August 8, 2023 05:49
Redshift Tips
# List all tables:
select db_id, id, name, sum(rows) as mysum
from stv_tbl_perm where db_id = 100546
group by db_id, id, name order by mysum desc;
# list all running processes:
select pid, query from stv_recents where status = 'Running';
# describe table
select * from PG_TABLE_DEF where tablename='audit_trail';
@ahmadshah
ahmadshah / randomizer.ex
Created September 1, 2016 10:28
Random string in elixir
defmodule Randomizer do
@moduledoc """
Random string generator module.
"""
@doc """
Generate random string based on the given legth. It is also possible to generate certain type of randomise string using the options below:
* :all - generate alphanumeric random string
* :alpha - generate nom-numeric random string
@kipcole9
kipcole9 / Map.Helpers
Last active October 24, 2023 22:13
Helpers for Elixir Maps: underscore, atomise and stringify map keys
defmodule Map.Helpers do
@moduledoc """
Functions to transform maps
"""
@doc """
Convert map string camelCase keys to underscore_keys
"""
def underscore_keys(nil), do: nil
@zabirauf
zabirauf / expng.ex
Created July 23, 2015 08:32
PNG format Parser in Elixir
defmodule Expng do
defstruct [:width, :height, :bit_depth, :color_type, :compression, :filter, :interlace, :chunks]
def png_parse(<<
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
_length :: size(32),
"IHDR",
width :: size(32),
height :: size(32),