Skip to content

Commit dba1daf

Browse files
committed
Added a few functions
* replicate * replicateA * none * singleton With some basic tests.
1 parent 6a9c621 commit dba1daf

File tree

5 files changed

+117
-3
lines changed

5 files changed

+117
-3
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"purescript-tuples": "^0.4.0"
2525
},
2626
"devDependencies": {
27-
"purescript-console": "^0.1.0"
27+
"purescript-console": "^0.1.0",
28+
"purescript-assert": "^0.1.0"
2829
}
2930
}

docs/Data/Unfoldable.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,51 @@ The generating function `f` in `unfoldr f` in understood as follows:
2727
instance unfoldableArray :: Unfoldable Array
2828
```
2929

30+
#### `replicate`
31+
32+
``` purescript
33+
replicate :: forall f a. (Unfoldable f) => Int -> a -> f a
34+
```
35+
36+
Replicate a value some natural number of times.
37+
For example:
38+
39+
~~~ purescript
40+
replicate 2 "foo" == ["foo", "foo"] :: Array String
41+
~~~
42+
43+
#### `replicateA`
44+
45+
``` purescript
46+
replicateA :: forall m f a. (Applicative m, Unfoldable f, Traversable f) => Int -> m a -> m (f a)
47+
```
48+
49+
Perform an Applicative action `n` times, and accumulate all the results.
50+
51+
#### `none`
52+
53+
``` purescript
54+
none :: forall f a. (Unfoldable f) => f a
55+
```
56+
57+
The container with no elements - unfolded with zero iterations.
58+
For example:
59+
60+
~~~ purescript
61+
none == [] :: forall a. Array a
62+
~~~
63+
64+
#### `singleton`
65+
66+
``` purescript
67+
singleton :: forall f a. (Unfoldable f) => a -> f a
68+
```
69+
70+
Contain a single value.
71+
For example:
72+
73+
~~~ purescript
74+
singleton "foo" == ["foo"] :: Array String
75+
~~~
76+
3077

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"scripts": {
44
"postinstall": "pulp dep install",
5-
"build": "pulp build && rimraf docs && pulp docs"
5+
"build": "pulp build && pulp test && rimraf docs && pulp docs"
66
},
77
"devDependencies": {
88
"pulp": "^4.0.2",

src/Data/Unfoldable.purs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Prelude
1111
import Data.Maybe
1212
import Data.Tuple
1313
import Data.Array.ST
14+
import Data.Traversable
1415
import Control.Monad.Eff
1516
import Control.Monad.ST
1617

@@ -24,7 +25,7 @@ import Control.Monad.ST
2425
-- | appended to the result of `unfoldr f b1`.
2526
class Unfoldable t where
2627
unfoldr :: forall a b. (b -> Maybe (Tuple a b)) -> b -> t a
27-
28+
2829
instance unfoldableArray :: Unfoldable Array where
2930
unfoldr f b = runPure (runSTArray (do
3031
arr <- emptySTArray
@@ -38,3 +39,41 @@ instance unfoldableArray :: Unfoldable Array where
3839
writeSTRef seed b2
3940
return false
4041
return arr))
42+
43+
-- | Replicate a value some natural number of times.
44+
-- | For example:
45+
-- |
46+
-- | ~~~ purescript
47+
-- | replicate 2 "foo" == ["foo", "foo"] :: Array String
48+
-- | ~~~
49+
replicate :: forall f a. (Unfoldable f) => Int -> a -> f a
50+
replicate n v = unfoldr step n
51+
where
52+
step :: Int -> Maybe (Tuple a Int)
53+
step i =
54+
if i <= 0 then Nothing
55+
else Just (Tuple v (i - 1))
56+
57+
-- | Perform an Applicative action `n` times, and accumulate all the results.
58+
replicateA :: forall m f a. (Applicative m, Unfoldable f, Traversable f) =>
59+
Int -> m a -> m (f a)
60+
replicateA n m = sequence (replicate n m)
61+
62+
-- | The container with no elements - unfolded with zero iterations.
63+
-- | For example:
64+
-- |
65+
-- | ~~~ purescript
66+
-- | none == [] :: forall a. Array a
67+
-- | ~~~
68+
none :: forall f a. (Unfoldable f) => f a
69+
none = unfoldr (const Nothing) unit
70+
71+
-- | Contain a single value.
72+
-- | For example:
73+
-- |
74+
-- | ~~~ purescript
75+
-- | singleton "foo" == ["foo"] :: Array String
76+
-- | ~~~
77+
singleton :: forall f a. (Unfoldable f) => a -> f a
78+
singleton = replicate 1
79+

test/Main.purs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Test.Main where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff())
6+
import Control.Monad.Eff.Console
7+
import Data.Maybe
8+
import Data.Unfoldable
9+
import Test.Assert
10+
11+
main = do
12+
log "Test none"
13+
assert $ none == [] :: Array Unit
14+
15+
log "Test singleton"
16+
assert $ singleton unit == [unit]
17+
18+
log "Test replicate"
19+
assert $ replicate 3 "foo" == ["foo", "foo", "foo"]
20+
21+
log "Test replicateA"
22+
assert $ replicateA 3 [1,2] == [
23+
[1,1,1],[1,1,2], [1,2,1],[1,2,2],
24+
[2,1,1],[2,1,2], [2,2,1],[2,2,2]
25+
]
26+
27+
log "All done!"

0 commit comments

Comments
 (0)