-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Compose.js
54 lines (42 loc) · 1.43 KB
/
Compose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var FL = require ('fantasy-land');
var ap = require ('./ap');
var equals = require ('./equals');
var map = require ('./map');
var of = require ('./of');
var toString = require ('./toString');
// Compose :: (Apply f, Apply g) => TypeRep f -> TypeRep g -> f (g a) -> Compose f g a
module.exports = function Compose(F) {
return function ComposeF(G) {
function ComposeFG(value) {
if (!(this instanceof ComposeFG)) return new ComposeFG (value);
this.value = value;
}
ComposeFG['@@type'] = 'sanctuary/Compose';
ComposeFG[FL.of] = function(x) {
return ComposeFG (of (F) (of (G) (x)));
};
ComposeFG.prototype[FL.equals] = function(other) {
return equals (this.value) (other.value);
};
ComposeFG.prototype[FL.map] = function(f) {
return ComposeFG (map (map (f)) (this.value));
};
ComposeFG.prototype[FL.ap] = function(other) {
return ComposeFG (ap (map (ap) (other.value)) (this.value));
};
// name :: TypeRep a -> String
function name(typeRep) {
return typeof typeRep['@@type'] === 'string' ?
typeRep['@@type'].replace (/^[^/]*[/]/, '') :
typeRep.name;
}
ComposeFG.prototype.inspect =
ComposeFG.prototype.toString = function() {
return 'Compose(' + name (F) + ')' +
'(' + name (G) + ')' +
'(' + toString (this.value) + ')';
};
return ComposeFG;
};
};