Skip to content

Instantly share code, notes, and snippets.

View liveforeverx's full-sized avatar

Dmitry Russ liveforeverx

  • Frankfurt am Main
View GitHub Profile
defmodule KV.GenServer do
use GenServer
def start_link(opts), do: GenServer.start_link(__MODULE__, :ok, opts)
def lookup(server, name), do: GenServer.call(server, {:lookup, name})
def create(server, name, value), do: GenServer.cast(server, {:create, name, value})
def init(:ok), do: {:ok, %{}}
@liveforeverx
liveforeverx / gist:1c30f06c004174a032a9
Last active August 29, 2015 14:11
SQL initial create string
defmodule Ecto.Model.SQL.Init do
def gen_sql_create(model, strict \\ false) when is_atom(model) do
table_name = model.__schema__(:source)
field_names = model.__schema__(:field_names)
assocs = model.__schema__(:associations) |> Enum.flat_map(fn(association) ->
case model.__schema__(:association, association) do
%Ecto.Reflections.BelongsTo{key: field, associated: assoc_model, assoc_key: assoc_key} ->
[{field, {assoc_model.__schema__(:source), assoc_key}}]
_ ->
[]
<<653545453567:(lists:foldl(fun erlang:'+'/2, 0, [4, 4, 8]))/little>>.
=> works
<<653545453567 :: (:lists.foldl(&(&1 + &2), 0, [4, 4, 8]))-little>>
** (CompileError)
<<653545453567 :: size(:lists.foldl(&(&1 + &2), 0, [4, 4, 8]))-little>>
** (CompileError)
@liveforeverx
liveforeverx / gist:7870008
Last active December 30, 2015 18:49
Elixir maps experimental
# github.com/liveforeverx/elixir branch: r17maps
defmodule Maps do
lc a inlist [:a, :b, :c] do
def test(%[ unquote(a) := n ]), do: n # Use at compiletime
end
end
defmodule TestRegex do
def erlang_regex do
:re.run("abc", "([a-z]+)", [{:capture, :all_but_first, :list}])
end
{:ok, regex} = :re.compile("([a-z]+)")
escaped_regex = Macro.escape(regex)
def compiled_regex do
:re.run("abc", unquote(escaped_regex), [{:capture, :all_but_first, :list}])
@liveforeverx
liveforeverx / gist:5729328
Created June 7, 2013 13:38
https://github.com/evanmiller/boss_db/blob/master/src/boss_record_compiler.erl More, as half, of it implemented, with some extra functionality as: private attributes and mappers
defmodule Generic.BossDB.Model do
defmacro defmodel(name, fields, options, do_block // []) do
attributes = lc {key, _} inlist fields, do: key
private = Keyword.get(options, :private, [])
public_attributes = lc attr inlist attributes, not Enum.member?(private, attr), do: attr
types = lc key inlist attributes, do: {key, to_char_list(key)}
{atom, _, _} = name
mappers = lc attr inlist attributes, do: {attr, Generic.Helper.to_bin_attr(attr)}
quoted = lc {attr, bin} inlist mappers do
quote do
to_generate =
[bad_request: [bad_version: 0,
bad_type: 1,
bad_stat: 2,
bad_experimenter: 3,
bad_subtype: 4,
eperm: 5,
bad_len: 6,
buffer_empty: 7,
buffer_unknown: 8],
/**
* Inspired by AngularJS' implementation of "click dblclick mousedown..."
*
* This ties in the Hammer events to attributes like:
*
* hm-tap="add_something()"
* hm-swipe="remove_something()"
*
* and also has support for Hammer options with:
*
# The install script
# Adapted from https://gist.github.com/579814
echo '# Added by install script for node.js and npm in 30s' >> ~/.bashrc
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
echo 'export NODE_PATH=$HOME/local/lib/node_modules' >> ~/.bashrc
. ~/.bashrc
mkdir -p ~/local
mkdir -p ~/Downloads/node-latest-install
@liveforeverx
liveforeverx / gist:5320797
Created April 5, 2013 16:48
Compile process by dynamo
dynamo dima$ MIX_ENV=test mix do deps.get, test
* Getting hackney [git: "https://github.com/benoitc/hackney.git"]
Cloning into 'deps/hackney'...
remote: Counting objects: 577, done.
Receiving objects: 100% (577/577), 284.67 KiB | 187 KiB/s, done. 0% (1/577)
Resolving deltas: 100% (407/407), done.
* Getting edown [git: "https://github.com/esl/edown.git"]
Cloning into 'deps/edown'...
remote: Counting objects: 742, done.
Receiving objects: 100% (742/742), 368.70 KiB | 286 KiB/s, done.