Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit eaf1341

Browse files
committed
Added a whole bunch of stuff.
1 parent 6216371 commit eaf1341

11 files changed

Lines changed: 434 additions & 27 deletions

File tree

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"purescript-bifunctors": "~0.0.4"
3737
},
3838
"devDependencies": {
39-
"purescript-easy-ffi": "~1.0.1"
39+
"purescript-easy-ffi": "~1.0.1",
40+
"purescript-quickcheck": "~0.1.3"
4041
}
4142
}

gulpfile.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22

33
var gulp = require('gulp')
44
, purescript = require('gulp-purescript')
5+
, run = require('gulp-run')
56
, runSequence = require('run-sequence')
67
;
78

8-
var paths = {
9-
src: 'src/**/*.purs',
10-
bowerSrc: [
11-
'bower_components/purescript-*/src/**/*.purs',
12-
'bower_components/purescript-*/src/**/*.purs.hs'
13-
],
14-
dest: '',
15-
docs: {
16-
'Control.Lens': {
17-
dest: 'src/Control/README.md',
18-
src: 'src/Control/Lens.purs'
19-
},
20-
'Control.Lens.*': {
21-
dest: 'src/Control/Lens/README.md',
22-
src: 'src/Control/Lens/*.purs'
23-
}
24-
}
25-
};
9+
var paths =
10+
{ src: 'src/**/*.purs'
11+
, bowerSrc: [ 'bower_components/purescript-*/src/**/*.purs'
12+
]
13+
, dest: ''
14+
, docs: { 'Control.Lens': { dest: 'src/Control/README.md'
15+
, src: 'src/Control/Lens.purs'
16+
}
17+
, 'Control.Lens.*': { dest: 'src/Control/Lens/README.md'
18+
, src: 'src/Control/Lens/*.purs'
19+
}
20+
}
21+
, test: 'test/**/*.purs'
22+
};
2623

27-
var options = {};
24+
var options =
25+
{ test: { main: 'Test.Control.Lens'
26+
, verboseErrors: true
27+
}
28+
};
2829

29-
var compile = function(compiler) {
30-
var psc = compiler(options);
30+
var compile = function(compiler, src, opts) {
31+
var psc = compiler(opts);
3132
psc.on('error', function(e) {
3233
console.error(e.message);
3334
psc.end();
3435
});
35-
return gulp.src([paths.src].concat(paths.bowerSrc))
36+
return gulp.src(src.concat(paths.bowerSrc))
3637
.pipe(psc)
3738
.pipe(gulp.dest(paths.dest));
3839
};
@@ -51,11 +52,11 @@ function docs (target) {
5152
}
5253

5354
gulp.task('make', function() {
54-
return compile(purescript.pscMake);
55+
return compile(purescript.pscMake, [paths.src], {});
5556
});
5657

5758
gulp.task('browser', function() {
58-
return compile(purescript.psc);
59+
return compile(purescript.psc, [paths.src], {});
5960
});
6061

6162
gulp.task('docs-Control.Lens', docs('Control.Lens'));
@@ -71,4 +72,9 @@ gulp.task('watch-make', function() {
7172
gulp.watch(paths.src, function() {runSequence('make', 'docs')});
7273
});
7374

75+
gulp.task('test', function() {
76+
return compile(purescript.psc, [paths.src, paths.test], options.test)
77+
.pipe(run('node').exec());
78+
});
79+
7480
gulp.task('default', function() {runSequence('make', 'docs')});

src/Control/Lens.purs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ module Control.Lens
44
, Index()
55
, IxValue()
66
, at
7+
, contains
78
, ix
9+
-- Cons
10+
, (<|), (|>)
11+
, _Cons
12+
, _Snoc
13+
, _cons
14+
, head
15+
, init
16+
, last
17+
, snoc
18+
, tail
19+
, uncons
20+
, unsnoc
821
-- Fold
922
, (^..), (^?)
1023
, filtered
@@ -38,6 +51,11 @@ module Control.Lens
3851
, lens
3952
-- Prism
4053
, clonePrism
54+
, is
55+
, isn't
56+
, matching
57+
, nearly
58+
, only
4159
, prism
4260
, prism'
4361
, withPrism
@@ -128,6 +146,7 @@ module Control.Lens
128146
) where
129147

130148
import qualified Control.Lens.At as At
149+
import qualified Control.Lens.Cons as Cons
131150
import qualified Control.Lens.Fold as Fold
132151
import qualified Control.Lens.Getter as Getter
133152
import qualified Control.Lens.Indexed as Indexed
@@ -142,8 +161,25 @@ module Control.Lens
142161
-- At
143162
type Index a b = At.Index a b
144163
type IxValue a b = At.IxValue a b
145-
at = At.at
146-
ix = At.ix
164+
at = At.at
165+
contains = At.contains
166+
ix = At.ix
167+
168+
-- Cons
169+
infixr 5 <|
170+
infixl 5 |>
171+
(<|) = Cons.(<|)
172+
(|>) = Cons.(|>)
173+
_Cons = Cons._Cons
174+
_Snoc = Cons._Snoc
175+
_cons = Cons._cons
176+
head = Cons.head
177+
init = Cons.init
178+
last = Cons.last
179+
snoc = Cons.snoc
180+
tail = Cons.tail
181+
uncons = Cons.uncons
182+
unsnoc = Cons.unsnoc
147183

148184
-- Fold
149185
infixl 8 ^..
@@ -192,6 +228,11 @@ module Control.Lens
192228

193229
-- Prism
194230
clonePrism = Prism.clonePrism
231+
is = Prism.is
232+
isn't = Prism.isn't
233+
matching = Prism.matching
234+
nearly = Prism.nearly
235+
only = Prism.only
195236
prism = Prism.prism
196237
prism' = Prism.prism'
197238
withPrism = Prism.withPrism

src/Control/Lens/At.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module Control.Lens.At
22
( At
3+
, Contains
34
, Ixed
45
, Index()
56
, IxValue()
67
, at
8+
, contains
79
, ix
810
) where
911

@@ -12,8 +14,10 @@ module Control.Lens.At
1214
import Control.Lens.Type (LensP(), TraversalP())
1315
import Control.Monad.Identity (Identity(..))
1416

17+
import Data.Array (delete, elemIndex, snoc, updateAt)
1518
import Data.Maybe (maybe, Maybe(..))
1619
import Data.Traversable (sequence, traverse)
20+
import Data.Tuple (snd)
1721

1822
import qualified Data.Map as M
1923
import qualified Data.Set as S

src/Control/Lens/Cons.purs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module Control.Lens.Cons
2+
( Cons
3+
, Snoc
4+
, (<|)
5+
, (|>)
6+
, _Cons
7+
, _Snoc
8+
, _cons
9+
, head
10+
, init
11+
, last
12+
, snoc
13+
, tail
14+
, uncons
15+
, unsnoc
16+
) where
17+
18+
import Control.Lens.Equality (simply)
19+
import Control.Lens.Fold ((^?))
20+
import Control.Lens.Prism (prism)
21+
import Control.Lens.Review (review)
22+
import Control.Lens.Tuple (_1, _2)
23+
import Control.Lens.Type (Prism(), TraversalP())
24+
import Control.Monad.Reader.Class (MonadReader)
25+
26+
import Data.Either (Either(..))
27+
import Data.Maybe (Maybe())
28+
import Data.Profunctor (dimap)
29+
import Data.Tuple (curry, snd, uncurry, Tuple(..))
30+
31+
import Prelude hiding (cons)
32+
33+
import qualified Data.Array as A
34+
import qualified Data.Array.Unsafe as AU
35+
36+
infixr 5 <|
37+
infixl 5 |>
38+
39+
class Cons s t a b where
40+
_Cons :: Prism (s a) (t b) (Tuple a (s a)) (Tuple b (t b))
41+
42+
instance consArray :: Cons [] [] a b where
43+
_Cons = prism (uncurry (:)) \xs -> case xs of
44+
(x:xs') -> Right (Tuple x xs')
45+
[] -> Left []
46+
47+
class Snoc s t a b where
48+
_Snoc :: Prism (s a) (t b) (Tuple (s a) a) (Tuple (t b) b)
49+
50+
instance snocArray :: Snoc [] [] a b where
51+
_Snoc = prism (uncurry A.snoc) \xs -> case xs of
52+
[] -> Left []
53+
_ -> Right (Tuple (AU.init xs) (AU.last xs))
54+
55+
(<|) :: forall a s. (Cons s s a a) => a -> s a -> s a
56+
(<|) = curry (simply review _Cons)
57+
58+
_cons :: forall a s. (Cons s s a a) => a -> s a -> s a
59+
_cons = (<|)
60+
61+
uncons :: forall a s. (Cons s s a a) => s a -> Maybe (Tuple a (s a))
62+
uncons xs = xs ^? _Cons
63+
64+
head :: forall a s. (Cons s s a a) => TraversalP (s a) a
65+
head = _Cons <<< _1
66+
67+
tail :: forall a s. (Cons s s a a) => TraversalP (s a) (s a)
68+
tail = _Cons <<< _2
69+
70+
(|>) :: forall a s. (Snoc s s a a) => s a -> a -> s a
71+
(|>) = curry (simply review _Snoc)
72+
73+
snoc :: forall a s. (Snoc s s a a) => s a -> a -> s a
74+
snoc = curry (simply review _Snoc)
75+
76+
unsnoc :: forall a s. (Snoc s s a a) => s a -> Maybe (Tuple (s a) a)
77+
unsnoc xs = xs ^? _Snoc
78+
79+
init :: forall a s. (Snoc s s a a) => TraversalP (s a) (s a)
80+
init = _Snoc <<< _1
81+
82+
last :: forall a s. (Snoc s s a a) => TraversalP (s a) a
83+
last = _Snoc <<< _2

src/Control/Lens/Equality.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Control.Lens.Equality where
2+
3+
import Control.Lens.Type (OpticP())
4+
5+
simply :: forall p f s a r. (OpticP p f s a -> r) -> OpticP p f s a -> r
6+
simply = id

src/Control/Lens/Prism.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ module Control.Lens.Prism
22
( APrism()
33
, APrismP()
44
, clonePrism
5+
, is
6+
, isn't
7+
, matching
8+
, nearly
9+
, only
510
, prism
611
, prism'
712
, withPrism
@@ -14,6 +19,7 @@ module Control.Lens.Prism
1419
import Control.Lens.Internal.Prism (Market(..))
1520
import Control.Lens.Type (Lens(), Prism(), PrismP())
1621
import Control.Monad.Identity (runIdentity, Identity(..))
22+
import Control.MonadPlus (guard)
1723

1824
import Data.Profunctor (dimap, lmap, Profunctor)
1925
import Data.Profunctor.Choice (right', Choice)
@@ -27,6 +33,21 @@ module Control.Lens.Prism
2733
clonePrism :: forall f p s t a b. (Applicative f, Choice p) => APrism s t a b -> p a (f b) -> p s (f t)
2834
clonePrism stab = withPrism stab prism
2935

36+
is :: forall s t a b. APrism s t a b -> s -> Boolean
37+
is stab s = either (const false) (const true) $ matching stab s
38+
39+
isn't :: forall s t a b. APrism s t a b -> s -> Boolean
40+
isn't stab s = not $ is stab s
41+
42+
matching :: forall s t a b. APrism s t a b -> s -> Either t a
43+
matching stab = withPrism stab \_ s -> s
44+
45+
nearly :: forall a. a -> (a -> Boolean) -> PrismP a Unit
46+
nearly x p = prism' (\_ -> x) $ guard <<< p
47+
48+
only :: forall a. (Eq a) => a -> PrismP a Unit
49+
only x = nearly x ((==) x)
50+
3051
-- outside :: forall f p r s t a b. (Profunctor p, Representable p f) => APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
3152
-- outside stab = withPrism stab \b2t s2Eta f ft ->
3253
-- (\fa -> tabulate $ either (rep ft) (rep fa) <<< s2Eta) <$> f (lmap b2t ft)

0 commit comments

Comments
 (0)