Skip to content

Commit

Permalink
Add Unfoldable1 instances for non-empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
garyb committed Aug 19, 2018
1 parent ad31f0a commit 4b9e47e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
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

0 comments on commit 4b9e47e

Please sign in to comment.