-
Notifications
You must be signed in to change notification settings - Fork 1
/
clos.js
166 lines (136 loc) · 3.33 KB
/
clos.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var clos = exports = module.exports;
var _ = require('underscore'),
util = require('util');
// error
clos.error = function(){};
util.inherits(clos.error, Error);
clos.noMethod = function(generic, parameters){
this.message = 'no ' + generic + ' for ' + _.invoke(parameters, 'toString').join(', ');
};
util.inherits(clos.noMethod, clos.error);
// identity
clos.is = function(example, standard){
if(_.isUndefined(standard)){
return true;
}
if(_.isEqual(example, standard)){
return true;
}
if(standard.validate && standard.validate(example)){
return true;
}
if(clos.isInstance(example, standard)){
return true;
}
return false;
};
clos.type = function(name, validate){
this.name = name;
this.validate = validate;
clos[name] = clos.types[name] = this;
};
clos.types = {};
new clos.type('boolean', _.isBoolean);
new clos.type('number', _.isNumber);
new clos.type('string', _.isString);
new clos.type('array', _.isArray);
clos.isInstance = function(example, standard){
if(_.isEqual(example.prototype, standard)){
return true;
}
var origin = example.origin;
if(!origin){
return false;
}
if(_.isArray(origin)){
return _.any(origin, function(parent){
return clos.is(parent, standard);
});
}else{
return clos.is(origin, standard);
}
};
// symbol
clos.symbols = {};
clos.symbol = function(name, origin){
this.name = name;
this.origin = origin || [];
clos.symbols[name] = this;
};
// generic
clos.generics = {};
clos.generic = function(name){
this.name = name;
this.methods = [];
clos.generics[name] = this;
};
clos.generic.prototype.lambda = function(){
var generic = this;
var lambda = function(){
return generic.call.apply(generic, arguments);
};
lambda.generic = generic;
lambda.toString = function(){
return generic.toString();
};
return lambda;
};
clos.generic.prototype.call = function(){
var generic = this,
parameters = arguments,
methods = generic.find.apply(generic, parameters);
if(!methods.length){
throw new clos.noMethod(generic, parameters);
}
methods = methods.sort(generic.order);
var result;
_.any(methods, function(method){
return result = method.call.apply(method, parameters);
});
return result;
};
clos.generic.prototype.find = function(){
var generic = this,
parameters = arguments;
return _.filter(generic.methods, function(method){
if(method.check(parameters)){
return method;
}
});
};
// method
clos.method = function(generic, clause, body){
this.clause = clause;
this.body = body;
generic.methods.push(this);
};
clos.method.prototype.check = function(parameters){
var clause = this.clause;
if(!_.isArray(clause)){
clause = [clause];
}
var index = -1;
return _.every(clause, function(clause){
index++;
return clos.is(parameters[index], clause);
});
};
clos.generic.prototype.order = function(method1, method2){
return method1.clause.length - method2.clause.length;
};
clos.method.prototype.call = function(){
return this.body.apply(undefined, arguments);
};
// pretty print
clos.error.prototype.toString = function(){
return '<error from="clos">' + this.message + '</error>';
};
clos.symbol.prototype.toString = function(){
return '<symbol name="' + this.name + '" />';
};
clos.type.prototype.toString = function(){
return '<type name="' + this.name + '" />';
};
clos.generic.prototype.toString = function(){
return '<generic name="' + this.name + '" />';
};