Skip to content

Commit

Permalink
2024/01/16 - Iterating on some ideas to implement 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
FadiShawki committed Jan 16, 2024
1 parent e9e52ba commit 6e18ea2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
45 changes: 24 additions & 21 deletions src/@orbitmines/explorer/Ray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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 --|
Expand Down Expand Up @@ -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);
//
// /**
// * | |
Expand Down Expand Up @@ -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');
Expand Down
44 changes: 24 additions & 20 deletions src/@orbitmines/explorer/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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(); },

}),
}),

}),

Expand All @@ -620,6 +623,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ??
return (ref: Ray) => method(ref)(step(ref));
}


/**
* Helper methods for commonly used directions
*
Expand All @@ -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 => {
Expand All @@ -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);

Expand Down Expand Up @@ -733,7 +737,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ??

// ___compute = ()

*traverse(step: ((ref: Ray) => Ray) = ((ref): Ray => ref.next)): Generator<Ray> {
*traverse(step: Implementation = Ray.directions.next): Generator<Ray> {
// TODO: Also to ___func??

if (this.type !== RayType.VERTEX)
Expand All @@ -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
Expand Down

0 comments on commit 6e18ea2

Please sign in to comment.