Skip to content

instances for First and Last #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Data/Unfoldable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Data.Unfoldable

import Prelude

import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Maybe (Maybe(..), fromJust, isNothing, maybe)
import Data.Semigroup.First (First(..))
import Data.Semigroup.Last (Last(..))
import Data.Traversable (class Traversable, sequence)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Unfoldable1 (class Unfoldable1, unfoldr1, singleton, range, replicate1, replicate1A)
Expand All @@ -41,6 +43,17 @@ class Unfoldable1 t <= Unfoldable t where
instance unfoldableArray :: Unfoldable Array where
unfoldr = unfoldrArrayImpl isNothing (unsafePartial fromJust) fst snd

instance unfoldableFirst :: Unfoldable First where
unfoldr step x = First (maybe Nothing (Just <<< fst) (step x))

instance unfoldableLast :: Unfoldable Last where
unfoldr step x = go Nothing x
where
go accum i =
case step i of
Just (Tuple a i') -> go (Just a) i'
Nothing -> Last accum

instance unfoldableMaybe :: Unfoldable Maybe where
unfoldr f b = fst <$> f b

Expand Down
14 changes: 13 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Prelude

import Data.Eq (class Eq1)
import Data.Maybe (Maybe(..))
import Data.Semigroup.First (First(..))
import Data.Semigroup.Last (Last(..))
import Data.Tuple (Tuple(..), uncurry)
import Data.Unfoldable as U
import Data.Unfoldable1 as U1
Expand Down Expand Up @@ -36,10 +38,14 @@ main = do

log "Test none"
assert $ U.none == ([] :: Array Unit)
assert $ U.none == First Nothing :: First Unit
assert $ U.none == Last Nothing :: Last Unit

log "Test singleton"
assert $ U.singleton unit == [unit]
assert $ U1.singleton unit == NonEmpty unit []
assert $ U.singleton unit == First (Just unit)
assert $ U.singleton unit == Last (Just unit)

log "Test replicate"
assert $ U.replicate 0 "foo" == []
Expand All @@ -54,14 +60,20 @@ main = do
]

log "Test range"
assert $ U.range 1 0 == []
assert $ U.range 0 0 == [0]
assert $ U.range 0 2 == [0, 1, 2]
assert $ U.range 0 2 == First (Just 0)
assert $ U.range 0 2 == Last (Just 2)

assert $ U1.range 1 0 == [1, 0]
assert $ U1.range 0 0 == [0]
assert $ U1.range 0 2 == [0, 1, 2]
assert $ U1.range 1 0 == NonEmpty 1 [0]
assert $ U1.range 0 0 == NonEmpty 0 []
assert $ U1.range 0 2 == NonEmpty 0 [1, 2]

log "Test Maybe.toUnfoldable"
log "Test fromMaybe"
assert $ U.fromMaybe (Just "a") == ["a"]
assert $ U.fromMaybe (Nothing :: Maybe String) == []

Expand Down