Skip to content

Commit

Permalink
♻️ refactor: Improve and simplify from, push, and unshift.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Nov 13, 2022
1 parent 0d49ca5 commit fe099e8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
13 changes: 8 additions & 5 deletions src/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
*
* @class
* @param {any} value The value to hold.
* @param {Node|null} prev The value to hold.
* @param {Node|null} next The value to hold.
*/
export default function Node(value) {
// eslint-disable-next-line unicorn/prevent-abbreviations
export default function Node(value, prev, next) {
/** @member {any} The value/key held by this node. */
this.value = value;
/** @member {Node} Pointer to previous (left) sibling */
this.prev = null;
/** @member {Node} Pointer to next (right) sibling */
this.next = null;
/** @member {Node|null} Pointer to previous (left) sibling */
this.prev = prev;
/** @member {Node|null} Pointer to next (right) sibling */
this.next = next;
}
18 changes: 18 additions & 0 deletions src/_push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert';
import Node from './Node.js';

/**
* Push value to list.
*
* @param {Node} z Last node of first input list (can be null).
* @param {any} value Value to push.
* @return {Node} The node at the front of the list (new node if empty, input
* node otherwise).
*/
export default function _push(z, value) {
assert(z instanceof Node);
assert(z.next === null);
const y = new Node(value, z, null);
z.next = y;
return y;
}
17 changes: 17 additions & 0 deletions src/_unshift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from 'assert';
import Node from './Node.js';

/**
* Unshift value to list.
*
* @param {Node} x First node of first input list (can be null).
* @param {any} value Value to unshift.
* @return {Node} The node at the front of the list (hence, the new node).
*/
export default function _unshift(x, value) {
assert(x instanceof Node);
assert(x.prev === null);
const y = new Node(value, null, x);
x.prev = y;
return y;
}
8 changes: 3 additions & 5 deletions src/from.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Node from './Node.js';
import _concat from './_concat.js';
import _push from './_push.js';

/**
* Creates a list from an input iterable.
Expand All @@ -13,13 +13,11 @@ export default function from(iterable) {

if (event.done) return null;

const first = new Node(event.value);
const first = new Node(event.value, null, null);
let last = first;

for (const value of it) {
const next = new Node(value);
_concat(last, next);
last = next;
last = _push(last, value);
}

return first;
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {default as _iter_fast} from './_iter_fast.js';
export {default as _last} from './_last.js';
export {default as _len} from './_len.js';
export {default as _pop} from './_pop.js';
export {default as _push} from './_push.js';
export {default as _remove} from './_remove.js';
export {default as _rotate_left} from './_rotate_left.js';
export {default as _rotate_left_modulo} from './_rotate_left_modulo.js';
Expand All @@ -15,6 +16,7 @@ export {default as _rotate_right_unknown_length} from './_rotate_right_unknown_l
export {default as _rotate_to} from './_rotate_to.js';
export {default as _shift} from './_shift.js';
export {default as _split} from './_split.js';
export {default as _unshift} from './_unshift.js';
export {default as concat} from './concat.js';
export {default as empty} from './empty.js';
export {default as from} from './from.js';
Expand Down
9 changes: 3 additions & 6 deletions src/push.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import Node from './Node.js';
import _concat from './_concat.js';
import _push from './_push.js';

/**
* Push value to list.
Expand All @@ -12,11 +12,8 @@ import _concat from './_concat.js';
* node otherwise).
*/
export default function push(x, z, value) {
if (x === null) return new Node(value);
if (x === null) return new Node(value, null, null);
assert(x instanceof Node);
assert(z instanceof Node);
assert(z.next === null);
const y = new Node(value);
_concat(z, y);
_push(z, value);
return x;
}
12 changes: 4 additions & 8 deletions src/unshift.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import assert from 'assert';
import Node from './Node.js';
import _concat from './_concat.js';
import _unshift from './_unshift.js';

/**
* Unshift value to list.
*
* @param {Node} x First node of first input list (can be null).
* @param {Object} value Value to unshift.
* @param {any} value Value to unshift.
* @return {Node} The node at the front of the list (hence, the new node).
*/
export default function unshift(x, value) {
if (x === null) return new Node(value);
assert(x instanceof Node);
const y = new Node(value);
_concat(y, x);
return y;
if (x === null) return new Node(value, null, null);
return _unshift(x, value);
}

0 comments on commit fe099e8

Please sign in to comment.