diff --git a/.travis.yml b/.travis.yml index 8daa38c1..116705f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,8 @@ node_js: stable env: - PATH=$HOME/purescript:$PATH install: - - TAG=$(basename $(curl --location --silent --output /dev/null -w %{url_effective} https://github.com/purescript/purescript/releases/latest)) + # - TAG=$(basename $(curl --location --silent --output /dev/null -w %{url_effective} https://github.com/purescript/purescript/releases/latest)) + - TAG=v0.14.0-rc3 - curl --location --output $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz - tar -xvf $HOME/purescript.tar.gz -C $HOME/ - chmod a+x $HOME/purescript diff --git a/bower.json b/bower.json index 4e217646..3b108a88 100644 --- a/bower.json +++ b/bower.json @@ -15,22 +15,22 @@ "package.json" ], "dependencies": { - "purescript-bifunctors": "^4.0.0", - "purescript-control": "^4.0.0", - "purescript-foldable-traversable": "^4.0.0", - "purescript-maybe": "^4.0.0", - "purescript-nonempty": "^5.0.0", - "purescript-partial": "^2.0.0", - "purescript-prelude": "^4.0.0", - "purescript-st": "^4.0.0", - "purescript-tailrec": "^4.0.0", - "purescript-tuples": "^5.0.0", - "purescript-unfoldable": "^4.0.0", - "purescript-unsafe-coerce": "^4.0.0" + "purescript-bifunctors": "master", + "purescript-control": "master", + "purescript-foldable-traversable": "master", + "purescript-maybe": "master", + "purescript-nonempty": "master", + "purescript-partial": "master", + "purescript-prelude": "master", + "purescript-st": "master", + "purescript-tailrec": "master", + "purescript-tuples": "master", + "purescript-unfoldable": "master", + "purescript-unsafe-coerce": "master" }, "devDependencies": { - "purescript-assert": "^4.0.0", - "purescript-console": "^4.0.0", - "purescript-const": "^4.0.0" + "purescript-assert": "master", + "purescript-console": "master", + "purescript-const": "master" } } diff --git a/package.json b/package.json index 42af30f1..8985bcfd 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "devDependencies": { "eslint": "^4.19.1", "pulp": "^15.0.0", - "purescript-psa": "^0.6.0", + "purescript-psa": "^0.8.0", "rimraf": "^2.6.2" } } diff --git a/src/Data/Array/NonEmpty/Internal.js b/src/Data/Array/NonEmpty/Internal.js index 0971fc45..40d88a75 100644 --- a/src/Data/Array/NonEmpty/Internal.js +++ b/src/Data/Array/NonEmpty/Internal.js @@ -1,6 +1,16 @@ "use strict"; -exports.fold1Impl = function (f) { +exports.foldr1Impl = function (f) { + return function (xs) { + var acc = xs[xs.length - 1]; + for (var i = xs.length - 2; i >= 0; i--) { + acc = f(xs[i])(acc); + } + return acc; + }; +}; + +exports.foldl1Impl = function (f) { return function (xs) { var acc = xs[0]; var len = xs.length; diff --git a/src/Data/Array/NonEmpty/Internal.purs b/src/Data/Array/NonEmpty/Internal.purs index 1603bd05..3d87a69e 100644 --- a/src/Data/Array/NonEmpty/Internal.purs +++ b/src/Data/Array/NonEmpty/Internal.purs @@ -35,7 +35,9 @@ derive newtype instance foldableWithIndexNonEmptyArray :: FoldableWithIndex Int instance foldable1NonEmptyArray :: Foldable1 NonEmptyArray where foldMap1 = foldMap1Default - fold1 = fold1Impl (<>) + fold1 = foldl1Impl (<>) + foldr1 = foldr1Impl + foldl1 = foldl1Impl derive newtype instance unfoldable1NonEmptyArray :: Unfoldable1 NonEmptyArray derive newtype instance traversableNonEmptyArray :: Traversable NonEmptyArray @@ -56,7 +58,8 @@ derive newtype instance monadNonEmptyArray :: Monad NonEmptyArray derive newtype instance altNonEmptyArray :: Alt NonEmptyArray -- we use FFI here to avoid the unncessary copy created by `tail` -foreign import fold1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a +foreign import foldr1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a +foreign import foldl1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a foreign import traverse1Impl :: forall m a b diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index 4a57939d..32f7c274 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -31,7 +31,7 @@ module Data.Array.ST import Prelude import Control.Monad.ST as ST -import Control.Monad.ST (ST, kind Region) +import Control.Monad.ST (ST, Region) import Data.Maybe (Maybe(..)) -- | A reference to a mutable array. @@ -43,6 +43,8 @@ import Data.Maybe (Maybe(..)) -- | except that mutation is allowed. foreign import data STArray :: Region -> Type -> Type +type role STArray nominal representational + -- | An element and its index. type Assoc a = { value :: a, index :: Int } diff --git a/test/Test/Data/Array/NonEmpty.purs b/test/Test/Data/Array/NonEmpty.purs index ba791e2b..b42c08e9 100644 --- a/test/Test/Data/Array/NonEmpty.purs +++ b/test/Test/Data/Array/NonEmpty.purs @@ -10,7 +10,7 @@ import Data.FunctorWithIndex (mapWithIndex) import Data.Maybe (Maybe(..), fromJust) import Data.Monoid.Additive (Additive(..)) import Data.NonEmpty ((:|)) -import Data.Semigroup.Foldable (foldMap1) +import Data.Semigroup.Foldable (foldMap1, foldr1, foldl1) import Data.Semigroup.Traversable (traverse1) import Data.Tuple (Tuple(..)) import Data.Unfoldable1 as U1 @@ -305,12 +305,22 @@ testNonEmptyArray = do log "Unfoldable instance" assert $ U1.range 0 9 == NEA.range 0 9 - log "foldl should work" + log "foldMap1 should work" + assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10 + + log "fold1 should work" -- test through sum assert $ sum (fromArray [1, 2, 3, 4]) == 10 - log "foldMap1 should work" - assert $ foldMap1 Additive (fromArray [1, 2, 3, 4]) == Additive 10 + log "foldr1 should work" + assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(a(b(cd)))" + assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)" + assert $ foldr1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a" + + log "foldl1 should work" + assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b", "c", "d"]) == "(((ab)c)d)" + assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a", "b"]) == "(ab)" + assert $ foldl1 (\l r -> "(" <> l <> r <> ")") (fromArray ["a"]) == "a" log "traverse1 should work" assert $ traverse1 Just (fromArray [1, 2, 3, 4]) == NEA.fromArray [1, 2, 3, 4]