Skip to content

Instantly share code, notes, and snippets.

@pwm
pwm / lens.php
Created August 2, 2018 18:42
lenses
<?php
declare(strict_types=1);
namespace Lens;
use Closure;
class Lens
{
/** @var Closure */
@pwm
pwm / actors.php
Last active July 5, 2018 09:09
Actors
<?php
declare(strict_types=1);
namespace RFSM3;
use Pwm\SFlow\FSM;
use Pwm\SFlow\Transition;
require_once __DIR__ . '/../vendor/autoload.php';
@pwm
pwm / composed.php
Last active October 24, 2018 10:25
Compose (unary) functions
<?php
declare(strict_types=1);
final class Compose
{
public static function these(Closure ...$closures): Closure
{
return array_reduce($closures, function (Closure $composed, Closure $f): Closure {
return function ($x) use ($f, $composed) {
return $f($composed($x));
@pwm
pwm / rfsm1.php
Last active July 2, 2018 15:03
Reactive/Dependent FSMs
<?php
declare(strict_types=1);
interface Subject {
public function attach(Observer $observer): void;
}
interface Observer {
public function reactTo(string $event): void;
}
@pwm
pwm / misu.hs
Last active August 24, 2021 07:39
"Make illegal states unrepresentable" (Yaron Minsky)
{-# OPTIONS_GHC -Wall #-}
-- An example of representing a connection
-- Types
data ConnInfo = ConnInfo
{ connState :: ConnState
, server :: String
} deriving (Show)
@pwm
pwm / map_to_list_to_map.php
Last active October 8, 2018 21:07
map list map list
<?php
declare(strict_types=1);
function mapToList(array $map): array
{
$list = [];
foreach ($map as $k => $v) {
$list[] = \is_array($v)
? [$k, mapToList($v)]
: [$k, $v];
@pwm
pwm / uuidv4_type.php
Last active April 13, 2018 12:37
UuidV4 Doctrine DBAL type
<?php
declare(strict_types=1);
use Pwm\UuidV4\UuidV4;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
final class UuidV4Type extends Type
{
@pwm
pwm / datetime_wtf.php
Last active March 30, 2018 04:38
Datetimes are hard to get right...
<?php
/*
* The timezone in these datetimes look the same but they are in fact represent different UTC offsets.
* The 1st one maps to UTC+01 (BST) while the 2nd one maps to a UTC+00 (GMT).
* This is because daylight saving time (DST) aka. clocks going forward/backward happen on different days in different years.
*/
$now = new DateTime('2018-03-26T08:00:00', new DateTimeZone('Europe/London'));
$inAYear = new DateTime('2019-03-26T08:00:00', new DateTimeZone('Europe/London'));
@pwm
pwm / cata.php
Created March 23, 2018 22:24
Treefolding/Treemapping
<?php
declare(strict_types=1);
function Node($node, array $subtree = []): array {
return [
'node' => $node,
'subtree' => $subtree
];
}
@pwm
pwm / immutable_bidirectional_ref.php
Last active April 10, 2018 21:09
Immutable bidirectional reference
<?php
declare(strict_types=1);
// https://softwareengineering.stackexchange.com/questions/334305/how-to-model-a-circular-reference-between-immutable-objects-in-c
final class Department {
/** @var string */
private $name;
/** @var Closure|User */
private $user;