Skip to content

Instantly share code, notes, and snippets.

View bzikarsky's full-sized avatar

Benjamin Zikarsky bzikarsky

View GitHub Profile
<?hh
async function stream_check(resource $stream, string $mode, int $usec): Awaitable<void>
{
$r = $w = $e = null;
do {
if ($mode == "r") {
$r = Vector{$stream};
}
@bzikarsky
bzikarsky / mongo_session_bench.php
Created November 20, 2014 07:30
Benchmark session lifetime condition: Mongo vs PHP
<?php
$mongo = new MongoClient();
$db = $mongo->test;
$collection = $db->selectCollection('session');
$sessionCount = $argv[1];
$sampleCount = $argv[2];
$samples = [];
// Setup collection
@bzikarsky
bzikarsky / test.bash
Last active August 29, 2015 14:10
Test timezone behavior with PHP and Mongo
#!/bin/bash
DB="test"
COL="tz"
ZONES="London Berlin Istanbul"
DATE="2000-01-01"
echo "System timezone: $(date -R)"
echo ""
#!/bin/bash
DB="test"
COL="tz"
ZONES="London Berlin Istanbul"
DATE="2000-01-01"
echo "System timezone: $(date -R)"
echo ""
@bzikarsky
bzikarsky / example.php
Last active August 29, 2015 14:13
Resolvable generators
<?php
// this is our generator, dns\* and socket\* are assumed
// to be async functions returning promises
function read_data_from($hostname, $port) {
$ip = yield dns\resolve($hostname);
$socket = yield socket\connect($ip, $port);
echo yield socket\read(1024);
};
@bzikarsky
bzikarsky / Enum.php
Created October 21, 2010 12:44
Provides an enum-like type (as in Java) for PHP, @see http://gist.github.com/638426 for example usage
<?php
/**
* This class provides an enum-like type (as in Java) for PHP
*
* To create an enum, the enum has to extends this Enum class and defined
* a static protected array $enum with its entites.
* This array can be either a normal collection, e.g.
*
* <code>
@bzikarsky
bzikarsky / Enum.examples.php
Created October 21, 2010 12:51
Several examples for Enum.php {@see http://gist.github.com/638407}
<?php
class Gender extends Enum
{
protected static $enum = array(
'MALE' => array('male', 'm', 'Dear Mr.'),
'FEMALE' => array('female', 'f', 'Dear Ms.')
);
@bzikarsky
bzikarsky / NumberToWordConverter.php
Created December 21, 2010 23:13
Verboce-/OO-Version of a numer to german word algorithm
<?php
class NumberToWordConverter
{
private $numbers = array(
'', 'ein', 'zwei', 'drei', 'vier', 'fünf', 'sech',
'sieb', 'acht', 'neun', 'zehn', 'elf', 'zwölf'
);
private $numbersSingle = array(
@bzikarsky
bzikarsky / vm.js
Created December 6, 2011 07:17
Solution to canyoucrackit#2
String.prototype.lpad = function(length, padString) {
var str = this;
if (padString == undefined) {
padString = ' ';
}
while (str.length < length) {
str = padString + str;
}
<html>
<head>
<title>VM</title>
<style type="text/css">
body {
font-family: "Courier New";
font-size: .8em;
}