Skip to content

Instantly share code, notes, and snippets.

@svdmitrij
svdmitrij / fsm
Last active January 22, 2016 18:20
((window) ->
$ = window.$
startApp = ->
StateMachine = window.StateMachine
$stateText = $("span.state")
fsm = StateMachine.create(
initial: "hungry"
events: [
name: "eat"
from: ["hungry", "hungrysick"]
@svdmitrij
svdmitrij / cartesian.coffee
Last active December 19, 2015 23:59
Cartesian product coffeescript
cartProd = (paramArray) ->
addTo = (curr, args) ->
rest = args.slice(1)
last = not rest.length
result = []
i = 0
while i < args[0].length
copy = curr.slice()
copy.push args[0][i]
if last
@svdmitrij
svdmitrij / cartesian.clj
Created July 19, 2013 12:15
Cartesian product
(defn cartesian-product
"All the ways to take one item from each sequence"
[& seqs]
(let [v-original-seqs (vec seqs)
step
(fn step [v-seqs]
(let [increment
(fn [v-seqs]
(loop [i (dec (count v-seqs)), v-seqs v-seqs]
(if (= i -1) nil
@svdmitrij
svdmitrij / cartesian.js
Created July 19, 2013 12:16
Cartesian product
function cartProd(paramArray) {
function addTo(curr, args) {
var i, copy,
rest = args.slice(1),
last = !rest.length,
result = [];
for (i = 0; i < args[0].length; i++) {
@svdmitrij
svdmitrij / sort.erl
Last active December 20, 2015 18:28
Merge sorting with inversions counting
-module('sort').
-export([start/0]).
start() ->
Ctr = spawn_link(counter, loop, [0]),
{ok, Data} = getData(),
{ok, _Sorted} = sortData(Data, Ctr),
counter:get_count(Ctr) .
getData() ->
@svdmitrij
svdmitrij / counter.erl
Created August 7, 2013 16:36
Counter process
-module('counter').
-export([incr/2, get_count/1, loop/1]).
loop(Count) ->
receive
{ incr, Add } ->
loop(Count + Add);
{ report, To } ->
To ! { count, Count },
loop(Count)
@svdmitrij
svdmitrij / cartesian.php
Last active January 10, 2020 14:09
4 algorithms of cartesian product
function cartesian($input) {
// http://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays
$result = array();
while (list($key, $values) = each($input)) {
// If a sub-array is empty, it doesn't affect the cartesian product
if (empty($values)) {
continue;
}
@svdmitrij
svdmitrij / cherryPick.sh
Created August 21, 2013 09:13
Cherry pick commit from other repository
#!/bin/bash
dir=$1
sha=$2
if [[ -z $dir || -z $sha ]]
then
echo "Usage: $0 otherRepoDir commitSha"
exit 1
fi
@svdmitrij
svdmitrij / count_files_by_date.sh
Last active January 3, 2016 23:49
Count files with date (crooked way). Just example for seq.
#!/bin/bash
for i in $(seq 1 31); do
echo $i = `ls -la|grep -c "Jan $i 2013"`;
done;
@svdmitrij
svdmitrij / openChangedFiles.sh
Last active January 4, 2016 18:59
Открыть все изменённые файлы в редакторе.
#!/bin/bash
subl `git status|grep modified|awk '{ print $3 }'`