2017年5月14日日曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 9(Arrays and Lists)の Exercise 9-1、2、3、4、5、6、7、8、9.を JavaScript で取り組んでみる。

Exercise 9-1、2、3、4、5、6、7、8、9.

words.txt

コード(Emacs)

HTML5

<pre id="output0"></pre>
<input id="file0" type="file">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_file0 = document.querySelector('#file0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];

        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };


let nestedSum = (aryOfAry) =>
    aryOfAry
    .map((ary) => ary.reduce((x, y) => x + y))
    .reduce((x, y) => x + y);

let cumulSum = (nums) => nums.map((x, i) => x + i);

let middle = (ary) => ary.slice(1, ary.length - 1);

let cropIt = (ary) => {
    ary.shift();
    ary.pop();
}

let isSorted = (ary) => {
    let ary0 = ary.slice();
    ary0.sort();
    
    return ary0.every((x, i) => x === ary[i]);
};

let isAnagram = (word1, word2) => {
    if (word1.length !== word2.length) {
        return false;
    }

    let a1 = word1.split(''),
        a2 = word2.split('');
    a1.sort();
    a2.sort();
    return a1.every((c, i) => c === a2[i]);
};

let hasDuplicates = (ary) => {
    let ary0 = ary.slice();

    ary0.sort();

    return ary0.slice(1).some((x, i) => x === ary0[i]);
};


let output = () => {
    p('1.');
    let aoa = [[1, 2], [3], [4, 5, 6]];
    p(nestedSum(aoa));

    p('2.');
    let nums = [1, 2, 3, 4];
    p(cumulSum(nums));

    p('3.');
    p(middle([1, 2, 3, 4]));

    p('4.');
    p(nums);
    p(cropIt(nums));
    p(nums);

    p('5.');
    p(isSorted([1, 2, 2]));
    p(isSorted([1, 2, 1]));

    p('6.');
    p(isAnagram('perl 6', '6p elr'));
    p(isAnagram('perl 6', '6pelr'));
    p(isAnagram('perl 6', '6pelra'));

    p('7.');
    p(hasDuplicates([1, 2, 3, 4, 5]));
    p(hasDuplicates([1, 2, 1, 4, 5]));

    p('8.');
    let total = 10000;
    let p0 = range(0, total)
        .map(() =>
             hasDuplicates(
                 range(0, 23).map(() => Math.floor(Math.random() * 365))
             ))
        .filter((x) => x)
        .length / total;
    p(`${p0 * 100}%`);

    let reader = new FileReader(),
        file = input_file0.files[0];
    
    reader.onload = () => {
        let words = reader.result.split('\n').filter((word) => word !== '');
        p('9.');
        range(0, 5).forEach(() => {
            let words1 = [],
                words2 = [];

            let t = new Date().getTime();
            words.forEach((word) => {
                words1.push(word);
            });
            p(`push:    ${new Date().getTime() - t}ms`);

            t = new Date().getTime();
            words.forEach((word) => {
                words2.unshift(word);
            });
            p(`unshift: ${new Date().getTime() - t}ms`);
        });
    };
    reader.readAsText(file);
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();



0 コメント:

コメントを投稿