Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
mindplay-dk / collections.md
Last active August 21, 2024 18:29
Linear collection workflow

You can have a linear workflow with the array functions.

The following is unreadable:

$output = array_reduce(
  array_map(
    function($n) { return $n ** 2; }
    array_filter($input, function($n) { return $n % 2 == 0; })
 ),
@mindplay-dk
mindplay-dk / middleware-host.php
Created August 16, 2016 12:52
An ultra-naiive universal middleware host
<?php
// an ultra-simple middleware host that works for middleware callables with any signature.
// the last argument is always the `$next` function, which delegates to the next middleware.
class Host
{
/**
* @var callable[]
*/
<?php
namespace Psr\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* This interface defines the formal method common to HTTP Middleware components.
*/
/**
* Automatically create ordered/unordered lists when detecting content like "* " or "1. "
*/
define(function () {
"use strict";
return function () {
return function (scribe) {
/**
@mindplay-dk
mindplay-dk / shared-toolbar.js
Last active March 21, 2016 10:21
Shared toolbars plugin for Guardian's Scribe editor
define(function () {
"use strict";
var active_scribe = null;
function create_handler_for(button) {
return function (event) {
if (!active_scribe) {
return;
}
@mindplay-dk
mindplay-dk / anonymous.php
Created March 10, 2016 11:25
class or object???
<?php
// anonymous classes are both instances and types. huh? what?
$type = new class {
public function foo() {
echo "YAY";
}
};
@mindplay-dk
mindplay-dk / dafuq.php
Created March 10, 2016 09:19
DAFUQ PHP?
<?php
function kthxbai() {
$result = "hey";
try {
return $result;
} finally {
$result = "dafuq";
}
@mindplay-dk
mindplay-dk / Container.php
Last active February 16, 2022 16:37
Minimal DI container
<?php
class Container
{
/** @var Closure[] */
private $factories = [];
/** @var array */
private $components = [];
@mindplay-dk
mindplay-dk / blah.md
Last active February 16, 2016 07:38
Documenting accessors

How do you document model attributes implemented using synchronous accessor methods?

Example - bare model, no documentation:

class Contact
{
    protected $phone;
    
    public function getPhone() {
@mindplay-dk
mindplay-dk / hooks.ts
Last active October 26, 2015 10:07
Type-safe event hooks and boxed values (aka "getter-setters") in Typescript
/// This interface defines an event listener
interface IListener<Event> {
(event: Event): void
}
/// This interface represents an event hook
interface IHook<Event> {
/// Attach a handler to this hookable
(handler: IListener<Event>): void