Skip to content

Instantly share code, notes, and snippets.

View mathiasverraes's full-sized avatar

Mathias Verraes mathiasverraes

View GitHub Profile
@mathiasverraes
mathiasverraes / ducktyping.php
Created November 24, 2016 18:55
duck typing
<?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 November 24, 2016 18:24
faking method overloading
<?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
}
@mathiasverraes
mathiasverraes / instanceof.php
Last active November 24, 2016 18:14
Instanceof rant
<?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 November 16, 2016 16:21
Extensible Monoidal FizzBuzz in Haskell
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"]
@mathiasverraes
mathiasverraes / Workflow.hs
Created August 16, 2016 08:45
haskell (publishing) workflow experiment
module Workflow where
import Data.Maybe
import Data.Set
newtype State = State String deriving(Ord, Show, Eq)
type States = Set State
data Action = Action String deriving(Eq, Show)
type Workflow = [Transition]
data Transition = Transition {
@mathiasverraes
mathiasverraes / zeromq.md
Last active September 23, 2021 13:29
zeromq php7
git clone git://github.com/mkoppanen/php-zmq.git
cd php-zmq
phpize && ./configure
make && make install

Finally add the following line to your php.ini:

@mathiasverraes
mathiasverraes / Maze.hs
Last active August 4, 2016 17:40
initial PoC for a maze game in Haskell
module Lib where
import Data.List.Split
import System.Random
a4by4Maze :: Maze
a4by4Maze = Maze [
[(╝),(╦),(╝),(╣)],
[(╩),(╣),(╗),(╝)],
[(═),(╩),(║),(╝)],
@mathiasverraes
mathiasverraes / LeSighTest.php
Created June 30, 2016 12:48
PHP string casting
<?php
final class FuckYouPHPTest extends \PHPUnit_Framework_TestCase {
/** @test */
public function should_fail () {
new WantsAString(new NotAString());
// Fails as expected
}
/** @test */
{-# LANGUAGE ExistentialQuantification #-}
module Beauforma where
import qualified Data.Map.Strict as M
data Event =
GuestBookedAppointment {
appointmentId :: Guid,
guestId :: GuestId,
appointmentDateAndTime :: DateTime,
@mathiasverraes
mathiasverraes / Money.hs
Last active June 21, 2016 21:08
fooling around with haskell types and money
{-# LANGUAGE ExistentialQuantification #-}
module Money where
data Money a = Currency a => Money {currency :: a, amount :: Float}
instance Show (Money a) where
show (Money c a) = show c ++ " " ++ show a
instance Eq (Money a) where
(Money cx x) == (Money cy y) = cx == cy && x == y
class (Eq a, Show a) => Currency a where symbol :: a
data EUR = EUR deriving (Show, Eq)