2017年5月13日土曜日

開発環境

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: Advanced Sorting Techniques.を JavaScript で取り組んでみる。

Exercise: Other Ways to Modify an Array.

コード(Emacs)

HTML5

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

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_n = document.querySelector('#n0'),
    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 myComp = (a, b) =>
    [a, b].map((s) => s.match(/^\d+(\w+)/)[1])
    .reduce((x, y) => x < y ? -1 : x === y ? 0 : 1);

let output = () => {
    let input = ['22ac', '34bd', '56aa3', '12c;', '4abc(', '1ca', '45bc'];

    p(input);
    p(input.sort(myComp));
    p(input);
};

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

output();










 
						

0 コメント:

コメントを投稿

Comments on Google+: