Skip to content

Commit ca01c22

Browse files
authored
Merge pull request #97 from matthewleon/nonempty-funcs
sorting for NonEmptyList
2 parents f44f011 + 9729249 commit ca01c22

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"purescript-lazy": "^3.0.0",
2424
"purescript-nonempty": "^4.0.0",
2525
"purescript-tailrec": "^3.0.0",
26-
"purescript-unfoldable": "^3.0.0"
26+
"purescript-unfoldable": "^3.0.0",
27+
"purescript-partial": "^1.0.0"
2728
},
2829
"devDependencies": {
2930
"purescript-arrays": "^4.0.0",

src/Data/List/NonEmpty.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Data.List.NonEmpty
1313
, length
1414
, concatMap
1515
, appendFoldable
16+
, sort
17+
, sortBy
1618
) where
1719

1820
import Prelude
@@ -21,11 +23,12 @@ import Data.Foldable (class Foldable)
2123
import Data.List ((:))
2224
import Data.List as L
2325
import Data.List.Types (NonEmptyList(..))
24-
import Data.Maybe (Maybe(..), maybe, fromMaybe)
26+
import Data.Maybe (Maybe(..), maybe, fromMaybe, fromJust)
2527
import Data.NonEmpty ((:|))
2628
import Data.NonEmpty as NE
2729
import Data.Tuple (Tuple(..))
2830
import Data.Unfoldable (class Unfoldable, unfoldr)
31+
import Partial.Unsafe (unsafePartial)
2932

3033
toUnfoldable :: forall f. Unfoldable f => NonEmptyList ~> f
3134
toUnfoldable =
@@ -68,3 +71,10 @@ concatMap = flip bind
6871
appendFoldable :: forall t a. Foldable t => NonEmptyList a -> t a -> NonEmptyList a
6972
appendFoldable (NonEmptyList (x :| xs)) ys =
7073
NonEmptyList (x :| (xs <> L.fromFoldable ys))
74+
75+
sort :: forall a. Ord a => NonEmptyList a -> NonEmptyList a
76+
sort xs = sortBy compare xs
77+
78+
sortBy :: forall a. (a -> a -> Ordering) -> NonEmptyList a -> NonEmptyList a
79+
sortBy cmp xs = unsafeFromList $ L.sortBy cmp (toList xs)
80+
where unsafeFromList ys = unsafePartial $ fromJust $ fromList ys

test/Test/Data/List/NonEmpty.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Test.Data.List.NonEmpty (testNonEmptyList) where
2+
3+
import Prelude
4+
import Data.List (fromFoldable)
5+
import Data.List.NonEmpty (NonEmptyList(..), sort, sortBy)
6+
import Data.NonEmpty ((:|))
7+
import Control.Monad.Eff (Eff)
8+
import Control.Monad.Eff.Console (CONSOLE, log)
9+
import Test.Assert (ASSERT, assert)
10+
11+
testNonEmptyList ::
12+
forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
13+
testNonEmptyList = do
14+
let nel x xs = NonEmptyList $ x :| fromFoldable xs
15+
16+
log "sort should reorder a non-empty list into ascending order based on the result of compare"
17+
assert $ sort (nel 1 [3, 2, 5, 6, 4]) == nel 1 [2, 3, 4, 5, 6]
18+
log "sortBy should reorder a non-empty list into ascending order based on the result of a comparison function"
19+
assert $ sortBy (flip compare) (nel 1 [3, 2, 5, 6, 4]) == nel 6 [5, 4, 3, 2, 1]

test/Test/Main.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import Test.Data.List (testList)
1010
import Test.Data.List.Lazy (testListLazy)
1111
import Test.Data.List.Partial (testListPartial)
1212
import Test.Data.List.ZipList (testZipList)
13+
import Test.Data.List.NonEmpty (testNonEmptyList)
1314

1415
main :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
1516
main = do
1617
testList
1718
testListLazy
1819
testZipList
1920
testListPartial
21+
testNonEmptyList

0 commit comments

Comments
 (0)