diff --git a/bower.json b/bower.json index 5fad604..b4f2662 100644 --- a/bower.json +++ b/bower.json @@ -23,7 +23,8 @@ "purescript-lazy": "^3.0.0", "purescript-nonempty": "^4.0.0", "purescript-tailrec": "^3.0.0", - "purescript-unfoldable": "^3.0.0" + "purescript-unfoldable": "^3.0.0", + "purescript-partial": "^1.0.0" }, "devDependencies": { "purescript-arrays": "^4.0.0", diff --git a/src/Data/List/NonEmpty.purs b/src/Data/List/NonEmpty.purs index 5ad997b..d8e6f6b 100644 --- a/src/Data/List/NonEmpty.purs +++ b/src/Data/List/NonEmpty.purs @@ -13,6 +13,8 @@ module Data.List.NonEmpty , length , concatMap , appendFoldable + , sort + , sortBy ) where import Prelude @@ -21,11 +23,12 @@ import Data.Foldable (class Foldable) import Data.List ((:)) import Data.List as L import Data.List.Types (NonEmptyList(..)) -import Data.Maybe (Maybe(..), maybe, fromMaybe) +import Data.Maybe (Maybe(..), maybe, fromMaybe, fromJust) import Data.NonEmpty ((:|)) import Data.NonEmpty as NE import Data.Tuple (Tuple(..)) import Data.Unfoldable (class Unfoldable, unfoldr) +import Partial.Unsafe (unsafePartial) toUnfoldable :: forall f. Unfoldable f => NonEmptyList ~> f toUnfoldable = @@ -68,3 +71,10 @@ concatMap = flip bind appendFoldable :: forall t a. Foldable t => NonEmptyList a -> t a -> NonEmptyList a appendFoldable (NonEmptyList (x :| xs)) ys = NonEmptyList (x :| (xs <> L.fromFoldable ys)) + +sort :: forall a. Ord a => NonEmptyList a -> NonEmptyList a +sort xs = sortBy compare xs + +sortBy :: forall a. (a -> a -> Ordering) -> NonEmptyList a -> NonEmptyList a +sortBy cmp xs = unsafeFromList $ L.sortBy cmp (toList xs) + where unsafeFromList ys = unsafePartial $ fromJust $ fromList ys diff --git a/test/Test/Data/List/NonEmpty.purs b/test/Test/Data/List/NonEmpty.purs new file mode 100644 index 0000000..3b244d2 --- /dev/null +++ b/test/Test/Data/List/NonEmpty.purs @@ -0,0 +1,19 @@ +module Test.Data.List.NonEmpty (testNonEmptyList) where + +import Prelude +import Data.List (fromFoldable) +import Data.List.NonEmpty (NonEmptyList(..), sort, sortBy) +import Data.NonEmpty ((:|)) +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Console (CONSOLE, log) +import Test.Assert (ASSERT, assert) + +testNonEmptyList :: + forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit +testNonEmptyList = do + let nel x xs = NonEmptyList $ x :| fromFoldable xs + + log "sort should reorder a non-empty list into ascending order based on the result of compare" + assert $ sort (nel 1 [3, 2, 5, 6, 4]) == nel 1 [2, 3, 4, 5, 6] + log "sortBy should reorder a non-empty list into ascending order based on the result of a comparison function" + assert $ sortBy (flip compare) (nel 1 [3, 2, 5, 6, 4]) == nel 6 [5, 4, 3, 2, 1] diff --git a/test/Test/Main.purs b/test/Test/Main.purs index 17c930c..ea999be 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -10,6 +10,7 @@ import Test.Data.List (testList) import Test.Data.List.Lazy (testListLazy) import Test.Data.List.Partial (testListPartial) import Test.Data.List.ZipList (testZipList) +import Test.Data.List.NonEmpty (testNonEmptyList) main :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit main = do @@ -17,3 +18,4 @@ main = do testListLazy testZipList testListPartial + testNonEmptyList