From f910e222f321e1e996665de1db8816fa013a1223 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 15 Feb 2019 11:46:56 -0800 Subject: [PATCH 1/2] Make fromLeft/fromRight total functions by providing default values --- src/Data/Either.purs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Data/Either.purs b/src/Data/Either.purs index 761b85a..365ef05 100644 --- a/src/Data/Either.purs +++ b/src/Data/Either.purs @@ -251,15 +251,17 @@ isLeft = either (const true) (const false) isRight :: forall a b. Either a b -> Boolean isRight = either (const false) (const true) --- | A partial function that extracts the value from the `Left` data constructor. --- | Passing a `Right` to `fromLeft` will throw an error at runtime. -fromLeft :: forall a b. Partial => Either a b -> a -fromLeft (Left a) = a - --- | A partial function that extracts the value from the `Right` data constructor. --- | Passing a `Left` to `fromRight` will throw an error at runtime. -fromRight :: forall a b. Partial => Either a b -> b -fromRight (Right a) = a +-- | A function that extracts the value from the `Left` data constructor. +-- | Passing a `Right` to `fromLeft` will return the default value. +fromLeft :: forall a b. a -> Either a b -> a +fromLeft _ (Left a) = a +fromLeft default _ = default + +-- | A function that extracts the value from the `Right` data constructor. +-- | Passing a `Left` to `fromRight` will return the default value. +fromRight :: forall a b. b -> Either a b -> b +fromRight _ (Right b) = b +fromRight default _ = default -- | Takes a default and a `Maybe` value, if the value is a `Just`, turn it into -- | a `Right`, if the value is a `Nothing` use the provided default as a `Left` From 9b0a6e955f97f4406fb673a2ad8de3e3bd881146 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Sat, 23 Feb 2019 14:20:03 -0800 Subject: [PATCH 2/2] Update documentation to use hdgarrood's suggestion --- src/Data/Either.purs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Data/Either.purs b/src/Data/Either.purs index 365ef05..ce7a46a 100644 --- a/src/Data/Either.purs +++ b/src/Data/Either.purs @@ -252,13 +252,15 @@ isRight :: forall a b. Either a b -> Boolean isRight = either (const false) (const true) -- | A function that extracts the value from the `Left` data constructor. --- | Passing a `Right` to `fromLeft` will return the default value. +-- | The first argument is a default value, which will be returned in the +-- | case where a `Right` is passed to `fromLeft`. fromLeft :: forall a b. a -> Either a b -> a fromLeft _ (Left a) = a fromLeft default _ = default -- | A function that extracts the value from the `Right` data constructor. --- | Passing a `Left` to `fromRight` will return the default value. +-- | The first argument is a default value, which will be returned in the +-- | case where a `Left` is passed to `fromRight`. fromRight :: forall a b. b -> Either a b -> b fromRight _ (Right b) = b fromRight default _ = default