Skip to content

Instantly share code, notes, and snippets.

View sumerman's full-sized avatar
🔮
DATABASE, DATABASE, JUST LIVING IN A DATABASE

Valery Meleshkin sumerman

🔮
DATABASE, DATABASE, JUST LIVING IN A DATABASE
View GitHub Profile
@sumerman
sumerman / escape_shell.ex
Created October 12, 2015 09:36
Escape shell arguments in elixir
defmodule Shell do
def escape_value(value), do: escape_value(value, "")
defp escape_value("", res), do: "\"#{res}\""
defp escape_value("\"" <> value, res), do: escape_value(value, res <> "\\\"")
defp escape_value("\\" <> value, res), do: escape_value(value, res <> "\\\\")
defp escape_value(<<char :: utf8, rest :: binary>>, res),
do: escape_value(rest, res <> <<char>>)
end
@sumerman
sumerman / is_primary.erl
Last active February 2, 2018 16:09
This snippet checks whether current riak node is a primary for the given key. (Works only for the N=1 case; use itr_pop for N > 1)
is_primary_for_doc(BKey) ->
{ok, CHBin} = riak_core_ring_manager:get_chash_bin(),
DocIdx = riak_core_util:chash_key(BKey),
Itr = chashbin:iterator(DocIdx, CHBin),
MyNode = node(),
case chashbin:itr_value(Itr) of
{Idx, Node} when Node == MyNode -> true;
{_Idx, _Node} -> false
end.
@sumerman
sumerman / udp_test.erl
Created November 19, 2012 19:54
Sample of receiving data from single UDP socket with more than one erlang process (thus potential bottleneck could be eliminated)
-module(udp_test).
-behaviour(gen_server).
-define(SERVER, ?MODULE).
-define(PORT, 9876).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([start_link/2, send/1,
@sumerman
sumerman / lambdaobj.scm
Created November 12, 2012 16:16
Incomplete process of object system implementation on top of scheme lambdas
#lang scheme
;;; lambda-dictionary
;;;
(define (make-dict pairs)
(define (pair-key p) (car p))
(define (find key pairs)
(define (target? p)
(equal? key (pair-key p)))
;;
@sumerman
sumerman / macro_src.scm
Created October 10, 2012 12:14
Scheme macro WTF
(define-syntax class
(syntax-rules (extends rmethods method)
((_ rmethods () r) `r)
((_ rmethods ((method (name arg1 ...) body ...) . methods) r)
(class rmethods methods ((name . ,(lambda (self arg1 ...) body ...)) . r)))
((_ header (extends parent) body ...)
(define header
(define methods (class rmethods (body ...) ()))
(make-dispatcher-self (extends (make-dict methods) parent))))
((_ header body ...)
@sumerman
sumerman / IntSet.hs
Created October 8, 2012 21:26
Scala class hierarchies vs Haskell algebraic-style vs Scala match
data IntSet = Empty
| Node { val :: Int,
left :: IntSet,
right :: IntSet } deriving Show
incl Empty x = Node x Empty Empty
incl node x | x < val node = node { left = incl (left node) x }
| x > val node = node { right = incl (right node) x }
| otherwise = node
@sumerman
sumerman / cowboy_config.erl
Created September 25, 2012 11:59
Sample cowboy (0.6.x) config(s)
cowboy_config() ->
{ok, App} = application:get_application(),
Disp = [
{'_', [
{[<<"combiners">>, <<"test">>], combiner_test_src, []},
{[<<"combiners">>, <<"list">>], combiners_list, []},
{[<<"combiners">>, <<"info">>, comb_name], combiner_info, []},
{[<<"combiners">>, '...'], cowboy_http_static, [
{directory, {priv_dir, App, []}},
{mimetypes, [
@sumerman
sumerman / pdscope.erl
Created September 3, 2012 10:02
Thoughts on safe Erlang PD usage to deal with short-living state (instead PTs like 'SeqBind')
-module(pdscope).
-export([pput/1, pget/0, pdscope/1, example/0]).
pput(X) ->
erlang:put(?MODULE, X).
pget() ->
erlang:get(?MODULE).
pdscope(Scope, F) ->
@sumerman
sumerman / config_srv.erl
Created May 28, 2012 16:00
Primitive config distribution server
-module(sp_config_srv).
-behaviour(gen_server).
-define(NODEUP_PAUSE, 100).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([
@sumerman
sumerman / brackets.hs
Created May 27, 2012 09:07
Solution for a classic balanced-parens problem
{-
Bal ::= (Bal)Bal | [Bal]Bal | ""
-}
import Control.Monad
brackets = [('[',']'), ('(',')')]
isObr b = elem b (map fst brackets)
isCoBr b1 b2 = elem (b1,b2) brackets || elem (b2,b1) brackets