Skip to content

Instantly share code, notes, and snippets.

@marcosh
marcosh / monads.md
Created January 15, 2016 19:51
MONADS IN THE REALM OF OBJECT ORIENTED PROGRAMMING

A monad is a particular construction that emerges in the branch of mathematics called category theory. Despite category theory being one of the most abstract field in mathematics, monads are heavily used in functional programming as a design pattern that allows, for example, to manage side-effects. Why should such a powerful design pattern be used only in functional programming languages? Is it possible to apply it also in an Object Oriented paradigm?

In this talk we will try to answer these questions, exploring in parallel the notion of monad in category theory, in functional programming and in object oriented programming, with the help of various examples taken from all the three different worlds.

@marcosh
marcosh / Country.php
Created October 15, 2020 13:08
Value object external validation
<?php
final class Country
{
private string $countryCode;
private function __construct(string $countryCode)
{
$this->countryCode = $countryCode;
}
PDO::FETCH_FUNC without validation
10: 0.001568078994751
0.00071001052856445
0.00085306167602539
0.00087618827819824
0.00083112716674805
100: 0.0012631416320801
0.0014598369598389
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module NamedTypeclass where
import Prelude hiding (Monoid, mempty, (<>))
<?php
declare(strict_types=1);
/**
* @template F
* @template A
* @extends Functor<F,A>
*/
interface Apply extends Functor
@marcosh
marcosh / Boolean.php
Created January 16, 2020 11:16
Boolean implementation in PHP
<?php
declare(strict_types=1);
namespace Marcosh\PhpValidationDSL;
final class Boolean
{
/** @var Bool */
private $isTrue;
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Graph where
import Control.Comonad
-- we need to separate `a` and `b` because `a` is covariant while `b` is contravariant => this is actually a profuctor
data PointedGraph moves b a = PointedGraph
@marcosh
marcosh / Application.hs
Created June 14, 2018 13:20
Web applications as profunctors
module Application where
import Data.Profunctor
newtype Application request response = Application {unApplication :: request -> IO response}
instance Profunctor Application where
dimap actOnRequest actOnResponse application = Application $ (fmap actOnResponse) . (unApplication application) . actOnRequest

Characterize recursable data structures

Recursion

Recursion is a procedure that allows us to iterate through certain data structure. For example we can iterate through lists

sum : List Nat -> Nat
sum list =
module Hanoi
import Data.Vect
data Peg
= First
| Second
| Third
-- TODO : can we do better here?