Skip to content
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
12 changes: 5 additions & 7 deletions src/Data/List/Lazy/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import Control.Alternative (class Alternative)
import Control.Comonad (class Comonad)
import Control.Extend (class Extend)
import Control.Lazy as Z
import Control.Monad.Rec.Class (tailRec)
import Control.Monad.Rec.Class as Rec
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)
Expand Down Expand Up @@ -112,13 +110,13 @@ instance foldableList :: Foldable List where
foldr op z xs = foldl (flip op) z (rev xs) where
rev = foldl (flip cons) nil

foldl op b xs = go (Tuple b xs)
foldl op = go
where
-- `go` is needed to ensure the function is tail-call optimized
go = tailRec \(Tuple b' xs') ->
case step xs' of
Nil -> Rec.Done b'
(Cons hd tl) -> Rec.Loop (Tuple (b' `op` hd) tl)
go b xs =
case step xs of
Nil -> b
Cons hd tl -> go (b `op` hd) tl

foldMap f = foldl (\b a -> b <> f a) mempty

Expand Down
27 changes: 12 additions & 15 deletions src/Data/List/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Control.Alternative (class Alternative)
import Control.Apply (lift2)
import Control.Comonad (class Comonad)
import Control.Extend (class Extend)
import Control.Monad.Rec.Class (Step(..), tailRec)
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)
Expand Down Expand Up @@ -67,26 +66,24 @@ instance functorList :: Functor List where
map f = foldr (\x acc -> f x : acc) Nil

instance foldableList :: Foldable List where
foldr f b as = foldl (flip f) b (rev (Tuple Nil as))
foldr f b = foldl (flip f) b <<< rev Nil
where
rev = tailRec \(Tuple acc xs) ->
case xs of
Nil -> Done acc
(x : xs') -> Loop (Tuple (x : acc) xs')
foldl f b as = go (Tuple b as)
rev acc = case _ of
Nil -> acc
a : as -> rev (a : acc) as
foldl f = go
where
go = tailRec \(Tuple b' xs) ->
case xs of
Nil -> Done b'
(a : as') -> Loop (Tuple (f b' a) as')
go b = case _ of
Nil -> b
a : as -> go (f b a) as
foldMap f = foldl (\acc -> append acc <<< f) mempty

instance unfoldableList :: Unfoldable List where
unfoldr f b = go (Tuple b Nil)
unfoldr f b = go b Nil
where
go = tailRec \(Tuple source memo) -> case f source of
Nothing -> Done (foldl (flip (:)) Nil memo)
Just (Tuple one rest) -> Loop (Tuple rest (one : memo))
go source memo = case f source of
Nothing -> (foldl (flip (:)) Nil memo)
Just (Tuple one rest) -> go rest (one : memo)

instance traversableList :: Traversable List where
traverse f = map (foldl (flip (:)) Nil) <<< foldl (\acc -> lift2 (flip (:)) acc <<< f) (pure Nil)
Expand Down