Skip to content

Add Unfoldable1 instances for non-empty lists #152

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

Merged
merged 1 commit into from
Aug 19, 2018
Merged
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
5 changes: 4 additions & 1 deletion src/Data/List/Lazy/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Data.Ord (class Ord1, compare1)
import Data.Traversable (class Traversable, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Tuple (Tuple(..), snd)
import Data.Unfoldable (class Unfoldable)
import Data.Unfoldable (class Unfoldable, unfoldr1)
import Data.Unfoldable1 (class Unfoldable1)

-- | A lazy linked list.
Expand Down Expand Up @@ -270,6 +270,9 @@ instance traversableNonEmptyList :: Traversable NonEmptyList where
sequence (NonEmptyList nel) =
map (\xxs -> NonEmptyList $ defer \_ -> xxs) $ sequence (force nel)

instance unfoldable1NonEmptyList :: Unfoldable1 NonEmptyList where
unfoldr1 f b = NonEmptyList $ defer \_ -> unfoldr1 f b

instance functorWithIndexNonEmptyList :: FunctorWithIndex Int NonEmptyList where
mapWithIndex f (NonEmptyList ne) = NonEmptyList $ defer \_ -> mapWithIndex (f <<< maybe 0 (add 1)) $ force ne

Expand Down
2 changes: 2 additions & 0 deletions src/Data/List/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ derive newtype instance traversableNonEmptyList :: Traversable NonEmptyList

derive newtype instance foldable1NonEmptyList :: Foldable1 NonEmptyList

derive newtype instance unfoldable1NonEmptyList :: Unfoldable1 NonEmptyList

instance functorWithIndexNonEmptyList :: FunctorWithIndex Int NonEmptyList where
mapWithIndex fn (NonEmptyList ne) = NonEmptyList $ mapWithIndex (fn <<< maybe 0 (add 1)) ne

Expand Down
8 changes: 7 additions & 1 deletion test/Test/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.NonEmpty ((:|))
import Data.Traversable (traverse)
import Data.TraversableWithIndex (traverseWithIndex)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (unfoldr)
import Data.Unfoldable (replicate1, unfoldr)
import Data.Unfoldable1 (unfoldr1)
import Effect (Effect)
import Effect.Console (log)
Expand Down Expand Up @@ -427,6 +427,12 @@ testListLazy = do
log "map should maintain order"
assert $ (1..5) == map identity (1..5)

log "unfoldable replicate1 should be stack-safe for NEL"
void $ pure $ NEL.length $ (replicate1 100000 1 :: NEL.NonEmptyList Int)

log "unfoldr1 should maintain order for NEL"
assert $ (nel (1 :| l [2, 3, 4, 5])) == unfoldr1 step1 1

step :: Int -> Maybe (Tuple Int Int)
step 6 = Nothing
step n = Just (Tuple n (n + 1))
Expand Down
20 changes: 17 additions & 3 deletions test/Test/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ module Test.Data.List.NonEmpty (testNonEmptyList) where

import Prelude

import Effect (Effect)
import Effect.Console (log)
import Data.Foldable (class Foldable, foldM, foldMap, foldl, length)
import Data.FoldableWithIndex (foldlWithIndex, foldrWithIndex, foldMapWithIndex)
import Data.TraversableWithIndex (traverseWithIndex)
import Data.List as L
import Data.List.NonEmpty as NEL
import Data.Maybe (Maybe(..))
import Data.Monoid.Additive (Additive(..))
import Data.NonEmpty ((:|))
import Data.TraversableWithIndex (traverseWithIndex)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (replicate, replicate1, unfoldr, unfoldr1)
import Effect (Effect)
import Effect.Console (log)
import Test.Assert (assert)

testNonEmptyList :: Effect Unit
Expand Down Expand Up @@ -265,6 +266,19 @@ testNonEmptyList = do
assert $ map (traverseWithIndex (\i a -> Just $ i + a)) (NEL.fromFoldable [2, 2, 2])
== Just (NEL.fromFoldable [2, 3, 4])

log "unfoldable replicate1 should be stack-safe"
void $ pure $ NEL.length $ (replicate1 100000 1 :: NEL.NonEmptyList Int)

log "unfoldr1 should maintain order"
assert $ (nel 1 [2, 3, 4, 5]) == unfoldr1 step1 1

step :: Int -> Maybe (Tuple Int Int)
step 6 = Nothing
step n = Just (Tuple n (n + 1))

step1 :: Int -> Tuple Int (Maybe Int)
step1 n = Tuple n (if n >= 5 then Nothing else Just (n + 1))

odd :: Int -> Boolean
odd n = n `mod` 2 /= zero

Expand Down