2016年8月29日月曜日

開発環境

Eloquent JavaScript(Marijn Haverbeke 著、No Starch Press)のPart 1(Language)、Chapter 4(DataStructures: Objects and Arrays)、Exercises(The Sum of Range, Reversing an Array, A List, Deep Comparison)を取り組んでみる。

Exercises

コード(Emacs)

(function () {
    'use strict';
    var ary,
        list,
        list1,
        sum,
        range,
        reverseArray,
        reverseArrayInPlace,
        arrayToList,
        listToArray,
        prepend,
        nth,
        deepEqual;

    sum = function (numbers) {
        var result = 0;

        numbers.forEach(function (num) {
            result += num;
        });
        return result;
    };
    range = function (start, end, step) {
        var i,
            nums = [];

        if (step === undefined) {
            step = 1;
        }
        if (step >= 0) {
            for (i = start; i <= end; i += step) {
                nums.push(i);
            }
        } else {
            for (i = start; i >= end; i += step) {
                nums.push(i);
            }
        }
        return nums;
    };

    console.log(sum(range(1, 10)));
    console.log(range(1, 10, 2));
    console.log(range(5, 2, -1));

    ary = ['a', 'b', 'c', 'd', 'e'];
    reverseArray = function (ary) {
        var len = ary.length,
            a = [],        
            i;
        
        for (i = 0; i < len; i += 1) {
            a[i] = ary[len - 1 - i];
        }
        return a;
    };
    reverseArrayInPlace = function (ary) {
        var i,
            j,
            t;

        for (i = 0, j = ary.length - 1; i < j; i += 1, j -= 1) {
            t = ary[i];
            ary[i] = ary[j];
            ary[j] = t;
        }
    };
    console.log(ary);
    console.log(reverseArray(ary));
    console.log(ary);
    reverseArrayInPlace(ary);
    console.log(ary);

    arrayToList = function (ary) {
        var obj = null,
            i,
            len = ary.length;

        for (i = len - 1; i >= 0; i -= 1) {
            if (obj === null) {
                obj = {value: ary[i], rest: null};
            } else {
                obj = {value: ary[i], rest: obj};
            }
        }
        return obj;
    };
    ary = [1,2,3];
    list = arrayToList([1,2,3]);
    console.log(ary);
    console.log(list);

    listToArray = function (list) {
        var ary = [],
            obj = list;

        if (obj === null) {
            return ary;
        }
        for (;obj.rest !== null; obj = obj.rest) {
            ary.push(obj.value);
        }
        if (obj !== null) {
            ary.push(obj.value);
        }
        return ary;
    };
    ary = listToArray(list);
    console.log(ary);
    console.log(list);

    prepend = function (elem, list) {
        var list0 = {value: elem, rest: list};
        
        return list0;
    };

    nth = function (list, num) {
        if (list === null) {
            return undefined;
        }
        if (num === 0) {
            return list.value;
        }
        return nth(list.rest, num - 1);
    };

    list1 = prepend(10, list);
    console.log(list)
    console.log(list1)

    console.log(nth(list, 0));
    console.log(nth(list, 1));
    console.log(nth(list, 2));
    console.log(nth(list1, 10));

    deepEqual = function (x, y) {
        var props,
            prop,
            i,
            max;
        if (x === null && y === null) {
            return true;
        }
        if (typeof x === 'object' && typeof y === 'object') {
            props = Object.getOwnPropertyNames(x);
            for (i = 0, max = props.length; i < max; i += 1) {
                prop = props[i];
                if (x[prop] !== y[prop]) {
                    return false;
                }
            }
            return true;
        }
        return x === y;
    };

    ary = [1, 2, 'a', 'b', [1,2], list1, deepEqual];

    console.log('deepEqual--------------------------------------------------')
    ary.forEach(function (x) {    
        ary.forEach(function (y) {
            console.log(x);
            console.log(y);
            console.log(deepEqual(x, y));
        });
    });
}());

入出力結果(Terminal, Node.js)

$ node sample.js
55
[ 1, 3, 5, 7, 9 ]
[ 5, 4, 3, 2 ]
[ 'a', 'b', 'c', 'd', 'e' ]
[ 'e', 'd', 'c', 'b', 'a' ]
[ 'a', 'b', 'c', 'd', 'e' ]
[ 'e', 'd', 'c', 'b', 'a' ]
[ 1, 2, 3 ]
{ value: 1, rest: { value: 2, rest: { value: 3, rest: null } } }
[ 1, 2, 3 ]
{ value: 1, rest: { value: 2, rest: { value: 3, rest: null } } }
{ value: 1, rest: { value: 2, rest: { value: 3, rest: null } } }
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
1
2
3
undefined
deepEqual--------------------------------------------------
1
1
true
1
2
false
1
a
false
1
b
false
1
[ 1, 2 ]
false
1
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
1
[Function]
false
2
1
false
2
2
true
2
a
false
2
b
false
2
[ 1, 2 ]
false
2
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
2
[Function]
false
a
1
false
a
2
false
a
a
true
a
b
false
a
[ 1, 2 ]
false
a
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
a
[Function]
false
b
1
false
b
2
false
b
a
false
b
b
true
b
[ 1, 2 ]
false
b
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
b
[Function]
false
[ 1, 2 ]
1
false
[ 1, 2 ]
2
false
[ 1, 2 ]
a
false
[ 1, 2 ]
b
false
[ 1, 2 ]
[ 1, 2 ]
true
[ 1, 2 ]
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
[ 1, 2 ]
[Function]
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
1
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
2
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
a
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
b
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
[ 1, 2 ]
false
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
true
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
[Function]
false
[Function]
1
false
[Function]
2
false
[Function]
a
false
[Function]
b
false
[Function]
[ 1, 2 ]
false
[Function]
{ value: 10,
  rest: { value: 1, rest: { value: 2, rest: [Object] } } }
false
[Function]
[Function]
true
$

0 コメント:

コメントを投稿