Skip to content

Commit

Permalink
Pair methods for working with ALists + Pair::reduce + throw in cdr/car
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Mar 3, 2018
1 parent 65bb8bc commit 26a8ac2
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 26 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* ignore comments everything after ; but not inside strings and regexes
* gensym and load functions
* better string function

* Pair methods for working with ALists + Pair::reduce
* throw exception on car/cdr with non list

### Bugs
* fix parsing empty strings
Expand Down
84 changes: 72 additions & 12 deletions dist/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2018 Jakub Jankiewicz <http://jcubic.pl/me>
* Released under the MIT license
*
* build: Thu, 01 Mar 2018 19:16:43 +0000
* build: Sat, 03 Mar 2018 22:46:52 +0000
*/
/*
* TODO: Pair.prototype.toObject = alist to Object
Expand Down Expand Up @@ -119,7 +119,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
} else if (token === ')') {
parents--;
if (!stack.length) {
throw new Error('Unbalanced parenthesis 1');
throw new Error('Unbalanced parenthesis');
}
if (stack.length === 1) {
result.push(stack.pop());
Expand Down Expand Up @@ -174,7 +174,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
}
});
if (stack.length) {
throw new Error('Unbalanced parenthesis 2');
throw new Error('Unbalanced parenthesis');
}
return result.map(function (arg) {
if (arg instanceof Array) {
Expand All @@ -196,6 +196,15 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
//return '<#symbol \'' + this.name + '\'>';
return this.name;
};

// ----------------------------------------------------------------------
// :: Nil constructor with only once instance
// ----------------------------------------------------------------------
function Nil() {}
Nil.prototype.toString = function () {
return 'nil';
};
var nil = new Nil();
// ----------------------------------------------------------------------
// :: Pair constructor
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -262,6 +271,59 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
}
}
};
Pair.prototype.toObject = function () {
var node = this;
var result = {};
while (true) {
if (node instanceof Pair && node.car instanceof Pair) {
var pair = node.car;
var name = pair.car;
if (name instanceof _Symbol) {
name = name.name;
}
result[name] = pair.cdr;
node = node.cdr;
} else {
break;
}
}
return result;
};
Pair.fromPairs = function (array) {
return array.reduce(function (list, pair) {
return new Pair(new Pair(new _Symbol(pair[0]), pair[1]), list);
}, nil);
};
Pair.fromObject = function (obj) {
var array = Object.keys(obj).map(function (key) {
return [key, obj[key]];
});
return Pair.fromPairs(array);
};
Pair.prototype.reduce = function (fn) {
var node = this;
var result = nil;
while (true) {
if (node !== nil) {
result = fn(result, node.car);
node = node.cdr;
} else {
break;
}
}
return result;
};
Pair.prototype.reverse = function () {
var node = this;
var prev = nil;
while (node !== nil) {
var next = node.cdr;
node.cdr = prev;
prev = node;
node = next;
}
return prev;
};
Pair.prototype.transform = function (fn) {
var visited = [];
function recur(pair) {
Expand Down Expand Up @@ -322,15 +384,6 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
return this;
};

// ----------------------------------------------------------------------
// :: Nil constructor with only once instance
// ----------------------------------------------------------------------
function Nil() {}
Nil.prototype.toString = function () {
return 'nil';
};
var nil = new Nil();

// ----------------------------------------------------------------------
// :: Macro constructor
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -451,11 +504,15 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
car: function car(list) {
if (list instanceof Pair) {
return list.car;
} else {
throw new Error('argument to car need to be a list');
}
},
cdr: function cdr(list) {
if (list instanceof Pair) {
return list.cdr;
} else {
throw new Error('argument to cdr need to be a list');
}
},
'set-car': function setCar(slot, value) {
Expand Down Expand Up @@ -723,6 +780,9 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
if (typeof obj === 'function') {
return '<#function>';
}
if (obj === nil) {
return 'nil';
}
if (obj instanceof Array || obj === null) {
return JSON.stringify(obj);
}
Expand Down

0 comments on commit 26a8ac2

Please sign in to comment.