Browse code
removing tmp files
Showing 4 changed files
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,62 +0,0 @@ |
1 |
- |
|
2 |
-window.devtoolsFormatters = [ |
|
3 |
- { |
|
4 |
- header(obj, config) { |
|
5 |
- if (config && config.genfunFormatter) { |
|
6 |
- return ["div", {}, config.key]; |
|
7 |
- } else if (! (obj.gf || obj instanceof GenericFunction) ) { |
|
8 |
- return null; |
|
9 |
- } else if (obj.gf) { |
|
10 |
- const args = obj.gf.lambda_list.join(', '); |
|
11 |
- const method_count = obj.gf.methods.length |
|
12 |
- return [ |
|
13 |
- 'div', {}, |
|
14 |
- `GenericFunction lambda: ${obj.gf.name}(${args}) ` |
|
15 |
- + `[${method_count} methods]` |
|
16 |
- ]; |
|
17 |
- } else { |
|
18 |
- const args = obj.lambda_list.join(', '); |
|
19 |
- const method_count = obj.methods.length |
|
20 |
- return [ |
|
21 |
- 'div', {}, |
|
22 |
- `#<GenericFunction: ${obj.name}(${args}) [${method_count} methods]>` |
|
23 |
- ]; |
|
24 |
- } |
|
25 |
- }, |
|
26 |
- hasBody(obj) {return obj instanceof GenericFunction || obj instanceof StandardMethod;}, |
|
27 |
- body(obj, config) { |
|
28 |
- if (! (obj instanceof GenericFunction || obj instanceof StandardMethod) ) { |
|
29 |
- return null; |
|
30 |
- } else if ( obj instanceof StandardMethod ) { |
|
31 |
- return ["div", {style: 'margin-left: 2em'}].concat( |
|
32 |
- Object.keys(obj).map( |
|
33 |
- key => { |
|
34 |
- if (obj[key] instanceof String) { |
|
35 |
- return ["div", {}, `${key}: ${obj[key]},`]; |
|
36 |
- } else { |
|
37 |
- return ["div", {}, `${key}: `, ["object", {object: obj[key]}], ',']; |
|
38 |
- } |
|
39 |
- } |
|
40 |
- ) |
|
41 |
- ); |
|
42 |
- } |
|
43 |
- |
|
44 |
- const children = obj.methods.map( |
|
45 |
- (method,idx) => { |
|
46 |
- const child = [ |
|
47 |
- "object", { |
|
48 |
- object: method, |
|
49 |
- config: { |
|
50 |
- genfunFormatter: true, |
|
51 |
- key: `#<StandardMethod ${method.qualifiers.map(x => ':'+x.toString().slice(7,-1)).join(' ')} (${method.specializers.map(x => x.name.toString())})>`, |
|
52 |
- }, |
|
53 |
- }, |
|
54 |
- ]; |
|
55 |
- return ["div", {style: `margin-left: 2em;`}, |
|
56 |
- child] |
|
57 |
- } |
|
58 |
- ); |
|
59 |
- return ["div", {}].concat(children); |
|
60 |
- } |
|
61 |
- } |
|
62 |
-]; |
63 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,234 +0,0 @@ |
1 |
-const before_qualifier = Symbol.for('before'); |
|
2 |
-const after_qualifier = Symbol.for('after'); |
|
3 |
-const around_qualifier = Symbol.for('around'); |
|
4 |
- |
|
5 |
-let genfun_prototype = { |
|
6 |
- name: "(placeholder)", |
|
7 |
- lambda_list: [], |
|
8 |
- methods: [], |
|
9 |
- method(qualifiers, specializers, body) { |
|
10 |
- ensure_method(this, this.lambda_list, qualifiers, specializers, body) |
|
11 |
- return this; |
|
12 |
- }, |
|
13 |
- primary(specializers, body) { |
|
14 |
- return this.method([], specializers, body); |
|
15 |
- }, |
|
16 |
- before(specializers, body) { |
|
17 |
- return this.method([before_qualifier], specializers, body); |
|
18 |
- }, |
|
19 |
- after(specializers, body) { |
|
20 |
- return this.method([after_qualifier], specializers, body); |
|
21 |
- }, |
|
22 |
- around(specializers, body) { |
|
23 |
- return this.method([around_qualifier], specializers, body); |
|
24 |
- }, |
|
25 |
- get fn() { |
|
26 |
- const gf = this; |
|
27 |
- const lambda = (function() { |
|
28 |
- return apply_generic_function(gf, [].slice.call(arguments)); |
|
29 |
- }).bind(gf); |
|
30 |
- return Object.defineProperties(lambda, { |
|
31 |
- 'name': {value: gf.name}, |
|
32 |
- 'lambda_list': {value: gf.lambda_list}, |
|
33 |
- 'gf': {value: gf}, |
|
34 |
- }); |
|
35 |
- } |
|
36 |
-}; |
|
37 |
- |
|
38 |
-function GenericFunction(name, lambda_list) { |
|
39 |
- if (! (this instanceof GenericFunction) ) { |
|
40 |
- return new GenericFunction(...arguments); |
|
41 |
- } |
|
42 |
- |
|
43 |
- this.name = name; |
|
44 |
- this.lambda_list = lambda_list; |
|
45 |
- this.methods = []; |
|
46 |
-} |
|
47 |
- |
|
48 |
-GenericFunction.prototype = Object.create(genfun_prototype); |
|
49 |
- |
|
50 |
-let method_prototype = { |
|
51 |
- lambda_list: [], |
|
52 |
- qualifiers: [], |
|
53 |
- specializrs: [], |
|
54 |
- body() { throw new Error('Unimplemented'); }, |
|
55 |
- environment: {}, |
|
56 |
- generic_function: {}, |
|
57 |
-}; |
|
58 |
- |
|
59 |
-function StandardMethod( |
|
60 |
- lambda_list, qualifiers, specializers, body |
|
61 |
-) { |
|
62 |
- if (! (this instanceof StandardMethod) ) { |
|
63 |
- return new StandardMethod(...arguments); |
|
64 |
- } |
|
65 |
- |
|
66 |
- this.lambda_list = lambda_list; |
|
67 |
- this.qualifiers = qualifiers; |
|
68 |
- this.specializers = specializers; |
|
69 |
- this.body = body; |
|
70 |
- this.generic_function = null; |
|
71 |
-} |
|
72 |
- |
|
73 |
-function ensure_method(gf, lambda_list, qualifiers, specializers, body) { |
|
74 |
- let new_method = StandardMethod(...[].slice.call(arguments, 1)); |
|
75 |
- add_method(gf, new_method); |
|
76 |
- return new_method; |
|
77 |
-} |
|
78 |
- |
|
79 |
-function add_method(gf, method) { |
|
80 |
- method.generic_function = gf; |
|
81 |
- gf.methods.push(method); |
|
82 |
- return method; |
|
83 |
-} |
|
84 |
- |
|
85 |
-function classes_of(args) { |
|
86 |
- return args.map(Object.getPrototypeOf); |
|
87 |
-} |
|
88 |
- |
|
89 |
-const required_portion = x => x; |
|
90 |
- |
|
91 |
-function apply_generic_function(gf, args) { |
|
92 |
- let applicable_methods = |
|
93 |
- compute_applicable_methods_using_classes(gf, required_portion(args)); |
|
94 |
- if (applicable_methods.length === 0) { |
|
95 |
- throw new Error(`no applicable methods for gf ${gf.name} with args ${JSON.stringify(args)}`); |
|
96 |
- } else { |
|
97 |
- return apply_methods(gf, args, applicable_methods); |
|
98 |
- } |
|
99 |
-} |
|
100 |
- |
|
101 |
-function method_more_specific_p(m1, m2, required_classes) { |
|
102 |
- const m1specializers = m1.specializers; |
|
103 |
- const m2specializers = m2.specializers; |
|
104 |
- |
|
105 |
- for ([spec1, spec2] of m1specializers.map((el, idx) => [el, m2specializers[idx]])) { |
|
106 |
- if (spec1 !== spec2) { |
|
107 |
- return sub_specializer_p(spec1, spec2); |
|
108 |
- } |
|
109 |
- } |
|
110 |
-} |
|
111 |
- |
|
112 |
-function sub_specializer_p(c1, c2) { |
|
113 |
- return c1.isPrototypeOf(c2); |
|
114 |
-} |
|
115 |
- |
|
116 |
-const idS = Symbol.for('id'); |
|
117 |
-Object.prototype[idS] = function () { return this }; |
|
118 |
- |
|
119 |
-export function matchesSpecializer(obj, specializer) { |
|
120 |
- let result = obj === specializer.prototype; |
|
121 |
- let objType = typeof obj; |
|
122 |
- |
|
123 |
- if (!result && objType === 'object') { |
|
124 |
- result = Object.isPrototypeOf.call(specializer.prototype, obj); |
|
125 |
- } else if (objType === 'number') { |
|
126 |
- result = matchesSpecializer(Number.prototype, specializer) || matchesSpecializer(specializer.prototype, Number); |
|
127 |
- } else if (objType === 'string') { |
|
128 |
- result = matchesSpecializer(String.prototype, specializer) || matchesSpecializer(specializer.prototype, String); |
|
129 |
- } |
|
130 |
- |
|
131 |
- return result; |
|
132 |
-} |
|
133 |
- |
|
134 |
- |
|
135 |
-function compute_applicable_methods_using_classes(gf, required_classes) { |
|
136 |
- const applicable_methods = gf.methods.filter( |
|
137 |
- method => method.specializers.every((specializer, idx) => matchesSpecializer(required_classes[idx], specializer)) |
|
138 |
- ); |
|
139 |
- |
|
140 |
- applicable_methods.sort((a,b) => { |
|
141 |
- if (method_more_specific_p(a,b)) { |
|
142 |
- return -1; |
|
143 |
- } |
|
144 |
- if (method_more_specific_p(b,a)) { |
|
145 |
- return 1; |
|
146 |
- } |
|
147 |
- return 0; |
|
148 |
- }) |
|
149 |
- |
|
150 |
- return applicable_methods; |
|
151 |
-} |
|
152 |
- |
|
153 |
- |
|
154 |
-function arr_eq(a1, a2) { |
|
155 |
- if (a1.length !== a2.length) { |
|
156 |
- return false; |
|
157 |
- } else { |
|
158 |
- for (let x = 0; x < a1.length; x++) { |
|
159 |
- if (a1[x] instanceof Array && a2[x] instanceof Array) { |
|
160 |
- if (!arr_eq(a1[x], a2[x])) { |
|
161 |
- return false; |
|
162 |
- } |
|
163 |
- } else if (a1[x] !== a2[x]) { |
|
164 |
- return false; |
|
165 |
- } |
|
166 |
- } |
|
167 |
- return true; |
|
168 |
- } |
|
169 |
-} |
|
170 |
- |
|
171 |
-const primary_method_p = method => method instanceof WrappedMethod || method.qualifiers.length === 0; |
|
172 |
-const before_method_p = method => !(method instanceof WrappedMethod) && arr_eq(method.qualifiers, [before_qualifier]); |
|
173 |
-const after_method_p = method => !(method instanceof WrappedMethod) && arr_eq(method.qualifiers, [after_qualifier]); |
|
174 |
-const around_method_p = method => !(method instanceof WrappedMethod) && arr_eq(method.qualifiers, [around_qualifier]); |
|
175 |
- |
|
176 |
-function WrappedMethod(continuation) { |
|
177 |
- this.continuation = continuation; |
|
178 |
-} |
|
179 |
- |
|
180 |
-function apply_methods(gf, args, applicable_methods) { |
|
181 |
- const primaries = applicable_methods.filter(primary_method_p); |
|
182 |
- const befores = applicable_methods.filter(before_method_p); |
|
183 |
- const arounds = applicable_methods.filter(around_method_p); |
|
184 |
- const afters = applicable_methods.filter(after_method_p); |
|
185 |
- afters.reverse(); |
|
186 |
- |
|
187 |
- const main_call = Object.defineProperty( |
|
188 |
- function() { |
|
189 |
- if (primaries.length === 0) { |
|
190 |
- throw new Error(`No primary method for ${gf.name}`); |
|
191 |
- } |
|
192 |
- |
|
193 |
- for (let before of befores) { |
|
194 |
- apply_method(before, args, []); |
|
195 |
- } |
|
196 |
- |
|
197 |
- try { |
|
198 |
- return apply_method(primaries[0], args, primaries.slice(1)); |
|
199 |
- } finally { |
|
200 |
- for (let after of afters) { |
|
201 |
- apply_method(after, args, []); |
|
202 |
- } |
|
203 |
- } |
|
204 |
- }, |
|
205 |
- 'name', {value: `main_call_${gf.name}`}, |
|
206 |
- ); |
|
207 |
- |
|
208 |
- if (arounds.length === 0) { |
|
209 |
- return main_call(); |
|
210 |
- } else { |
|
211 |
- const wrapped_main_call = new WrappedMethod(main_call); |
|
212 |
- const next_methods = arounds.slice(1).concat([wrapped_main_call]); |
|
213 |
- return apply_method(arounds[0], args, next_methods); |
|
214 |
- } |
|
215 |
-} |
|
216 |
- |
|
217 |
-function apply_method(method, args, next_methods) { |
|
218 |
- const method_context = { |
|
219 |
- call_next_method(...cnm_args) { |
|
220 |
- if (next_methods.length === 0) { |
|
221 |
- throw new Error(`no next method for genfun ${method.generic_function.name}`); |
|
222 |
- } |
|
223 |
- |
|
224 |
- return method instanceof WrappedMethod |
|
225 |
- ? method.continuation() |
|
226 |
- : apply_methods(method.generic_function, cnm_args.length > 0 ? cnm_args : args, next_methods); |
|
227 |
- }, |
|
228 |
- get next_method_p() { |
|
229 |
- return next_methods.length === 0 |
|
230 |
- } |
|
231 |
- }; |
|
232 |
- |
|
233 |
- return method.body ? method.body.bind(method_context)(...args) : method.continuation(); |
|
234 |
-} |
235 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,47 +0,0 @@ |
1 |
-import * as uut from './genfuns.js'; |
|
2 |
- |
|
3 |
-describe('matchesSpecializer', () => { |
|
4 |
- function AThing() {}; |
|
5 |
- const an_instance = new AThing(); |
|
6 |
- |
|
7 |
- test('works in expected cases', () => { |
|
8 |
- expect(uut.matchesSpecializer(an_instance, AThing)).toBeTruthy(); |
|
9 |
- expect(uut.matchesSpecializer(an_instance, String)).toBeFalsy(); |
|
10 |
- expect(uut.matchesSpecializer(an_instance, Object)).toBeTruthy(); |
|
11 |
- |
|
12 |
- expect(uut.matchesSpecializer(new String("foobar"), String)).toBeTruthy(); |
|
13 |
- expect(uut.matchesSpecializer(new String("foobar"), Object)).toBeTruthy(); |
|
14 |
- |
|
15 |
- expect(uut.matchesSpecializer(new Number(1), Number)).toBeTruthy(); |
|
16 |
- expect(uut.matchesSpecializer(new Number(1), Object)).toBeTruthy(); |
|
17 |
- expect(uut.matchesSpecializer(new Number(1), String)).toBeFalsy(); |
|
18 |
- |
|
19 |
- expect(uut.matchesSpecializer([], Array)).toBeTruthy(); |
|
20 |
- expect(uut.matchesSpecializer([], Object)).toBeTruthy(); |
|
21 |
- expect(uut.matchesSpecializer([], Number)).toBeFalsy(); |
|
22 |
- |
|
23 |
- function Foo() {} |
|
24 |
- Foo.prototype = Object.create(null); |
|
25 |
- const inst = new Foo(); |
|
26 |
- expect(uut.matchesSpecializer(inst, Foo)).toBeTruthy(); |
|
27 |
- expect(uut.matchesSpecializer(inst, Object)).toBeFalsy(); |
|
28 |
- }); |
|
29 |
- |
|
30 |
- test('works in for primitives', () => { |
|
31 |
- expect(uut.matchesSpecializer(1, Number)).toBeTruthy(); |
|
32 |
- expect(uut.matchesSpecializer(1, Object)).toBeTruthy(); |
|
33 |
- expect(uut.matchesSpecializer(1, String)).toBeFalsy(); |
|
34 |
- |
|
35 |
- expect(uut.matchesSpecializer("1", String)).toBeTruthy(); |
|
36 |
- expect(uut.matchesSpecializer("1", Object)).toBeTruthy(); |
|
37 |
- expect(uut.matchesSpecializer("1", Number)).toBeFalsy(); |
|
38 |
- |
|
39 |
- expect(uut.matchesSpecializer(null, Number)).toBeFalsy(); |
|
40 |
- expect(uut.matchesSpecializer(null, String)).toBeFalsy(); |
|
41 |
- expect(uut.matchesSpecializer(null, Object)).toBeFalsy(); |
|
42 |
- |
|
43 |
- expect(uut.matchesSpecializer(undefined, Number)).toBeFalsy(); |
|
44 |
- expect(uut.matchesSpecializer(undefined, String)).toBeFalsy(); |
|
45 |
- expect(uut.matchesSpecializer(undefined, Object)).toBeFalsy(); |
|
46 |
- }); |
|
47 |
-}) |
48 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,88 +0,0 @@ |
1 |
-function zipWith(fn, ...args) { |
|
2 |
- const minLen = Math.min(...args.map(x => x.length)); |
|
3 |
- const res = []; |
|
4 |
- |
|
5 |
- for (let x = 0; x < minLen; x++) { |
|
6 |
- res.push(fn(...args.map(a => a[x]))); |
|
7 |
- } |
|
8 |
- |
|
9 |
- return res; |
|
10 |
-} |
|
11 |
- |
|
12 |
-const gf = GenericFunction( |
|
13 |
- "foobar", ["a", "b"] |
|
14 |
-).before( |
|
15 |
- [Object, Array], function (a,b) {console.log('in before', this.next_method_p);} |
|
16 |
-).primary( |
|
17 |
- [Object, Array], function (a,b) { |
|
18 |
- console.info('next_result: ', this.call_next_method(), this.next_method_p); |
|
19 |
- return [a,...b]; |
|
20 |
- } |
|
21 |
-).primary( |
|
22 |
- [Object, Object], function (thing, single) { |
|
23 |
- console.log("hello from previous method", this.next_method_p); |
|
24 |
- return [thing, single]; |
|
25 |
- } |
|
26 |
-).after( |
|
27 |
- [Number, Array], function (a,b) {console.log(`in after for ${a}`, this.next_method_p);} |
|
28 |
-).fn; |
|
29 |
- |
|
30 |
-function groupGFMessages(gf) { |
|
31 |
- return gf.method([around_qualifier], [Object,Object], function(a,b) { |
|
32 |
- console.groupCollapsed(gf.name); |
|
33 |
- try { |
|
34 |
- return this.call_next_method(); |
|
35 |
- } finally { |
|
36 |
- console.groupEnd(); |
|
37 |
- } |
|
38 |
- }) |
|
39 |
-} |
|
40 |
- |
|
41 |
-groupGFMessages(gf.gf); |
|
42 |
- |
|
43 |
-console.log(gf(2,["asdf"])); |
|
44 |
- |
|
45 |
-const gf2 = GenericFunction( |
|
46 |
- "another", ["a"] |
|
47 |
-).primary( |
|
48 |
- [Object], function (a) { return {value: a}; } |
|
49 |
-).method( |
|
50 |
- [around_qualifier], [Number], function(thing) { |
|
51 |
- console.log('before next method in number around'); |
|
52 |
- const val = this.call_next_method(); |
|
53 |
- console.log('after next method in number around', val); |
|
54 |
- return {was_num: true, ...val}; |
|
55 |
- } |
|
56 |
-).method( |
|
57 |
- [around_qualifier], [Object], function(thing) { |
|
58 |
- console.log('before next method in generic around'); |
|
59 |
- const val = this.call_next_method(); |
|
60 |
- console.log('after next method in generic around', val); |
|
61 |
- return {was_obj: true, ...val}; |
|
62 |
- } |
|
63 |
-); |
|
64 |
- |
|
65 |
-function MyComponent() { |
|
66 |
- this.name = 'foo'; |
|
67 |
- this.address = '1234 asdfadfd' |
|
68 |
-} |
|
69 |
- |
|
70 |
-const renderFn = GenericFunction( |
|
71 |
- "dorender", ["component", "el"] |
|
72 |
-).primary( |
|
73 |
- [MyComponent, HTMLHeadingElement], (comp, heading) => { |
|
74 |
- console.log('heading el'); |
|
75 |
- heading.textContent = comp.name; |
|
76 |
- } |
|
77 |
-).primary( |
|
78 |
- [MyComponent, HTMLDivElement], (comp, el) => { |
|
79 |
- console.log('address el'); |
|
80 |
- el.textContent = comp.address; |
|
81 |
- } |
|
82 |
-).primary( |
|
83 |
- [MyComponent, HTMLElement], (comp, el) => { |
|
84 |
- console.log('HtmlElement el ', el); |
|
85 |
- renderFn.fn(comp, el.querySelector('h2')); |
|
86 |
- renderFn.fn(comp, el.querySelector('div')); |
|
87 |
- } |
|
88 |
-) |