Skip to content

Commit 9090158

Browse files
Make fromLeft/fromRight total functions by providing default values (#48)
* Make fromLeft/fromRight total functions by providing default values * Update documentation to use hdgarrood's suggestion
1 parent b4da429 commit 9090158

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/Data/Either.purs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,19 @@ isLeft = either (const true) (const false)
251251
isRight :: forall a b. Either a b -> Boolean
252252
isRight = either (const false) (const true)
253253

254-
-- | A partial function that extracts the value from the `Left` data constructor.
255-
-- | Passing a `Right` to `fromLeft` will throw an error at runtime.
256-
fromLeft :: forall a b. Partial => Either a b -> a
257-
fromLeft (Left a) = a
258-
259-
-- | A partial function that extracts the value from the `Right` data constructor.
260-
-- | Passing a `Left` to `fromRight` will throw an error at runtime.
261-
fromRight :: forall a b. Partial => Either a b -> b
262-
fromRight (Right a) = a
254+
-- | A function that extracts the value from the `Left` data constructor.
255+
-- | The first argument is a default value, which will be returned in the
256+
-- | case where a `Right` is passed to `fromLeft`.
257+
fromLeft :: forall a b. a -> Either a b -> a
258+
fromLeft _ (Left a) = a
259+
fromLeft default _ = default
260+
261+
-- | A function that extracts the value from the `Right` data constructor.
262+
-- | The first argument is a default value, which will be returned in the
263+
-- | case where a `Left` is passed to `fromRight`.
264+
fromRight :: forall a b. b -> Either a b -> b
265+
fromRight _ (Right b) = b
266+
fromRight default _ = default
263267

264268
-- | Takes a default and a `Maybe` value, if the value is a `Just`, turn it into
265269
-- | a `Right`, if the value is a `Nothing` use the provided default as a `Left`

0 commit comments

Comments
 (0)