From 6e18ea2f5bc535c711d2f05fcf3176c288664144 Mon Sep 17 00:00:00 2001 From: Fadi Shawki Date: Tue, 16 Jan 2024 15:37:41 +0100 Subject: [PATCH] 2024/01/16 - Iterating on some ideas to implement 'next' --- src/@orbitmines/explorer/Ray.spec.ts | 45 +++++++++++++++------------- src/@orbitmines/explorer/Ray.ts | 44 ++++++++++++++------------- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/@orbitmines/explorer/Ray.spec.ts b/src/@orbitmines/explorer/Ray.spec.ts index c310f19..70e20cf 100644 --- a/src/@orbitmines/explorer/Ray.spec.ts +++ b/src/@orbitmines/explorer/Ray.spec.ts @@ -20,14 +20,14 @@ describe("Ray", () => { expect(method(a)(b).terminal.self.any.js).toBe('B'); expect(method(a)(b).type).toBe(RayType.VERTEX); }); - test(".next", () => { + test(".next()", () => { const A = Ray.vertex().o({ js: 'A' }).as_reference().o({ js: 'A.#' }); const B = Ray.vertex().o({ js: 'B' }).as_reference().o({ js: 'B.#' }); const C = Ray.vertex().o({ js: 'C' }).as_reference().o({ js: 'C.#' }); A.continues_with(B).continues_with(C); - // let pointer = A.step(Ray.directions.next(A)); + // let pointer = A.step(Ray.directions.next()(A)); let pointer = new Ray({ initial: () => A, @@ -39,6 +39,9 @@ describe("Ray", () => { * [ |--] [--| ][ |--] [--| ][ |--] [--| ] * [--A--] [--B--] [--C--] */ + + expect(pointer.initial.self.any.js).toBe('A'); + expect(pointer.initial.type).toBe(RayType.VERTEX); expect(pointer.terminal.type).toBe(RayType.TERMINAL); @@ -115,33 +118,33 @@ describe("Ray", () => { // expect(pointer.terminal.type).toBe(RayType.TERMINAL); ?? expect(pointer.terminal.is_none()).toBe(true); }); - test("[A, B, C][.next, .previous]", () => { + test("[A, B, C][.next(), .previous()]", () => { const A = Ray.vertex().o({ js: 'A' }).as_reference().o({ js: 'A.#' }); const B = Ray.vertex().o({ js: 'B' }).as_reference().o({ js: 'B.#' }); const C = Ray.vertex().o({ js: 'C' }).as_reference().o({ js: 'C.#' }); - expect(() => A.next).toThrow(); // TODO: Should be empty.. - expect(() => A.previous).toThrow(); // TODO: Should be empty.. + expect(() => A.next()).toThrow(); // TODO: Should be empty.. + expect(() => A.previous()).toThrow(); // TODO: Should be empty.. A.continues_with(B).continues_with(C); - expect(() => A.previous).toThrow(); // TODO: Should be ??.. - expect(() => A.next.next.next).toThrow(); // TODO: Should be ??.. + expect(() => A.previous()).toThrow(); // TODO: Should be ??.. + expect(() => A.next().next().next()).toThrow(); // TODO: Should be ??.. - expect(A.next.type).toBe(RayType.VERTEX); - // expect(current.next.any.js).toBe('B.#'); TODO, maybe the ref?? - expect(A.next.self.any.js).toBe('B'); + expect(A.next().type).toBe(RayType.VERTEX); + // expect(current.next().any.js).toBe('B.#'); TODO, maybe the ref?? + expect(A.next().self.any.js).toBe('B'); - expect(A.next.next.type).toBe(RayType.VERTEX); - expect(A.next.next.self.any.js).toBe('C'); + expect(A.next().next().type).toBe(RayType.VERTEX); + expect(A.next().next().self.any.js).toBe('C'); - expect(A.next.previous.self.any.js).toBe('A'); - expect(A.next.next.previous.self.any.js).toBe('B'); - expect(A.next.next.previous.previous.self.any.js).toBe('A'); - expect(A.next.previous.next.next.previous.self.any.js).toBe('B'); - expect(A.next.previous.next.next.previous.next.self.any.js).toBe('C'); + expect(A.next().previous().self.any.js).toBe('A'); + expect(A.next().next().previous().self.any.js).toBe('B'); + expect(A.next().next().previous().previous().self.any.js).toBe('A'); + expect(A.next().previous().next().next().previous().self.any.js).toBe('B'); + expect(A.next().previous().next().next().previous().next().self.any.js).toBe('C'); }); - // test("[A, [X, Y, Z].initial, B, C][.next, .previous]", () => { + // test("[A, [X, Y, Z].initial, B, C][.next(), .previous()]", () => { // /** // * | | // * |-- A --| |-- B --|-- C --| @@ -202,7 +205,7 @@ describe("Ray", () => { // } // )).toEqual([true, true, true]); // From the perspective of the 'terminal' it's ignorant of 'ret'. // - // expect(A.next.type).toBe(RayType.INITIAL); + // expect(A.next().type).toBe(RayType.INITIAL); // // /** // * | | @@ -287,11 +290,11 @@ describe("Ray", () => { // TODO async_generator / async_iterator / for await * }); - // test(".next(ref => .continues_with(.vertex.#))", () => { + // test(".next()(ref => .continues_with(.vertex.#))", () => { // let A = Ray.vertex().o({ js: 'A' }).as_reference(); // let B = Ray.vertex().o({ js: 'B'}).as_reference(); // - // B = A.next(ref => ref.continues_with(B)) + // B = A.next()(ref => ref.continues_with(B)) // // expect(B.type).toBe(RayType.VERTEX); // expect(B.self.any.js).toBe('B'); diff --git a/src/@orbitmines/explorer/Ray.ts b/src/@orbitmines/explorer/Ray.ts index 90b8a6a..0c24a9e 100644 --- a/src/@orbitmines/explorer/Ray.ts +++ b/src/@orbitmines/explorer/Ray.ts @@ -525,6 +525,14 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? // } // }); + // const step: Implementation = (ref: Ray): Ray => { + // const pointer = new Ray({ + // initial: ref.as_arbitrary(), + // terminal: _first.as_arbitrary() + // }); + // } + + // terminal: this._terminal // Pass terminal without evaluating return { @@ -574,11 +582,6 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? } } - /** - * TODO: next/continues_with/compose all generalizable?? - * - * @param direction Generalized as 'some function'. - */ static ___next = (step: Implementation) => { const method = Ray.___func(ref => { const { initial, terminal } = ref.self; @@ -590,26 +593,26 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? [RayType.INITIAL]: (ref) => ref.self.___primitive_switch({ [RayType.TERMINAL]: (ref) => ref.self.initial.as_reference() - .___primitive_switch({ - // Found a next Vertex. - [RayType.VERTEX]: (self) => self, + .___primitive_switch({ + // Found a next Vertex. + [RayType.VERTEX]: (self) => self, - }), + }), }), [RayType.TERMINAL]: (ref) => ref.self.___primitive_switch({ // A possible continuation [RayType.INITIAL]: (ref) => ref.self.terminal.as_reference() - .___primitive_switch({ // TODO: This is applying the function again, should be separate? - // Found a next Vertex. - [RayType.VERTEX]: (self) => self, + .___primitive_switch({ // TODO: This is applying the function again, should be separate? + // Found a next Vertex. + [RayType.VERTEX]: (self) => self, - // TODO: Same, but defined a step further - // [RayType.TERMINAL]: () => Ray.None(), - [RayType.TERMINAL]: () => { throw new NotImplementedError(); }, + // TODO: Same, but defined a step further + // [RayType.TERMINAL]: () => Ray.None(), + [RayType.TERMINAL]: () => { throw new NotImplementedError(); }, - }), + }), }), @@ -620,6 +623,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? return (ref: Ray) => method(ref)(step(ref)); } + /** * Helper methods for commonly used directions * @@ -633,7 +637,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? /** * .next */ - get next() { return Ray.___next(Ray.directions.next)(this); } + next = () => { return Ray.___next(Ray.directions.next)(this); } has_next = (step: Implementation = Ray.directions.next): boolean => step(this).is_none(); // @alias('end', 'result') last = (step: Implementation = Ray.directions.next): Ray => { @@ -643,7 +647,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? /** * .previous */ - get previous(): Ray { return Ray.___next(Ray.directions.previous)(this); } + previous = (): Ray => { return Ray.___next(Ray.directions.previous)(this); } has_previous = (step: Implementation = Ray.directions.previous): boolean => this.has_next(step); first = (step: Implementation = Ray.directions.previous): Ray => this.last(step); @@ -733,7 +737,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? // ___compute = () - *traverse(step: ((ref: Ray) => Ray) = ((ref): Ray => ref.next)): Generator { + *traverse(step: Implementation = Ray.directions.next): Generator { // TODO: Also to ___func?? if (this.type !== RayType.VERTEX) @@ -745,7 +749,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ?? yield current; try { - current = current.next; + current = current.next(); } catch (e) { // console.error('stopped traversal through implementation error...', e) break; // TODO: HACKY FOR NOW