Electronic Elves Residence
Add
{code} {code} == == * * _ _ [] * #

Add arbitrary numbers

2017-02-13 15:18:23

function add($a, $b) {
    $r = '';
    $a = str_split(strrev($a));
    $b = str_split(strrev($b));
    if (count($b) > count($a)) {
        $tmp = $a;
        $a = $b;
        $b = $tmp;
    }
    for ($carry = 0, $idx = 0; isset($a[$idx]) || $carry; $idx++) {
        $value = (int)($a[$idx] ?? 0) + (int)($b[$idx] ?? 0) + $carry;
        if ($carry = (int)($value > 9)) $value -= 10;
        $r .= $value;
    }
    return strrev($r);
}

204

2017-01-12 12:44:40
$window = function ($window_size) {
    $window = [];
    $window_ptr = 0;
    $avg = 0;
    while (true) {
        $value = (yield $avg);
        if ($value === null) break;
        $window[$window_ptr] = $value;
        $window_ptr = ($window_ptr + 1) % $window_size;
        $avg = array_sum($window) / count($window);
    }
};

/** @var Generator $windowed_avg */
$windowed_avg = $window(10);
foreach ([10, 20, 30, 40, 50, 60, 70, 80, 90, 10, 10, 10, 10] as $v) {
    $avg = $windowed_avg->send($v);
    echo "sent $v got $avg\n";
}

203

2017-01-12 12:44:27
class defer {
        private $cb;
        public function __construct($cb) {$this->cb = $cb;}
        public function __destruct() {$cb = $this->cb; $cb();}
}

$log_id = \Logs::addLog(CLASS_DB, new \LogScreen());
$defer = new \defer(function() use ($log_id) { \Logs::delLog(CLASS_DB, $log_id);});

function cpusleep($cpu_time) {
    $cur = $prev_cur = $start = microtime(true);
    $cycle_time = 1e-5;
    $cycles = 1;
    $cycle_time_reaction = 1;

    while (($left = $cpu_time - ($cur - $start)) > 0) {
        $cycle_time = ($cycle_time + ($cur - $prev_cur) / $cycles) * $cycle_time_reaction / (1 + $cycle_time_reaction);
        $cycles = $left / $cycle_time;
        for ($i = 0; $i < $cycles; $i++);
        $prev_cur = $cur;
        $cur = microtime(true);
    }
}

function ru($msg, $ru_param = null) {
    $ru = $ru_param !== null ? getrusage($ru_param) : getrusage();
    echo "$msg:" . ($ru['ru_utime.tv_sec'] * 1e6 + $ru['ru_utime.tv_usec']) . "\n";
}

function sleep_period($epoch_period) {
        $now_ms = microtime(true) * 1000;
        $epoch_ms = (((int)($now_ms / $epoch_period)) + 1) * $epoch_period;
        usleep(($epoch_ms - $now_ms) * 1000);
}

202

2017-01-12 12:44:07
function utf8_encode2($num) {
    if ($num <= 0x7f) {
        return chr($num);
    } else if ($num <= 0x07ff) {
        return chr(0xc0 | ($num >> 6)) . chr(0x80 | ($num & 0x3f));
    } else if ($num <= 0xffff) {
        return chr(0xe0 | ($num >> 12)) . chr(0x80 | ($num >> 10 & 0x3f)) . chr(0x80 | ($num & 0x3f));
    } else if ($num <= 0x1fffff) {
        return chr(0xf0 | ($num >> 18)) . chr(0x80 | ($num >> 12 & 0x2f)) . chr(0x80 | ($num >> 6 & 0x3f)) . chr($num & 0x3f);
    }
}

201

2017-01-12 12:43:41
/**
 * Makes ANSI escape seq for 256 color, each color comp is 0..5
 */
function tc2e($r, $g, $b) {
    return "\33[38;5;" . ($r * 36 + $g * 6 + $b + 16) . "m";
}

function c($s, $k) {
    $res = ["\r\33[2K"];
    foreach (str_split($s, floor(strlen($s) / 12) + 1) as $j => $chunk) {
        $res[] = tc2e(
            0,
            1 + abs(4 - (($k + $j) % 9)),
            0) . $chunk;
    }
    return implode('', $res);
}

if (!stream_set_blocking(STDIN, 0)) die("could not set stream to nonblocking\n");

proc_close(proc_open('stty -icanon -echo -echoe', array(STDIN, STDOUT, STDERR), $pp));

$i = 0;
$s = '';
while (true) {
    $c = fgets(STDIN);
    if (feof(STDIN)) break;
    if ($c !== false) {
        foreach (str_split($c) as $c) {
            if ($c === "\4" || $c == "\33") {
                break 2;
            } else if ($c == "\x7f") { /*backspace*/
                $s = substr($s, 0, -1);
            } else if ($c == "\n") {
                echo "\r\33[2K\33[0m$s\n";
                $s = '';
            } else {
                $s .= $c;
            }
        }
    }
    $i++;
    echo c($s, $i);
    usleep(100000);
}
proc_close(proc_open('stty sane', array(STDIN, STDOUT, STDERR), $pp));

200

2017-01-12 12:02:51
class LogEm {
    const TILL_RETURN = 1;
    const TILL_COMMIT_ROLLBACK = 2;
    private $cb;
    static $log_id = null;
    function __construct($cb) {
        $this->cb = $cb;
    }
    function __destruct() {
        call_user_func_array($this->cb, [self::$log_id]);
        self::$log_id = null;
    }
    static function delLog() {
        Logs::delLog(CLASS_DB, self::$log_id);
    }
    static function sql($till, $file, $line, $msg = '') {
        file_put_contents('/tmp/emakhrov.log', sprintf("%s:%s %s\n", $file, $line, $msg), FILE_APPEND);
        if (self::$log_id !== null) return null;
        self::$log_id = \Logs::addLog(CLASS_DB, new LogFile('/tmp/sql_emakhrov.log'));
        if ($till == self::TILL_RETURN) return new self(array(__CLASS__, 'delLog'));
        ConnectionManager::addCommitCallBack(array(__CLASS__, 'delLog'), []);
        ConnectionManager::addRollbackCallBack(array(__CLASS__, 'delLog'), []);
    }
}


examples:
$ololo = LogEm::SQL(LogEm::TILL_RETURN, __METHOD__, __LINE__, $this->real_command);

LogEm::sql(LogEm::TILL_COMMIT_ROLLBACK, __FILE__, __LINE__);

199

2017-01-12 12:02:31
settings set target.x86-disassembly-flavor intel
http://www.linusakesson.net/programming/tty/index.php
http://rachid.koucha.free.fr/tech_corner/pty_pdip.html
https://gist.github.com/koshigoe/822455

198

2017-01-12 12:01:32
Shadow: a patch of shade; a dark figure projected by anything which intercepts rays of light. Shadow: the darker, less illuminated part of a picture. Shadow: an inseparable companion; a ghost. Shadow: phantom; to shadow; to cast a shadow over; to outline. Shadow: shadow; moving shadow. The first few steps: the first few words: the first moment when a line crosses a line and suddenly you can never stop seeing... this change is seeing more, seeing less, seeing better, seeing worse but seeing is the first stone in the pond and as the ripples spread they alter everything they touch fundamentally everything takes on meaning, a look, a touch, a motion, a sign all seeds growing, seeds that never stop growing because you just never can stop them how can it be explained ? think of the one thing that you always wanted then think of it existing within you as a form of every day, like breathing then think of the breathing as a living thing that begins to change and then it disappears and the world once you get it but then you just go what you are wa

197

2017-01-12 12:01:25
поток тёк - потёк ток

196

2017-01-12 12:00:56
class I {
    static $i = 0;
    public function __construct($val)
    {
        $this->val = $val;
        $this->cnt = ++self::$i;
    }
    public function __invoke() {
        echo "called for ", $this->cnt, PHP_EOL;
        return $this->val;
    }
}

$f = function($a, $b) {
    return $a && !$b || !$a && $b;
};

foreach([true, false] as $a)
{
    $a = new I($a);
    foreach([true, false] as $b)
    {
        $b = new I($b);
//        var_dump($a, $b, $a() && !$b() || !$a() && $b());
        var_dump($a, $b, $a() xor $b());
        echo PHP_EOL, PHP_EOL;
    }
}
20 19 18 17 1