2017年5月10日水曜日

開発環境

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 8(Arrays and Lists)の Exercise: Implementing a Queue.を JavaScript で取り組んでみる。

Exercise: Implementing a Queue.

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    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 putInQueue = (queue, item) => queue.unshift(item),
    takeFromQueue = (queue) => queue.pop();

let output = () => {
    let queue = [1, 2, 3, 4, 5];

    putInQueue(queue, 0);
    p(queue);

    range(0, 3).forEach((i) => {
        p(takeFromQueue(queue));
    });
    p(queue);
};

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

output();










 
						

0 コメント:

コメントを投稿

Comments on Google+: