Skip to content

Commit 34e83ce

Browse files
authored
Merge branch 'compiler/0.12' into NonEmpty-Derive-Foldable1
2 parents 53425f1 + a49621c commit 34e83ce

File tree

14 files changed

+101
-59
lines changed

14 files changed

+101
-59
lines changed

bower.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23-
"purescript-lazy": "^3.0.0",
24-
"purescript-nonempty": "^4.2.0",
25-
"purescript-tailrec": "^3.3.0",
26-
"purescript-unfoldable": "^3.0.0",
27-
"purescript-partial": "^1.0.0",
28-
"purescript-foldable-traversable": "^3.4.0"
23+
"purescript-lazy": "#compiler/0.12",
24+
"purescript-nonempty": "#compiler/0.12",
25+
"purescript-tailrec": "#compiler/0.12",
26+
"purescript-unfoldable": "#compiler/0.12",
27+
"purescript-partial": "#compiler/0.12",
28+
"purescript-foldable-traversable": "#compiler/0.12"
2929
},
3030
"devDependencies": {
31-
"purescript-arrays": "^4.0.0",
32-
"purescript-assert": "^3.0.0",
33-
"purescript-console": "^3.0.0",
34-
"purescript-math": "^2.0.0"
31+
"purescript-arrays": "#compiler/0.12",
32+
"purescript-assert": "#compiler/0.12",
33+
"purescript-console": "#compiler/0.12",
34+
"purescript-math": "#compiler/0.12"
3535
}
3636
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
55
"build": "pulp build -- --censor-lib --strict",
6-
"test": "pulp test"
6+
"test": "pulp test --check-main-type Effect.Effect"
77
},
88
"devDependencies": {
9-
"pulp": "^11.0.0",
10-
"purescript-psa": "^0.5.1",
11-
"rimraf": "^2.6.1"
9+
"pulp": "^12.0.1",
10+
"purescript-psa": "^0.6.0",
11+
"rimraf": "^2.6.2"
1212
}
1313
}

src/Data/List.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ reverse = go Nil
372372
-- |
373373
-- | Running time: `O(n)`, where `n` is the total number of elements.
374374
concat :: forall a. List (List a) -> List a
375-
concat = (_ >>= id)
375+
concat = (_ >>= identity)
376376

377377
-- | Apply a function to each element in a list, and flatten the results
378378
-- | into a single, new list.
@@ -423,7 +423,7 @@ mapMaybe f = go Nil
423423
-- | Filter a list of optional values, keeping only the elements which contain
424424
-- | a value.
425425
catMaybes :: forall a. List (Maybe a) -> List a
426-
catMaybes = mapMaybe id
426+
catMaybes = mapMaybe identity
427427

428428

429429
-- | Apply a function to each element and its index in a list starting at 0.

src/Data/List/Lazy.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ reverse xs = Z.defer \_ -> foldl (flip cons) nil xs
409409
-- |
410410
-- | Running time: `O(n)`, where `n` is the total number of elements.
411411
concat :: forall a. List (List a) -> List a
412-
concat = (_ >>= id)
412+
concat = (_ >>= identity)
413413

414414
-- | Apply a function to each element in a list, and flatten the results
415415
-- | into a single, new list.
@@ -463,7 +463,7 @@ mapMaybe f = List <<< map go <<< unwrap
463463
-- | Filter a list of optional values, keeping only the elements which contain
464464
-- | a value.
465465
catMaybes :: forall a. List (Maybe a) -> List a
466-
catMaybes = mapMaybe id
466+
catMaybes = mapMaybe identity
467467

468468
--------------------------------------------------------------------------------
469469
-- Sorting ---------------------------------------------------------------------
@@ -509,10 +509,11 @@ slice start end xs = take (end - start) (drop start xs)
509509
-- |
510510
-- | Running time: `O(n)` where `n` is the number of elements to take.
511511
take :: forall a. Int -> List a -> List a
512-
take n = List <<< map (go n) <<< unwrap
512+
take n = if n <= 0
513+
then const nil
514+
else List <<< map (go n) <<< unwrap
513515
where
514516
go :: Int -> Step a -> Step a
515-
go i _ | i <= 0 = Nil
516517
go _ Nil = Nil
517518
go n' (Cons x xs) = Cons x (take (n' - 1) xs)
518519

src/Data/List/Lazy/Types.purs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWit
1616
import Data.FunctorWithIndex (class FunctorWithIndex)
1717
import Data.Lazy (Lazy, defer, force)
1818
import Data.Maybe (Maybe(..))
19-
import Data.Monoid (class Monoid, mempty)
2019
import Data.Newtype (class Newtype, unwrap)
2120
import Data.NonEmpty (NonEmpty, (:|))
2221
import Data.NonEmpty as NE
@@ -25,6 +24,7 @@ import Data.Traversable (class Traversable, traverse, sequence)
2524
import Data.TraversableWithIndex (class TraversableWithIndex)
2625
import Data.Tuple (Tuple(..), snd)
2726
import Data.Unfoldable (class Unfoldable)
27+
import Data.Unfoldable1 (class Unfoldable1)
2828

2929
-- | A lazy linked list.
3030
newtype List a = List (Lazy (Step a))
@@ -141,6 +141,12 @@ instance foldableWithIndexList :: FoldableWithIndex Int List where
141141
snd <<< foldl (\(Tuple i b) a -> Tuple (i + 1) (f i b a)) (Tuple 0 acc)
142142
foldMapWithIndex f = foldlWithIndex (\i acc -> append acc <<< f i) mempty
143143

144+
instance unfoldable1List :: Unfoldable1 List where
145+
unfoldr1 = go where
146+
go f b = Z.defer \_ -> case f b of
147+
Tuple a (Just b') -> a : go f b'
148+
Tuple a Nothing -> a : nil
149+
144150
instance unfoldableList :: Unfoldable List where
145151
unfoldr = go where
146152
go f b = Z.defer \_ -> case f b of
@@ -151,7 +157,7 @@ instance traversableList :: Traversable List where
151157
traverse f =
152158
foldr (\a b -> cons <$> f a <*> b) (pure nil)
153159

154-
sequence = traverse id
160+
sequence = traverse identity
155161

156162
instance traversableWithIndexList :: TraversableWithIndex Int List where
157163
traverseWithIndex f =

src/Data/List/NonEmpty.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ catMaybes :: forall a. NonEmptyList (Maybe a) -> L.List a
210210
catMaybes = lift L.catMaybes
211211

212212
concat :: forall a. NonEmptyList (NonEmptyList a) -> NonEmptyList a
213-
concat = (_ >>= id)
213+
concat = (_ >>= identity)
214214

215215
concatMap :: forall a b. (a -> NonEmptyList b) -> NonEmptyList a -> NonEmptyList b
216216
concatMap = flip bind

src/Data/List/Types.purs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Data.Foldable (class Foldable, foldl, foldr, intercalate)
1515
import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWithIndex)
1616
import Data.FunctorWithIndex (class FunctorWithIndex)
1717
import Data.Maybe (Maybe(..))
18-
import Data.Monoid (class Monoid, mempty)
1918
import Data.Newtype (class Newtype)
2019
import Data.NonEmpty (NonEmpty, (:|))
2120
import Data.NonEmpty as NE
@@ -26,6 +25,7 @@ import Data.Traversable (class Traversable, traverse)
2625
import Data.TraversableWithIndex (class TraversableWithIndex)
2726
import Data.Tuple (Tuple(..), snd)
2827
import Data.Unfoldable (class Unfoldable)
28+
import Data.Unfoldable1 (class Unfoldable1)
2929

3030
data List a = Nil | Cons a (List a)
3131

@@ -99,6 +99,13 @@ instance foldableWithIndexList :: FoldableWithIndex Int List where
9999
snd <<< foldl (\(Tuple i b) a -> Tuple (i + 1) (f i b a)) (Tuple 0 acc)
100100
foldMapWithIndex f = foldlWithIndex (\i acc -> append acc <<< f i) mempty
101101

102+
instance unfoldable1List :: Unfoldable1 List where
103+
unfoldr1 f b = go b Nil
104+
where
105+
go source memo = case f source of
106+
Tuple one (Just rest) -> go rest (one : memo)
107+
Tuple one Nothing -> foldl (flip (:)) Nil (one : memo)
108+
102109
instance unfoldableList :: Unfoldable List where
103110
unfoldr f b = go b Nil
104111
where
@@ -108,7 +115,7 @@ instance unfoldableList :: Unfoldable List where
108115

109116
instance traversableList :: Traversable List where
110117
traverse f = map (foldl (flip (:)) Nil) <<< foldl (\acc -> lift2 (flip (:)) acc <<< f) (pure Nil)
111-
sequence = traverse id
118+
sequence = traverse identity
112119

113120
instance traversableWithIndexList :: TraversableWithIndex Int List where
114121
traverseWithIndex f =
@@ -210,4 +217,4 @@ instance traversable1NonEmptyList :: Traversable1 NonEmptyList where
210217
traverse1 f (NonEmptyList (a :| as)) =
211218
foldl (\acc -> lift2 (flip nelCons) acc <<< f) (pure <$> f a) as
212219
<#> case _ of NonEmptyList (x :| xs) → foldl (flip nelCons) (pure x) xs
213-
sequence1 = traverse1 id
220+
sequence1 = traverse1 identity

src/Data/List/ZipList.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ module Data.List.ZipList
66
) where
77

88
import Prelude
9+
import Prim.TypeError (class Fail, Text)
910
import Control.Alt (class Alt)
1011
import Control.Alternative (class Alternative)
1112
import Control.Plus (class Plus)
1213
import Data.Foldable (class Foldable)
1314
import Data.List.Lazy (List, repeat, zipWith)
14-
import Data.Monoid (class Monoid, mempty)
1515
import Data.Newtype (class Newtype)
1616
import Data.Traversable (class Traversable)
1717
import Partial.Unsafe (unsafeCrashWith)
@@ -54,12 +54,12 @@ instance plusZipList :: Plus ZipList where
5454
instance alternativeZipList :: Alternative ZipList
5555

5656
instance zipListIsNotBind
57-
:: Fail """
57+
:: Fail (Text """
5858
ZipList is not Bind. Any implementation would break the associativity law.
5959
6060
Possible alternatives:
6161
Data.List.List
6262
Data.List.Lazy.List
63-
"""
63+
""")
6464
=> Bind ZipList where
6565
bind = unsafeCrashWith "bind: unreachable"

test/Test/Data/List.purs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module Test.Data.List (testList) where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, log)
75
import Data.Foldable (foldMap, foldl)
86
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
97
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, dropEnd, takeWhile, take, takeEnd, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
@@ -15,10 +13,13 @@ import Data.Traversable (traverse)
1513
import Data.TraversableWithIndex (traverseWithIndex)
1614
import Data.Tuple (Tuple(..))
1715
import Data.Unfoldable (replicate, replicateA, unfoldr)
16+
import Data.Unfoldable1 (unfoldr1)
17+
import Effect (Effect)
18+
import Effect.Console (log)
1819
import Partial.Unsafe (unsafePartial)
19-
import Test.Assert (ASSERT, assert)
20+
import Test.Assert (assert)
2021

21-
testList :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
22+
testList :: Effect Unit
2223
testList = do
2324
let l = fromFoldable
2425

@@ -357,8 +358,11 @@ testList = do
357358
log "unfoldr should maintain order"
358359
assert $ (1..5) == unfoldr step 1
359360

361+
log "unfoldr1 should maintain order"
362+
assert $ (1..5) == unfoldr1 step1 1
363+
360364
log "map should maintain order"
361-
assert $ (1..5) == map id (1..5)
365+
assert $ (1..5) == map identity (1..5)
362366

363367
log "transpose"
364368
assert $ transpose (l [l [1,2,3], l[4,5,6], l [7,8,9]]) ==
@@ -388,11 +392,13 @@ testList = do
388392
log "append should be stack-safe"
389393
void $ pure $ xs <> xs
390394

391-
392395
step :: Int -> Maybe (Tuple Int Int)
393396
step 6 = Nothing
394397
step n = Just (Tuple n (n + 1))
395398

399+
step1 :: Int -> Tuple Int (Maybe Int)
400+
step1 n = Tuple n (if n >= 5 then Nothing else Just (n + 1))
401+
396402
nil :: List Int
397403
nil = Nil
398404

test/Test/Data/List/Lazy.purs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ module Test.Data.List.Lazy (testListLazy) where
33
import Prelude
44

55
import Control.Lazy (defer)
6-
import Control.Monad.Eff (Eff)
7-
import Control.Monad.Eff.Console (CONSOLE, log)
86
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
97
import Data.FunctorWithIndex (mapWithIndex)
108
import Data.Lazy as Z
11-
import Data.List.Lazy (List, nil, stripPrefix, Pattern(..), cons, foldl, foldr, foldMap, singleton, transpose, take, iterate, filter, uncons, foldM, foldrLazy, range, unzip, zip, length, zipWithA, replicate, repeat, zipWith, intersectBy, intersect, deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group, partition, span, dropWhile, drop, takeWhile, slice, catMaybes, mapMaybe, filterM, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, init, tail, last, head, insertBy, insert, snoc, null, replicateM, fromFoldable, (:), (\\), (!!))
9+
import Data.List.Lazy (List, Pattern(..), alterAt, catMaybes, concat, concatMap, cons, delete, deleteAt, deleteBy, drop, dropWhile, elemIndex, elemLastIndex, filter, filterM, findIndex, findLastIndex, foldM, foldMap, foldl, foldr, foldrLazy, fromFoldable, group, groupBy, head, init, insert, insertAt, insertBy, intersect, intersectBy, iterate, last, length, mapMaybe, modifyAt, nil, nub, nubBy, null, partition, range, repeat, replicate, replicateM, reverse, singleton, slice, snoc, span, stripPrefix, tail, take, takeWhile, transpose, uncons, union, unionBy, unzip, updateAt, zip, zipWith, zipWithA, (!!), (..), (:), (\\))
1210
import Data.List.Lazy.NonEmpty as NEL
1311
import Data.Maybe (Maybe(..), isNothing, fromJust)
1412
import Data.Monoid.Additive (Additive(..))
1513
import Data.NonEmpty ((:|))
1614
import Data.Traversable (traverse)
1715
import Data.TraversableWithIndex (traverseWithIndex)
1816
import Data.Tuple (Tuple(..))
17+
import Data.Unfoldable (unfoldr)
18+
import Data.Unfoldable1 (unfoldr1)
19+
import Effect (Effect)
20+
import Effect.Console (log)
1921
import Partial.Unsafe (unsafePartial)
20-
import Test.Assert (ASSERT, assert)
22+
import Test.Assert (assert)
2123

22-
testListLazy :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
24+
testListLazy :: Effect Unit
2325
testListLazy = do
2426
let
2527
l = fromFoldable
@@ -270,6 +272,12 @@ testListLazy = do
270272
assert $ (take 2 (l [1, 2, 3])) == l [1, 2]
271273
assert $ (take 1 nil') == nil'
272274

275+
log "take should evaluate exactly n items which we needed"
276+
assert let oops x = 0 : oops x
277+
xs = 1 : defer oops
278+
in take 1 xs == l [1]
279+
-- If `take` evaluate more than once, it would crash with a stack overflow
280+
273281
log "takeWhile should keep all values that match a predicate from the front of an list"
274282
assert $ (takeWhile (_ /= 2) (l [1, 2, 3])) == l [1]
275283
assert $ (takeWhile (_ /= 3) (l [1, 2, 3])) == l [1, 2]
@@ -396,9 +404,26 @@ testListLazy = do
396404
((10:20:30:nil) : (11:31:nil) : (32:nil) : nil)
397405
log "transpose nil == nil"
398406
assert $ transpose nil == (nil :: List (List Int))
407+
399408
log "transpose (singleton nil) == nil"
400409
assert $ transpose (singleton nil) == (nil :: List (List Int))
401410

411+
log "unfoldr should maintain order"
412+
assert $ (1..5) == unfoldr step 1
413+
414+
log "unfoldr1 should maintain order"
415+
assert $ (1..5) == unfoldr1 step1 1
416+
417+
log "map should maintain order"
418+
assert $ (1..5) == map identity (1..5)
419+
420+
step :: Int -> Maybe (Tuple Int Int)
421+
step 6 = Nothing
422+
step n = Just (Tuple n (n + 1))
423+
424+
step1 :: Int -> Tuple Int (Maybe Int)
425+
step1 n = Tuple n (if n >= 5 then Nothing else Just (n + 1))
426+
402427
nil' :: List Int
403428
nil' = nil
404429

test/Test/Data/List/NonEmpty.purs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ module Test.Data.List.NonEmpty (testNonEmptyList) where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, log)
5+
import Effect (Effect)
6+
import Effect.Console (log)
77
import Data.Foldable (class Foldable, foldM, foldMap, foldl, length)
88
import Data.List as L
99
import Data.List.NonEmpty as NEL
1010
import Data.Maybe (Maybe(..))
1111
import Data.Monoid.Additive (Additive(..))
1212
import Data.NonEmpty ((:|))
1313
import Data.Tuple (Tuple(..))
14-
import Test.Assert (ASSERT, assert)
14+
import Test.Assert (assert)
1515

16-
testNonEmptyList ::
17-
forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
16+
testNonEmptyList :: Effect Unit
1817
testNonEmptyList = do
1918
let
2019
nel :: f a. Foldable f => a -> f a -> NEL.NonEmptyList a
@@ -223,7 +222,7 @@ testNonEmptyList = do
223222
assert $ foldMap show (nel 1 (L.range 2 5)) == "12345"
224223

225224
log "map should maintain order"
226-
assert $ nel 0 (L.range 1 5) == map id (nel 0 (L.range 1 5))
225+
assert $ nel 0 (L.range 1 5) == map identity (nel 0 (L.range 1 5))
227226

228227
log "traverse1 should be stack-safe"
229228
let xs = nel 0 (L.range 1 100000)

test/Test/Data/List/Partial.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ module Test.Data.List.Partial (testListPartial) where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, log)
5+
import Effect (Effect)
6+
import Effect.Console (log)
77

88
import Data.List (List(..), fromFoldable)
99
import Data.List.Partial (init, tail, last, head)
1010

1111
import Partial.Unsafe (unsafePartial)
1212

13-
import Test.Assert (ASSERT, assert, assertThrows)
13+
import Test.Assert (assert, assertThrows)
1414

15-
testListPartial :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
15+
testListPartial :: Effect Unit
1616
testListPartial = do
1717
let l = fromFoldable
1818

0 commit comments

Comments
 (0)