Skip to content

Instantly share code, notes, and snippets.

@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_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_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 / 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 / table.md
Last active November 6, 2018 11:51
beauty_kernel_list
Kernel's function with arity BeautyKernel's alias
!=/2 not_eq?
*/2 multiply
++/2 list_concat
+/1 plus
+/2 plus
--/2 list_remove
-/1 minus
-/2 minus
@sofakingworld
sofakingworld / Example.ex
Last active March 25, 2018 15:18
Meta Elixir (Create methods dynamic)
defmodule Dynamic do
@methods ~w(method1 method2 method3)a
@methods
|> Enum.each( fn method ->
method_name = "prefix_#{method}"
def unquote(:"#{method_name}")(argument1, argument2) do
{unquote(method), argument1, argument2}
end
end)
# For MacOS and "Postgres" App Users
# where var-10 is version of postgreSQL
cd /Users/$USER/Library/Application\ Support/Postgres/var-10/
rm postmaster.pid
@sofakingworld
sofakingworld / config.exs
Last active February 14, 2018 09:17
Форматирование логов
.....
config :logger, :console,
level: :debug,
format: {LoggerFormatter, :format_log},
@sofakingworld
sofakingworld / Array.eachSlice
Last active October 16, 2017 13:45
JavaScript analog of EachSlice from Ruby
Array.prototype.eachSlice = function(size=1) {
return Array(Math.ceil(this.length / size))
.fill()
.map(( _, idx) => {
return this.slice(idx * size, (1 + idx) * size)
})
}
@sofakingworld
sofakingworld / PSQL_tip_1
Created December 16, 2016 15:21
PSQL btree_gin index searching
/*
Making btree_gin index and using it in search
*/
CREATE EXTENSION btree_gin;
create index ix_table_1 on table_1 USING GIN (to_tsvector('simple', coalesce(table_1.seach_field.value::text, '')), table_1.id)
select * from table_1
where to_tsvector('simple', coalesce(table_1.seach_field.value::text, '')) @@ to_tsquery( 'example:*' );
-- index used