Skip to content

Instantly share code, notes, and snippets.

@cs3b
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save cs3b/0f45545913ebeedf1de0 to your computer and use it in GitHub Desktop.

Select an option

Save cs3b/0f45545913ebeedf1de0 to your computer and use it in GitHub Desktop.
Eloquent JavaScript (chapter 4 # Excersise)
arrayToList = (elements) ->
{ value: elements.shift(), rest: if elements.length == 0 then null else arrayToList(elements) }
listToArray = (list) ->
if list.rest is null then [list.value] else [list.value].concat(listToArray(list.rest))
nth = (list, element_index, index) ->
index = 0 if index is undefined
if list is null
null
else if index == element_index
list.value
else
nth(list.rest, element_index, index + 1)
# alternative
nth = (list, index) ->
if list is null
undefined
else if index == 0
list.value
else
nth(list.rest, index - 1)
prepend = (element, list) ->
{ value: element, rest: list }
function arrayToList(elements) {
return {
value: elements.shift(),
rest: elements.length == 0 ? null : arrayToList(elements)
}
}
function listToArray(list) {
function addToArray(array, current_list) {
array.push(current_list.value);
if (current_list.rest != null)
addToArray(array, current_list.rest);
}
var values = [];
addToArray(values, list);
return values;
}
function prepend(element, list) {
return {
value: element,
rest: list
}
}
function nth(list, element_index) {
function searchInList(current_index, element_index, current_list) {
if (current_index == element_index)
return current_list.value;
else if (current_list.rest != null)
return searchInList(current_index + 1, element_index, current_list.rest);
else
return null;
}
return searchInList(0, element_index, list);
}
console.log(arrayToList([10, 20]));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(listToArray(arrayToList([10, 20, 30])));
// → [10, 20, 30]
console.log(prepend(10, prepend(20, null)));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(nth(arrayToList([10, 20, 30]), 1));
// → 20
deepEqual = (a, b) ->
if a == b
true
else if a isnt null && b isnt null && Object.keys(a).length == Object.keys(b).length
for property of a
return(false) unless deepEqual(a[property], b[property])
true
else
false
function deepEqual(a, b) {
if (a === b)
return true;
else if (typeof(a) === "object") {
for (property in a) {
if ((b != undefined) && deepEqual(a[property], b[property]))
continue;
else
return false;
}
return true;
}
}
var obj = {here: {is: "an", a: {c: 5}}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an", a: {c: 5}}, object: 2}));
// → true
reverseArray = (list) ->
list.reduceRight(((list, element) -> list.concat(element)),[])
reverseArrayInPlace = (list) ->
swap = (b_index) ->
tmp_value = list[a_index]
list[a_index] = list[b_index]
list[b_index] = tmp_value
for a_index in [0..Math.floor(list.length / 2)]
swap list.length - (a_index + 1)
list
function reverseArray(list) {
var result = [];
while (list.length > 0)
result.push(list.pop());
return result;
}
function reverseArrayInPlace(list) {
function swap(a_index, b_index) {
var tmp_value = list[a_index];
list[i] = list[b_index];
list[b_index] = tmp_value;
}
var half_size_of_array = Math.floor(list.length / 2);
for (var i = 0; i < half_size_of_array; i++) {
swap(i, (list.length - (i + 1)));
}
return list;
}
console.log(reverseArray(["A", "B", "C", "E"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
range = (start, end, step) ->
step = (if start < end then 1 else -1) if step is undefined
number for number in [start..end] by step
add = (a, b) ->
a + b
sum = (list) ->
list.reduce(add)
function range(a, b, step) {
var result = [];
var condition = (a < b) ? function () {
return a <= b
} : function () {
return b <= a
};
var increment = step ? function () {
return a += step
} : function () {
return a++
};
for (a; condition(); increment()) {
result.push(a);
}
return result;
}
function sum(list) {
var result = 0;
for (var i = 0; i < list.length; i++) {
result += list[i];
}
return result;
}
console.log(sum(range(1, 10)));
// → 55
console.log(range(10, -5, -3));
// → [5, 4, 3, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment