Maybe now can be pattern matched
authorwidmogrod <widmogrod@gmail.com>
Fri, 1 Jun 2018 19:49:02 +0000 (21:49 +0200)
committerwidmogrod <widmogrod@gmail.com>
Fri, 1 Jun 2018 19:49:02 +0000 (21:49 +0200)
src/Monad/Maybe/Just.php
src/Monad/Maybe/Nothing.php

index 3cfb4ab..7ded5fc 100644 (file)
@@ -7,8 +7,9 @@ namespace Widmogrod\Monad\Maybe;
 use Widmogrod\Common;
 use FunctionalPHP\FantasyLand;
 use Widmogrod\Primitive\TypeMismatchError;
+use Widmogrod\Useful\PatternMatcher;
 
-class Just implements Maybe
+class Just implements Maybe, PatternMatcher
 {
     use Common\PointedTrait;
 
@@ -92,4 +93,12 @@ class Just implements Maybe
     {
         return $function($accumulator, $this->value);
     }
+
+    /**
+     * @inheritdoc
+     */
+    public function patternMatched(callable $fn)
+    {
+        return $fn($this->value);
+    }
 }
index 538990e..f0237dd 100644 (file)
@@ -5,8 +5,9 @@ declare(strict_types=1);
 namespace Widmogrod\Monad\Maybe;
 
 use FunctionalPHP\FantasyLand;
+use Widmogrod\Useful\PatternMatcher;
 
-class Nothing implements Maybe
+class Nothing implements Maybe, PatternMatcher
 {
     const of = 'Widmogrod\Monad\Maybe\Nothing::of';
 
@@ -84,4 +85,12 @@ class Nothing implements Maybe
     {
         return $accumulator;
     }
+
+    /**
+     * @inheritdoc
+     */
+    public function patternMatched(callable $fn)
+    {
+        return $fn();
+    }
 }