Created
June 26, 2016 09:57
-
-
Save eugene-dounar/1b094a2709df1a1976c12d63adce62a0 to your computer and use it in GitHub Desktop.
Haskell vs PHP: simple domain logic example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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