Skip to content

Updates for Arrays #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2015
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
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"package.json"
],
"dependencies": {
"purescript-integers": "~0.2.0"
"purescript-prelude": "~0.1.0"
}
}
10 changes: 9 additions & 1 deletion docs/Control.Alt.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This module defines the `Alt` type class.

``` purescript
class (Functor f) <= Alt f where
(<|>) :: forall a. f a -> f a -> f a
alt :: forall a. f a -> f a -> f a
```

The `Alt` type class identifies an associative operation on a type
Expand All @@ -25,5 +25,13 @@ types of kind `* -> *`, like `Array` or `List`, rather than concrete types
For example, the `Array` (`[]`) type is an instance of `Alt`, where
`(<|>)` is defined to be concatenation.

#### `(<|>)`

``` purescript
(<|>) :: forall f a. (Alt f) => f a -> f a -> f a
```

An infix version of `alt`.



23 changes: 0 additions & 23 deletions docs/Control.Alternative.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,5 @@ laws:
- Distributivity: `(f <|> g) <*> x == (f <*> x) <|> (g <*> x)`
- Annihilation: `empty <*> f = empty`

#### `some`

``` purescript
some :: forall f a. (Alternative f, Lazy (f [a])) => f a -> f [a]
```

Attempt a computation multiple times, requiring at least one success.

The `Lazy` constraint is used to generate the result lazily, to ensure
termination.

#### `many`

``` purescript
many :: forall f a. (Alternative f, Lazy (f [a])) => f a -> f [a]
```

Attempt a computation multiple times, returning as many successful results
as possible (possibly zero).

The `Lazy` constraint is used to generate the result lazily, to ensure
termination.



18 changes: 9 additions & 9 deletions docs/Control.Extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This module defines the `Extend` type class and associated helper functions.

``` purescript
class (Functor w) <= Extend w where
(<<=) :: forall b a. (w a -> b) -> w a -> w b
extend :: forall b a. (w a -> b) -> w a -> w b
```

The `Extend` class defines the extension operator `(<<=)`
Expand All @@ -30,6 +30,14 @@ instance extendArr :: (Semigroup w) => Extend (Prim.Function w)
```


#### `(<<=)`

``` purescript
(<<=) :: forall w a b. (Extend w) => (w a -> b) -> w a -> w b
```

An infix version of `extend`

#### `(=>>)`

``` purescript
Expand All @@ -54,14 +62,6 @@ Forwards co-Kleisli composition.

Backwards co-Kleisli composition.

#### `extend`

``` purescript
extend :: forall b a w. (Extend w) => (w a -> b) -> w a -> w b
```

An alias for `(<<=)`.

#### `duplicate`

``` purescript
Expand Down
31 changes: 0 additions & 31 deletions docs/Control.Monad.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@

This module defines helper functions for working with `Monad` instances.

#### `replicateM`

``` purescript
replicateM :: forall m a. (Monad m) => Int -> m a -> m [a]
```

Perform a monadic action `n` times collecting all of the results.

#### `foldM`

``` purescript
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
```

Perform a fold using a monadic step function.

#### `when`

``` purescript
Expand All @@ -37,20 +21,5 @@ unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit

Perform a monadic action unless a condition is true.

#### `filterM`

``` purescript
filterM :: forall a m. (Monad m) => (a -> m Boolean) -> [a] -> m [a]
```

Filter where the predicate returns a monadic `Boolean`.

For example:

```purescript
powerSet :: forall a. [a] -> [[a]]
powerSet = filterM (const [true, false])
```



10 changes: 7 additions & 3 deletions src/Control/Alt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module Control.Alt where

infixl 3 <|>

-- | The `Alt` type class identifies an associative operation on a type
-- | constructor. It is similar to `Semigroup`, except that it applies to
-- | types of kind `* -> *`, like `Array` or `List`, rather than concrete types
Expand All @@ -17,4 +15,10 @@ infixl 3 <|>
-- | For example, the `Array` (`[]`) type is an instance of `Alt`, where
-- | `(<|>)` is defined to be concatenation.
class (Functor f) <= Alt f where
(<|>) :: forall a. f a -> f a -> f a
alt :: forall a. f a -> f a -> f a

infixl 3 <|>

-- | An infix version of `alt`.
(<|>) :: forall f a. (Alt f) => f a -> f a -> f a
(<|>) = alt
15 changes: 0 additions & 15 deletions src/Control/Alternative.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,3 @@ import Control.Plus
-- | - Annihilation: `empty <*> f = empty`
class (Applicative f, Plus f) <= Alternative f

-- | Attempt a computation multiple times, requiring at least one success.
-- |
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
-- | termination.
some :: forall f a. (Alternative f, Lazy (f [a])) => f a -> f [a]
some v = (:) <$> v <*> defer (\_ -> many v)

-- | Attempt a computation multiple times, returning as many successful results
-- | as possible (possibly zero).
-- |
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
-- | termination.
many :: forall f a. (Alternative f, Lazy (f [a])) => f a -> f [a]
many v = some v <|> pure []

14 changes: 7 additions & 7 deletions src/Control/Extend.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ infixr 1 =<=
-- |
-- | - Associativity: `extend f <<< extend g = extend (f <<< extend g)`
class (Functor w) <= Extend w where
(<<=) :: forall b a. (w a -> b) -> w a -> w b
extend :: forall b a. (w a -> b) -> w a -> w b

instance extendArr :: (Semigroup w) => Extend ((->) w) where
(<<=) f g w = f \w' -> g (w <> w')
extend f g w = f \w' -> g (w <> w')

-- | An infix version of `extend`
(<<=) :: forall w a b. (Extend w) => (w a -> b) -> w a -> w b
(<<=) = extend

-- | A version of `(<<=)` with its arguments flipped.
(=>>) :: forall b a w. (Extend w) => w a -> (w a -> b) -> w b
Expand All @@ -35,12 +39,8 @@ instance extendArr :: (Semigroup w) => Extend ((->) w) where
(=<=) :: forall b a w c. (Extend w) => (w b -> c) -> (w a -> b) -> w a -> c
(=<=) f g w = f (g <<= w)

-- | An alias for `(<<=)`.
extend :: forall b a w. (Extend w) => (w a -> b) -> w a -> w b
extend = (<<=)

-- | Duplicate a comonadic context.
-- |
-- | `duplicate` is dual to `Control.Bind.join`.
duplicate :: forall a w. (Extend w) => w a -> w (w a)
duplicate w = id <<= w
duplicate = extend id
31 changes: 0 additions & 31 deletions src/Control/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

module Control.Monad where

import Data.Int (Int())

-- | Perform a monadic action `n` times collecting all of the results.
replicateM :: forall m a. (Monad m) => Int -> m a -> m [a]
replicateM n m | n < one = return []
| otherwise = do a <- m
as <- replicateM (n - one) m
return (a : as)

-- | Perform a fold using a monadic step function.
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
foldM _ a [] = return a
foldM f a (b:bs) = f a b >>= \a' -> foldM f a' bs

-- | Perform a monadic action when a condition is true.
when :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
when true m = m
Expand All @@ -25,20 +11,3 @@ when false _ = return unit
unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
unless false m = m
unless true _ = return unit

-- | Filter where the predicate returns a monadic `Boolean`.
-- |
-- | For example:
-- |
-- | ```purescript
-- | powerSet :: forall a. [a] -> [[a]]
-- | powerSet = filterM (const [true, false])
-- | ```
filterM :: forall a m. (Monad m) => (a -> m Boolean) -> [a] -> m [a]
filterM _ [] = return []
filterM p (x:xs) = do
b <- p x
xs' <- filterM p xs
return $ if b
then x : xs'
else xs'