Skip to content

Instantly share code, notes, and snippets.

@eugene-dounar
Created June 26, 2016 09:57
Show Gist options
  • Save eugene-dounar/1b094a2709df1a1976c12d63adce62a0 to your computer and use it in GitHub Desktop.
Save eugene-dounar/1b094a2709df1a1976c12d63adce62a0 to your computer and use it in GitHub Desktop.
Haskell vs PHP: simple domain logic example
newtype Rule = Rule String deriving (Show)
class RuleSet a where
getRules :: a -> [Rule]
data Bus = Bus [Rule] deriving (Show)
instance RuleSet Bus where
getRules (Bus rules) = rules
data Net = Net Bus [Rule] deriving (Show)
instance RuleSet Net where
getRules (Net bus rules) = getRules(bus) ++ rules
bus = Bus [Rule "Rule 1", Rule "Rule 2"]
net = Net bus [Rule "Own net rule"]
main = putStrLn(show $ getRules(net))
<?php
class Rule {
private $text;
function __construct($text) {
$this->text = $text;
}
}
interface RuleSet {
function getRules();
}
class Bus implements RuleSet {
private $rules;
function __construct(array $rules) {
$this->rules = $rules;
}
function getRules() {
return $this->rules;
}
}
class Net implements RuleSet {
private $ownRules;
private $bus;
function __construct(Bus $bus, array $ownRules) {
$this->bus = $bus;
$this->ownRules = $ownRules;
}
function getRules() {
return array_merge($this->bus->getRules(), $this->ownRules);
}
}
$bus = new Bus([new Rule("Rule 1"), new Rule("Rule 2")]);
$net = new Net($bus, [new Rule("Own rule")]);
var_dump($net->getRules());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment