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
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
language: node_js
sudo: false
node_js:
- 0.10
sudo: required
dist: trusty
node_js: 5
env:
- PATH=$HOME/purescript:$PATH
install:
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $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
- npm install -g bower
- npm install
- bower install
script:
- npm run build
- npm test
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
[![Maintainer: paf31](https://img.shields.io/badge/maintainer-paf31-lightgrey.svg)](http://github.com/paf31)

A generic non-empty data structure

- [Module Documentation](docs/Data/NonEmpty.md)
9 changes: 7 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "purescript-nonempty",
"homepage": "https://github.com/purescript-contrib/purescript-nonempty",
"description": "A generic non-empty data structure",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
"output",
"test",
"bower.json",
"package.json"
],
"repository": {
"type": "git",
Expand All @@ -14,7 +19,7 @@
"purescript-console": "^0.1.0",
"purescript-foldable-traversable": "^0.4.0"
},
"dev-dependencies": {
"devDependencies": {
"purescript-assert": "^0.1.0"
}
}
108 changes: 0 additions & 108 deletions docs/Data/NonEmpty.md

This file was deleted.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"private": true,
"scripts": {
"postinstall": "pulp dep install",
"build": "pulp test && rimraf docs && pulp docs"
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^4.0.1",
"rimraf": "^2.4.1"
"pulp": "^8.1.0",
"rimraf": "^2.5.0"
}
}
38 changes: 17 additions & 21 deletions src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
module Data.NonEmpty
( NonEmpty(..)
, singleton
, nonEmpty
, (:|)
, foldl1
, foldMap1
Expand Down Expand Up @@ -34,62 +33,59 @@ import Data.Traversable (class Traversable, traverse, sequence)
-- | ```
data NonEmpty f a = NonEmpty a (f a)

nonEmpty :: forall f a. a -> f a -> NonEmpty f a
nonEmpty = NonEmpty

-- | An infix synonym for `NonEmpty`.
infixr 5 nonEmpty as :|
infixr 5 NonEmpty as :|

-- | Create a non-empty structure with a single value.
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
singleton a = NonEmpty a empty
singleton a = a :| empty

-- | Fold a non-empty structure, collecting results using a binary operation.
foldl1 :: forall f a. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
foldl1 f (NonEmpty a fa) = foldl f a fa
foldl1 f (a :| fa) = foldl f a fa

-- | Fold a non-empty structure, collecting results in a `Semigroup`.
foldMap1 :: forall f a s. (Semigroup s, Foldable f) => (a -> s) -> NonEmpty f a -> s
foldMap1 f (NonEmpty a fa) = foldl (\s a1 -> s <> f a1) (f a) fa
foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa

-- | Fold a non-empty structure.
fold1 :: forall f s. (Semigroup s, Foldable f) => NonEmpty f s -> s
fold1 = foldMap1 id

fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
fromNonEmpty f (NonEmpty a fa) = a `f` fa
fromNonEmpty f (a :| fa) = a `f` fa

oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a
oneOf (NonEmpty a fa) = pure a <|> fa
oneOf (a :| fa) = pure a <|> fa

-- | Get the 'first' element of a non-empty container.
head :: forall f a. NonEmpty f a -> a
head (NonEmpty x _) = x
head (x :| _) = x

-- | Get everything but the 'first' element of a non-empty container.
tail :: forall f a. NonEmpty f a -> f a
tail (NonEmpty _ xs) = xs
tail (_ :| xs) = xs

instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where
show (NonEmpty a fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"
show (a :| fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"

instance eqNonEmpty :: (Eq a, Eq (f a)) => Eq (NonEmpty f a) where
eq (NonEmpty a1 fa1) (NonEmpty a2 fa2) = a1 == a2 && fa1 == fa2
eq (a1 :| fa1) (a2 :| fa2) = a1 == a2 && fa1 == fa2

instance ordNonEmpty :: (Ord a, Ord (f a)) => Ord (NonEmpty f a) where
compare (NonEmpty a1 fa1) (NonEmpty a2 fa2) =
compare (a1 :| fa1) (a2 :| fa2) =
case compare a1 a2 of
EQ -> compare fa1 fa2
other -> other

instance functorNonEmpty :: (Functor f) => Functor (NonEmpty f) where
map f (NonEmpty a fa) = NonEmpty (f a) (map f fa)
map f (a :| fa) = f a :| map f fa

instance foldableNonEmpty :: (Foldable f) => Foldable (NonEmpty f) where
foldMap f (NonEmpty a fa) = f a <> foldMap f fa
foldl f b (NonEmpty a fa) = foldl f (f b a) fa
foldr f b (NonEmpty a fa) = f a (foldr f b fa)
foldMap f (a :| fa) = f a <> foldMap f fa
foldl f b (a :| fa) = foldl f (f b a) fa
foldr f b (a :| fa) = f a (foldr f b fa)

instance traversableNonEmpty :: (Traversable f) => Traversable (NonEmpty f) where
sequence (NonEmpty a fa) = NonEmpty <$> a <*> sequence fa
traverse f (NonEmpty a fa) = NonEmpty <$> f a <*> traverse f fa
sequence (a :| fa) = NonEmpty <$> a <*> sequence fa
traverse f (a :| fa) = NonEmpty <$> f a <*> traverse f fa