Skip to content

Instantly share code, notes, and snippets.

View jesseschalken's full-sized avatar

Jesse Schalken jesseschalken

View GitHub Profile
<?hh // strict
namespace HackUtils;
final class SystemError extends \RuntimeException {
const int EPERM = 1;
const int ENOENT = 2;
const int ESRCH = 3;
const int EINTR = 4;
const int EIO = 5;
<?php
function json_emit($x, $nl = "\n") {
if (is_array($x)) {
if (!$x)
return '[]';
$nl2 = $nl . ' ';
if (is_assoc($x)) {
$parts = array();
foreach ($x as $k => $v) {
@jesseschalken
jesseschalken / double-conversion.patch
Last active September 15, 2016 05:48
AUR double-conversion
diff --git a/PKGBUILD b/PKGBUILD
index 201632c..893f879 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -4,27 +4,30 @@
pkgname=double-conversion
pkgver=2.0.1
-pkgrel=2
+pkgrel=3
<?php
ini_set('memory_limit', '-1');
function is_vector(array $x) {
return array_values($x) === $x;
}
function test($name, array $x) {
$t = microtime(true);
<?hh // strict
namespace JesseSchalken\HackTypes;
class TypeError<T> extends \Exception {
public function __construct(private mixed $x, private Type<T> $type) {
parent::__construct();
}
}
import fs = require('fs')
type NodeCallback<T> = (err:any, data:T) => void
/**
* Accepts a function accepting a Node.js style callback, and calls it, returning a Promise
*/
const promisify = <T>(func:(cb:NodeCallback<T>) => void) =>
new Promise<T>((resolve, reject) => {
@jesseschalken
jesseschalken / Uri.php
Last active January 28, 2022 08:14
Simple implementation of \Psr\Http\Message\UriInterface
<?php
class Uri implements \Psr\Http\Message\UriInterface {
const PASS = 'pass';
const USER = 'user';
const PORT = 'port';
const SCHEME = 'scheme';
const QUERY = 'query';
const FRAGMENT = 'fragment';
const HOST = 'host';
interface Interface_ {
public function getName();
public function getMethods();
public function getExtends();
}
interface HasTraits {
public function getTraits():array;
@jesseschalken
jesseschalken / README.md
Last active April 6, 2016 04:20
Transaction API for nested transactions in SQL databases. PHP 5.3+

Usage

This library implements nesting of transactions so code that uses transactions can be safely and arbitrarily composed.

There is a stack of in-progress transactions. Calling $txn = new Transaction(...) adds a transaction to the top of the stack. Calling $txn->commit() removes a transaction from the stack (and releases the savepoint). Calling $txn->rollback() rolls back a transaction and all transactions above it on the stack.

The transaction at the bottom of the stack represents a real START TRANSACTION/COMMIT/ROLLBACK. Transactions above the root transaction represent a SAVEPOINT .../ROLLBACK TO .../RELEASE SAVEPOINT .... Committing the root transaction does not COMMIT until all transactions on the stack have been committed or rolled back.

Nothing happens when a Transaction object falls out of scope. A transaction that is never committed or rolled back remains stuck on the stack and prevents the root transaction from committing. A Transaction object which commits on `__d

<?php
namespace JesseSchalken;
/**
* @param mixed $x
* @return string
*/
function assert_string($x) {
if (is_string($x)) {