From baf591b94c5b257204ddf1b5fc1c357c4a057b87 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 May 2020 17:52:40 -0700 Subject: [PATCH 1/3] Show Maybe's "do notation" similarity to nested if-then-else statement --- src/Data/Maybe.purs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Data/Maybe.purs b/src/Data/Maybe.purs index 9cd65cc..cc1f039 100644 --- a/src/Data/Maybe.purs +++ b/src/Data/Maybe.purs @@ -141,6 +141,23 @@ instance bindMaybe :: Bind Maybe where -- | ``` purescript -- | x >>= (\x' -> y >>= (\y' -> pure (f x' y'))) -- | ``` +-- | +-- | and is conceptually similar to writing... +-- | ``` +-- | var x = // ... +-- | if x.isNothing { +-- | return x; // Nothing +-- | } else { +-- | var x' = x.unwrapJust; +-- | var y = // ... +-- | if y.isNothing { +-- | return y; // Nothing +-- | } else { +-- | var y' = y.unwrapJust; +-- | return wrapIntoJust(f(x', y')); +-- | } +-- | } +-- | ``` instance monadMaybe :: Monad Maybe instance monadZeroMaybe :: MonadZero Maybe From 17da8278b8bc7e455d6cfacb45f03e50107bce27 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 13 Oct 2020 17:47:18 -0700 Subject: [PATCH 2/3] Desugar `bind` into pattern matching --- src/Data/Maybe.purs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Data/Maybe.purs b/src/Data/Maybe.purs index cc1f039..9801ea9 100644 --- a/src/Data/Maybe.purs +++ b/src/Data/Maybe.purs @@ -142,6 +142,16 @@ instance bindMaybe :: Bind Maybe where -- | x >>= (\x' -> y >>= (\y' -> pure (f x' y'))) -- | ``` -- | +-- | Which is equivalent to: +-- | +-- | ``` purescript +-- | case x of +-- | Nothing -> Nothing +-- | Just x' -> case y of +-- | Nothing -> Nothing +-- | Just y' -> Just (f x' y') +-- | ``` +-- | -- | and is conceptually similar to writing... -- | ``` -- | var x = // ... From 37ec4fa4e1aeb0eae79b6d482341ee0ce2495614 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 13 Oct 2020 18:05:40 -0700 Subject: [PATCH 3/3] Remove JavaScript example of Maybe's do notation --- src/Data/Maybe.purs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/Data/Maybe.purs b/src/Data/Maybe.purs index 9801ea9..1360c69 100644 --- a/src/Data/Maybe.purs +++ b/src/Data/Maybe.purs @@ -151,23 +151,6 @@ instance bindMaybe :: Bind Maybe where -- | Nothing -> Nothing -- | Just y' -> Just (f x' y') -- | ``` --- | --- | and is conceptually similar to writing... --- | ``` --- | var x = // ... --- | if x.isNothing { --- | return x; // Nothing --- | } else { --- | var x' = x.unwrapJust; --- | var y = // ... --- | if y.isNothing { --- | return y; // Nothing --- | } else { --- | var y' = y.unwrapJust; --- | return wrapIntoJust(f(x', y')); --- | } --- | } --- | ``` instance monadMaybe :: Monad Maybe instance monadZeroMaybe :: MonadZero Maybe