Skip to content

Instantly share code, notes, and snippets.

@sofakingworld
sofakingworld / elixir_bad_practice.ex
Created November 25, 2018 10:26
"Alternative" state storage
defmodule Foo do
def bar do
Application.get_env(:foo, :bar)
end
end
Application.put_env(:foo, :bar, 3)
Foo.bar() # => 3
@sofakingworld
sofakingworld / stack_genserver.ex
Last active March 20, 2019 00:00
Elixir Genserver#1
defmodule StackGenserver do
use GenServer
# Client
def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default)
end
def push(pid, item) do
@sofakingworld
sofakingworld / stack_genserver_test.exs
Created March 19, 2019 21:10
Elixir Genserver#1 test
defmodule StackGenserverTest do
use ExUnit.Case
test "pops first item" do
{:ok, pid} = StackGenserver.start_link([1, 2, 3])
assert 1 == StackGenserver.pop(pid)
end
test "pops empty list" do
{:ok, pid} = StackGenserver.start_link([])
@sofakingworld
sofakingworld / stack_imp.ex
Last active March 19, 2019 23:50
StackImp#2
defmodule StackImp do
def pop(list) do
# Возвращает кортеж с вынутым значением, и остатком списка.
# подходит в качестве ответа для генсервера
List.pop_at(list, 0)
end
def push(list, item) do
new_list = [item | list]
@sofakingworld
sofakingworld / stack.ex
Last active March 19, 2019 23:58
Stack#2
defmodule Stack do
use GenServer
# Client
def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default)
end
def push(pid, item) do
defmodule StackImpTest do
use ExUnit.Case
test "pops first item" do
assert {1, [2, 3]} = StackImp.pop([1,2,3])
end
test "pops empty list" do
assert {nil, []} = StackImp.pop([])
end
@sofakingworld
sofakingworld / cloudSettings
Last active September 25, 2019 13:19
Visual Studio Code Settings Sync Gist
{"lastUpload":"2019-04-01T10:01:42.760Z","extensionVersion":"v3.2.7"}
@sofakingworld
sofakingworld / bash_result
Last active April 18, 2019 18:52
Timestamps in console
21:49:09 ➜ ~ echo 'Hello world'
Hello world
@sofakingworld
sofakingworld / benchmark.rb
Last active June 5, 2019 23:43
Custom serializer benchmarking
user = User.first
require 'Benchmark'
Benchmark.bm do |x|
x.report { 1000.times do UserSerializer.serialize(user) end}
x.report { 1000.times do FJUserSerializer.new(user).serializable_hash end}
end
# # =>
;WITH hours as (
SELECT 0 hour_id, '00:00 - 01:00' hour_literal UNION
SELECT 1 hour_id, '01:00 - 02:00' hour_literal UNION
SELECT 2 hour_id, '02:00 - 03:00' hour_literal UNION
SELECT 3 hour_id, '03:00 - 04:00' hour_literal UNION
SELECT 4 hour_id, '04:00 - 05:00' hour_literal UNION
SELECT 5 hour_id, '05:00 - 06:00' hour_literal UNION
SELECT 6 hour_id, '06:00 - 07:00' hour_literal UNION
SELECT 7 hour_id, '07:00 - 08:00' hour_literal UNION
SELECT 8 hour_id, '08:00 - 09:00' hour_literal UNION