Skip to content

Instantly share code, notes, and snippets.

Mathias Verraes mathiasverraes

View GitHub Profile
@mathiasverraes
mathiasverraes / rollyourown.php
Created May 30, 2018
We don't need no DIC libs / we don't need no deps control
View rollyourown.php
<?php
// Context: I'm trying to argue that DI (and DIC) are great, and DIC libs suck.
// Happy to be proven wrong!
final class Router {
private $dependencies;
public function __construct (Dependencies $dependencies) {
$this->dependencies = $dependencies;
// You might say that this is Service Locator, but it's not. This router is toplevel,
// and toplevel must have access to dependencies. After that it can all just bubble nicely using proper DI.
@mathiasverraes
mathiasverraes / .gitignore
Created May 21, 2018
gitignore a folder except the gitignore file
View .gitignore
*
!.gitignore
@mathiasverraes
mathiasverraes / gist:b6673d1a1e010b2db0db46ac11eb80c8
Created Apr 23, 2018 — forked from jimbojsb/gist:1630790
Code highlighting for Keynote presentations
View gist:b6673d1a1e010b2db0db46ac11eb80c8

Step 0:

Get Homebrew installed on your mac if you don't already have it

Step 1:

Install highlight. "brew install highlight". (This brings down Lua and Boost as well)

Step 2:

@mathiasverraes
mathiasverraes / fib.php
Created Nov 14, 2017
Supposedly Lazy Fibonacci
View fib.php
<?php
foreach (fib() as $x) echo "$x\n";
function fib () {
yield 0; yield 1;
$f = fib(); $g = fib();
$g->next();
while (true) {
yield $f->current() + $g->current();
$f->next(); $g->next();
View Maybe.php
<?php declare(strict_types=1);
// In reaction to @marcoshuttle's http://marcosh.github.io/post/2017/06/16/maybe-in-php.html
// Warning: none of this code has been tested or even run.
namespace Verraes\Maybe;
interface Just extends Maybe {
// We can only extract if we know it's a Just
View water.php
<?php declare(strict_types = 1);
function volume (array $levels) {
return array_sum(
zipWith(
'subtract',
zipWith(
'min',
scanl('max', $levels),
scanr('max', $levels)
View ducktyping.php
<?php
// duck typing
final class Foo {
public function bar ($object) {
$object->quack();
}
}
// Because we know nothing about $object, it's only by calling quack that we can
// tell whether object supports quack()
@mathiasverraes
mathiasverraes / file.php
Created Nov 24, 2016
faking method overloading
View file.php
<?php
// method overloading example (not possible in php)
final class SomeEventListener {
public function when(EmployeeWasHired $event) {
// set a salary
}
public function when(EmployeeWasPromoted $event) {
// increase salary
}
View instanceof.php
<?php
final class Foo {
public function bar (AnemicObject $object) {
switch (true) {
case $object instanceof Alfa:
// do alpha shizzle
case $object instanceof Beta:
// do beta shizzle
}
@mathiasverraes
mathiasverraes / MonoidalFizzBuzz.hs
Created Nov 16, 2016
Extensible Monoidal FizzBuzz in Haskell
View MonoidalFizzBuzz.hs
module MonoidalFizzBuzz where
import Data.Monoid
import Data.Maybe
-- based on @mittie https://twitter.com/mittie/status/798257339736453120
monoidalFizzbuzz = zipWith fromMaybe numbers strings
where
fizzes = cycle [Nothing, Nothing, Just "Fizz"]
buzzes = cycle [Nothing, Nothing, Nothing, Nothing, Just "Buzz"]
You can’t perform that action at this time.