Skip to content

Instantly share code, notes, and snippets.

View morrisonlevi's full-sized avatar

Levi Morrison morrisonlevi

View GitHub Profile

In PHP anonymous functions and closures are really useful for callbacks but they are verbose:

array_map(function($x) {
    return $x * $x * $x; 
}, $input);

The short-closure proposal by Bob tries to address this by introducing ~>:

array_map($x ~> $x * $x * $x, $input);

// or

@morrisonlevi
morrisonlevi / small_ptr.hh
Last active August 29, 2015 14:27
A discriminated or tagged pointer that uses variadic templates and takes only 48 bits of memory.
#ifndef SMALL_PTR_HH
#define SMALL_PTR_HH
#include <cassert>
#include <cstdint>
#include <cstring>
static_assert(sizeof(void *) == 8, "Pointers must be 8 bytes");
static_assert(sizeof(uintptr_t) == 8, "uintptr_t must be 8 bytes");

References in PHP suck. Here's an example:

$foo = 1;
func($foo);
// Has $foo changed value?
// You *must* know the definition of func

You may write this off as crazy -- of course you need to be familiar with func to use it, right? Well… depends on who "you" is. I've debugged code that I didn't write many times. Often it calls code that I also didn't write and am not familiar with. In order to know where the variable I care about changes requires me to look at assignments and every single function where it is used as a parameter. This majorly sucks.

Debugging isn't the only area where references suck. Remember how we cannot know if any given function passes a parameter by reference? This makes optimizing code more difficult. In general it makes writing any code that reasons about code more difficult.

@morrisonlevi
morrisonlevi / EmptyNode.php
Created March 25, 2015 16:07
Implementing a very basic set using inheritance / polymorphism
<?php
class EmptyNode implements Node {
function add($value) {
return new ValueNode($this, $this, $value);
}
function contains($value) {
return false;
}
@morrisonlevi
morrisonlevi / zend_status.diff
Created December 27, 2014 00:01
Some conversions from int to zend_status.
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index 7e7cd06..838f400 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -43,7 +43,7 @@ static zend_module_entry **module_post_deactivate_handlers;
static zend_class_entry **class_cleanup_handlers;
/* this function doesn't check for too many parameters */
-ZEND_API int zend_get_parameters(int ht, int param_count, ...) /* {{{ */
+ZEND_API zend_status zend_get_parameters(int ht, int param_count, ...) /* {{{ */
@morrisonlevi
morrisonlevi / doc.html
Created December 3, 2014 20:35
Transform super basic DocBook into HTML 5 via XSLT.
<!DOCTYPE html SYSTEM "about:legacy-compat">
<html>
<head>
<title>Products</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<main>
<h1>Product A</h1>
@morrisonlevi
morrisonlevi / C++11ForDynamicLanguage.md
Last active August 29, 2015 14:06
Features of C++11 that I think would be helpful in implementing a dynamic language.

These features of C++11 are ones that I think would be useful when implementing a dynamic language, such as PHP or Python.

  • Shared and unique pointers. These are provided by the standard library in C++11 making memory management easier and safer. I consider this to be the biggest feature of C++11 that would help when implementing a dynamic language.
  • Enum classes. Enum classes are scoped and strongly typed enums; you can also specify their underlying size.
  • Unions. C++11's unions are considerably more powerful than previous versions, notably unions can have member functions such as move constructors and move assignment operators. They can now contain complex types (such as shared pointers).
  • Rvalue references and move semantics. Rvalue references and move semantics are very useful for practically every C++ project and implementing a dynamic language is no exception; if you are not familiar with them you should go learn them.
  • Explicit operator overloading. It is useful to c
@morrisonlevi
morrisonlevi / pre-commit
Last active December 30, 2015 21:19
A git hook that won't let you commit on Tuesday or Wednesday in preparation for BC Break Thursday.
#!/usr/bin/env php
<?php
/**
* @installation Copy this file to .git/hooks/pre-commit if
* you do not have any pre-commit hooks.
* @policy Enforce BC Break Thursday
* @desc If we are within 2 days of Thursday, do not
* accept commits.
*/
@morrisonlevi
morrisonlevi / loops.php
Last active December 16, 2015 20:29
Empty Loop Micro-benchmarks. Useless except for curiosity.
<?php
define('ITERATIONS', 10000000);
$var = 0;
$start = microtime(true);
$var = 0;
while ($var < ITERATIONS) {
++$var;
}
@morrisonlevi
morrisonlevi / ReduceImplementations.php
Last active December 15, 2015 15:59
Performance tests of implementing reduce in PHP.
<?php
function add($a, $b) {
return $a + $b;
}
function foldl(array $array, $function, $initial) {
if (empty($array)) {
return $initial;
}