Skip to content

Instantly share code, notes, and snippets.

@kaja47
kaja47 / gist:5900076
Last active December 19, 2015 04:49
Generators and Promises in PHP
<?php
// synchronous code
// fetch operations are blocking
$profile = fetchProfile('@kaja47');
$ids = [];
$cursor = '-1';
do {
@kaja47
kaja47 / access.php
Last active December 18, 2015 21:19
Atrox\Access examples
<?php
$_ = new Atrox\Access();
$_->property;
// generates:
@kaja47
kaja47 / mem-test.php
Last active December 18, 2015 15:39
PHP memory test
<?php
$testNumber = $argv[1];
const TEST_SIZE = 100000;
class TestPriv {
private $a = 1;
private $b = 2;
private $c = 3;
@kaja47
kaja47 / gist:5743608
Last active December 18, 2015 06:59
I'm pretty sure, PHP guys will crucify me for this abomination.
<?php
// The only chance now, I felt was the possibility that we'd gone to such
// excess that nobody in the position to bring the hammer down on us could
// possibly believe it.
// Hunter S. Thompson
trait CompactClass {
private $data;
@kaja47
kaja47 / IntArray.php
Last active December 18, 2015 01:39
Unboxed int array in PHP
<?php
// advanced bit-twiddling
abstract class AbstractIntArray {
protected $length;
protected $data;
protected $typeSize;
function __construct($length) {
@kaja47
kaja47 / xorswap.scala
Created June 4, 2013 01:50
Dr. Strangelove or: How I Learned to Swap Two Variables In Place and Love the XOR
var a: Int = 100
var b: Int = 200
a = a ^ b
b = b ^ a
a = a ^ b
println(a) // 200
println(b) // 100
@kaja47
kaja47 / gist:5610375
Created May 20, 2013 04:13
PHP or: how I learned to stop worrying and define functions with reserved names
<?php
class Magic {
function _list() { return "list"; }
function _function() { return "function"; }
function _and() { return "and"; }
function __call($name, $args) { return $this->{"_$name"}(); }
}
<?php
namespace Really\Long\Namespace;
class A {
static function myfunc() {}
const myfunc = 'Really\Long\Namespace\A::myfunc'
}
class A { val l: Long = 47 }
class B { val a: Int = 0; val b: Int = 0; def aa = a; def bb = b }
val a = new A
val b = new B
unsafe.putInt(a, 8, unsafe.getInt(b, 8))
val a_b = a.asInstanceOf[B]
a_b.getClass // B
@kaja47
kaja47 / gist:4407844
Created December 29, 2012 16:19
React.PHP promise timeout
<?php
use React\Promise\Deferred;
function timeout($time, $promise, $loop) {
$defer = new Deferred;
$r = $defer->resolver();
$sig = $loop->addTimer($time, function() use($r) {
$r->reject('timeout');
});