Skip to content

Instantly share code, notes, and snippets.

View mpugach's full-sized avatar

Maksym Pugach mpugach

View GitHub Profile
@mpugach
mpugach / deep_compare.ex
Last active September 22, 2022 14:08
Elixir deep compare hack
defmodule DeepCompare do
def call(payload_a, payload_b) when is_map(payload_a) and is_map(payload_b) do
Enum.reduce(payload_a, true, fn {k, v}, acc ->
acc and call(v, payload_b[k])
end)
end
def call([element_a | rest_of_a], [element_b | rest_of_b]) do
call(element_a, element_b) and call(rest_of_a, rest_of_b)
end
@mpugach
mpugach / map_from_deep_struct.ex
Last active October 11, 2022 02:04
Convert deeply nested Elixir struct into map
defmodule MapFromDeepStruct do
def from_deep_struct(%{} = map), do: convert(map)
defp convert(data) when is_struct(data) do
data |> Map.from_struct() |> convert()
end
defp convert(data) when is_map(data) do
for {key, value} <- data, reduce: %{} do
acc ->
@mpugach
mpugach / foo.ex
Created March 29, 2021 06:58
What implementation of Foo.mega_to_payload/3 do you prefer? Why?
defmodule Foo do
def mega_to_payload(
%MyMegaProtobuf{
c_seq: c_seq,
call_id: call_id
},
timestamp,
message_id
) do
%{
@mpugach
mpugach / OTP
Created February 26, 2019 14:56 — forked from maierru/OTP
Phoenix (elixir) highload optimization
# Move erts IO Polling to dedicated threads
# https://github.com/erlang/otp/pull/1552
@mpugach
mpugach / application.ex
Created February 18, 2019 07:08
Elixir Cowboy 2 options example
#https://gitlab.com/stressgrid/dummies/blob/master/elixir_dummy/lib/elixir_dummy/application.ex#L23
defmodule Dummy.Application do
@moduledoc false
use Application
def start(_type, _args) do
dispatch =
:cowboy_router.compile([
{:_,
@mpugach
mpugach / observer.md
Created April 28, 2018 14:10 — forked from pnc/observer.md
Using Erlang observer/appmon remotely

Using OTP's observer (appmon replacement) remotely

$ ssh remote-host "epmd -names"
epmd: up and running on port 4369 with data:
name some_node at port 58769

Note the running on port for epmd itself and the port of the node you're interested in debugging. Reconnect to the remote host with these ports forwarded:

$ ssh -L 4369:localhost:4369 -L 58769:localhost:58769 remote-host

Keybase proof

I hereby claim:

  • I am mpugach on github.
  • I am mpugach (https://keybase.io/mpugach) on keybase.
  • I have a public key ASDOu5tC72fZB_QozLHEIgU-7ejDbmzluffDRseBeLI6WQo

To claim this, I am signing this object:

@mpugach
mpugach / controllers.application.js
Last active November 11, 2020 15:49
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
queryParams: ['categories', 'serachQuery'],
categories: [],
serachQuery: '',
@mpugach
mpugach / activities_controller.rb
Last active June 12, 2017 13:47
Active interaction controller
module Api
module V1
class ActivitiesController < Api::V1::ApplicationController
render_interactions :create, :update, :show, :destroy
end
end
end
@mpugach
mpugach / each_compile.rb
Created May 16, 2017 07:18
Find uglifier errored files
JS_PATH = "app/assets/javascripts/**/*.js";
Dir[JS_PATH].each do |file_name|
puts "\n#{file_name}"
puts Uglifier.compile(File.read(file_name))
end