Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * A Maybe Monad class, encapsulates null checking
- */
- class Maybe {
- private $value;
- public function __construct($value=null) {
- $this->value = $value;
- }
- public function isEmpty() {
- return is_null($this->value);
- }
- public function nonEmpty() {
- return !$this->isEmpty();
- }
- public function map($fn) {
- if ($this->isEmpty()) {
- return $this;
- } else {
- return new Maybe($fn($this->value));
- }
- }
- public function getOrElse($default) {
- if ($this->isEmpty()) {
- return $default;
- }
- return $this->value;
- }
- }
Add Comment
Please, Sign In to add comment