Skip to content

Commit 72acbbb

Browse files
eskimorpaf31
authored andcommitted
Added toUnFoldable function for Data.Maybe (#8)
* Added toUnFoldable function for Data.Maybe This needs to go to the unfoldable package instead of the maybe package to avoid a circular dependency. * Added test for Maybe.toUnfoldable * Maybe.toUnfoldable -> fromMaybe as suggested by paf31.
1 parent 6cd0679 commit 72acbbb

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/Data/Unfoldable.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Data.Unfoldable
1010
, replicateA
1111
, none
1212
, singleton
13+
, fromMaybe
1314
) where
1415

1516
import Prelude
@@ -84,3 +85,7 @@ none = unfoldr (const Nothing) unit
8485
-- | ~~~
8586
singleton :: forall f a. Unfoldable f => a -> f a
8687
singleton = replicate 1
88+
89+
-- | Convert a Maybe to any Unfoldable like lists and arrays.
90+
fromMaybe :: forall f a. Unfoldable f => Maybe a -> f a
91+
fromMaybe = unfoldr (\b -> flip Tuple Nothing <$> b)

test/Main.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ main = do
4242
[2,1,1],[2,1,2], [2,2,1],[2,2,2]
4343
]
4444

45+
log "Test Maybe.toUnfoldable"
46+
assert $ U.fromMaybe (Just "a") == ["a"]
47+
assert $ U.fromMaybe (Nothing :: Maybe String) == []
48+
4549
log "All done!"

0 commit comments

Comments
 (0)