JavaScriptのコールバック関数を引数付きで実行する方法は、無名関数に包むこと。
1 2 3 4 5 6 7 8 9 10 |
const p = console.log; const disp = arg => { p(arg); }; setTimeout(() => { const str = 'delay : ' + 13; disp(str); }, 13); |
1 |
delay : 13 |
JavaScriptのコールバック関数を引数付きで実行する方法は、無名関数に包むこと。
1 2 3 4 5 6 7 8 9 10 |
const p = console.log; const disp = arg => { p(arg); }; setTimeout(() => { const str = 'delay : ' + 13; disp(str); }, 13); |
1 |
delay : 13 |
(data.csv)
1 2 3 4 5 6 7 8 9 10 11 |
プログラミング言語,リクナビNext,DODA,パイザ,キャリコネ,Indeed HTML,117,256,283,1642,7643 JavaScript,104,380,889,2362,7093 PHP,248,722,866,3605,9319 Java,484,1729,990,5372,15893 Python,87,301,255,1606,3378 C++,1172,701,393,998,5897 C#,1172,669,552,882,6412 Swift,34,92,278,805,1129 Ruby,83,286,464,2200,3919 R,5,8,26,22,313 |
(Main.js)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
const main = () => { const p = console.log const fs = require('fs') class Agent { constructor(name, html, js, php, java, python, cp, cs, swift, ruby, r) { this.name = name this.html = Number(html) this.js = Number(js) this.php = Number(php) this.java = Number(java) this.python = Number(python) this.cp = Number(cp) this.cs = Number(cs) this.swift = Number(swift) this.ruby = Number(ruby) this.r = Number(r) this.updateShare() } calcSum() { return this.html + this.js + this.php + this.java + this.python + this.cp + this.cs + this.swift + this.ruby + this.r } updateShare() { const sum = this.calcSum() this.htmlShare = 100 * this.html / sum this.jsShare = 100 * this.js / sum this.phpShare = 100 * this.php / sum this.javaShare = 100 * this.java / sum this.pythonShare = 100 * this.python / sum this.cpShare = 100 * this.cp / sum this.csShare = 100 * this.cs / sum this.swiftShare = 100 * this.swift / sum this.rubyShare = 100 * this.ruby / sum this.rShare = 100 * this.r / sum } } const convertDataTo2dArray = data => { const a = [] data.split('\n').forEach(line => { a.push(line.split(',')) }) return a } const convert2dArrayToAgents = a => { const agents = {} agents.rnn = new Agent(a[0][1], a[1][1], a[2][1], a[3][1], a[4][1], a[5][1], a[6][1], a[7][1], a[8][1], a[9][1], a[10][1]) agents.doda = new Agent(a[0][2], a[1][2], a[2][2], a[3][2], a[4][2], a[5][2], a[6][2], a[7][2], a[8][2], a[9][2], a[10][2]) agents.paiza = new Agent(a[0][3], a[1][3], a[2][3], a[3][3], a[4][3], a[5][3], a[6][3], a[7][3], a[8][3], a[9][3], a[10][3]) agents.cc = new Agent(a[0][4], a[1][4], a[2][4], a[3][4], a[4][4], a[5][4], a[6][4], a[7][4], a[8][4], a[9][4], a[10][4]) agents.indeed = new Agent(a[0][5], a[1][5], a[2][5], a[3][5], a[4][5], a[5][5], a[6][5], a[7][5], a[8][5], a[9][5], a[10][5]) return agents } const calcHtmlShareAverage = agents => (agents.rnn.htmlShare + agents.doda.htmlShare + agents.paiza.htmlShare + agents.cc.htmlShare + agents.indeed.htmlShare) / 5 const calcJsShareAverage = agents => (agents.rnn.jsShare + agents.doda.jsShare + agents.paiza.jsShare + agents.cc.jsShare + agents.indeed.jsShare) / 5 const calcPhpShareAverage = agents => (agents.rnn.phpShare + agents.doda.phpShare + agents.paiza.phpShare + agents.cc.phpShare + agents.indeed.phpShare) / 5 const calcJavaShareAverage = agents => (agents.rnn.javaShare + agents.doda.javaShare + agents.paiza.javaShare + agents.cc.javaShare + agents.indeed.javaShare) / 5 const calcPythonShareAverage = agents => (agents.rnn.pythonShare + agents.doda.pythonShare + agents.paiza.pythonShare + agents.cc.pythonShare + agents.indeed.pythonShare) / 5 const calcCpShareAverage = agents => (agents.rnn.cpShare + agents.doda.cpShare + agents.paiza.cpShare + agents.cc.cpShare + agents.indeed.cpShare) / 5 const calcCsShareAverage = agents => (agents.rnn.csShare + agents.doda.csShare + agents.paiza.csShare + agents.cc.csShare + agents.indeed.csShare) / 5 const calcSwiftShareAverage = agents => (agents.rnn.swiftShare + agents.doda.swiftShare + agents.paiza.swiftShare + agents.cc.swiftShare + agents.indeed.swiftShare) / 5 const calcRubyShareAverage = agents => (agents.rnn.rubyShare + agents.doda.rubyShare + agents.paiza.rubyShare + agents.cc.rubyShare + agents.indeed.rubyShare) / 5 const calcRShareAverage = agents => (agents.rnn.rShare + agents.doda.rShare + agents.paiza.rShare + agents.cc.rShare + agents.indeed.rShare) / 5 const sub = () => { const d = fs.readFileSync("data.csv", {encoding: "utf-8"}) const a = convertDataTo2dArray(d) const o = convert2dArrayToAgents(a) p('HtmlShareAverage : ', calcHtmlShareAverage (o)) p('JsShareAverage : ', calcJsShareAverage (o)) p('PhpShareAverage : ', calcPhpShareAverage (o)) p('JavaShareAverage : ', calcJavaShareAverage (o)) p('PythonShareAverage : ', calcPythonShareAverage(o)) p('CpShareAverage : ', calcCpShareAverage (o)) p('CsShareAverage : ', calcCsShareAverage (o)) p('SwiftShareAverage : ', calcSwiftShareAverage (o)) p('RubyShareAverage : ', calcRubyShareAverage (o)) p('RShareAverage : ', calcRShareAverage (o)) } sub() } main() |
1 2 3 4 5 6 7 8 9 10 |
HtmlShareAverage : 6.986354843278131 JsShareAverage : 10.378601229059685 PhpShareAverage : 14.44282913557401 JavaShareAverage : 24.16914753977421 PythonShareAverage : 5.442704295588216 CpShareAverage : 13.941919944259928 CsShareAverage : 14.503864909656471 SwiftShareAverage : 2.860625180852734 RubyShareAverage : 6.985042195338946 RShareAverage : 0.28891072661766853 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
'use strict' const main = () => { const p = console.log const getRandomInt = (min = 0, max = 9) => { const offset = Math.floor(Math.random() * (max - min + 1)) return min + offset } const getBlankArray = (size = 10) => { const a = [] for (let i = 0; i < size; i++) { a[i] = 0 } return a } const dispArrayList = a => { const len = a.length for (let i = 0; i < len; i++) { p(i + " => " + a[i]) } } const setRandomForArray = (a, min = 0, max = 9) => { const len = a.length for (let i = 0; i < len; i++) { a[i] = getRandomInt(min, max) } } const getPivot = (a, b, c) => { if (a < b) { if (b < c) { return b } else { return a < c ? c : a } } else { if (a < c) { return a } else { return b < c ? b : c } } } const swap = (a, left, right) => { const t = a[left] a[left] = a[right] a[right] = t } const quicksort = (a, left = 0, right = -1) => { if (right === -1) { right = a.length - 1 } if (right <= left) { return } let l = left let r = right const p = getPivot(a[l], a[l + 1], a[r]) while (true) { while (a[l] < p) { l++ } while (p < a[r]) { r-- } if (r <= l) { break } swap(a, l, r) l++ r-- } quicksort(a, left, l - 1) quicksort(a, r + 1, right) } const bubblesort = a => { const len = a.length for (let i = 0; i < len - 1; i++) { for (let j = i + 1; j < len; j++) { if (a[j] < a[i]) { swap(a, i, j) } } } } const getCloneArray = a => { const len = a.length const b = [] for (let i = 0; i < len; i++) { b[i] = a[i] } return b } const sub = () => { const a = getBlankArray(20) setRandomForArray(a) const b = getCloneArray(a) p("a : " + a) p("b : " + b) quicksort(a) bubblesort(b) p("a : " + a) p("b : " + b) } sub() } main() |
1 2 3 4 |
a : 3,1,7,4,6,2,4,2,3,9,7,1,8,0,4,7,4,2,3,8 b : 3,1,7,4,6,2,4,2,3,9,7,1,8,0,4,7,4,2,3,8 a : 0,1,1,2,2,2,3,3,3,4,4,4,4,6,7,7,7,8,8,9 b : 0,1,1,2,2,2,3,3,3,4,4,4,4,6,7,7,7,8,8,9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
const main = () => { const p = console.log const getRandomInt = (min = 0, max = 9) => { const randomOffset = Math.floor(Math.random() * (max - min + 1)) return min + randomOffset } const dispArrayList = a => { a.forEach((v, k) => { p(k + " => " + v) }) } const getBlankArray = (len = 10) => { const a = [] for (let i = 0; i < len; i++) { a[i] = 0 } return a } const setRandomIntForArray = (a, min = 0, max = 9) => { const len = a.length for (let i = 0; i < len; i++) { a[i] = getRandomInt(min, max) } } const getPivot = (a, b, c) => { if (a < b) { if (b < c) { return b } else { return a < c ? c : a } } else { if (a < c) { return a } else { return b < c ? c : b } } } const swap = (a, l, r) => { const t = a[l] a[l] = a[r] a[r] = t } const quicksort = (a, left = 0, right = -1) => { if (right === -1) { right = a.length - 1 } if (right <= left) { return } let l = left let r = right const p = getPivot(a[l], a[l + 1], a[r]) while (true) { while (a[l] < p) { l++ } while (p < a[r]) { r-- } if (r <= l) { break; } swap(a, l, r) l++ r-- } quicksort(a, left, l - 1) quicksort(a, r + 1, right) } const sub = () => { const a = getBlankArray(30) setRandomIntForArray(a) p("ソート前", a) quicksort(a) p("ソート後", a) } sub() } main() |
1 2 |
ソート前 [ 8, 2, 7, 5, 4, 8, 0, 1, 6, 7, 7, 3, 8, 0, 7, 9, 5, 0, 4, 4, 1, 1, 3, 5, 1, 8, 9, 1, 0, 8 ] ソート後 [ 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9 ] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
const fs = require('fs') const getFilePathListSortedByTimeStampAsc = dir => { const fileList = [] files = fs.readdirSync(dir) for (const file of files) { const filePath = dir + '/' + file const mtime = fs.statSync(filePath).mtime fileList.push({ filePath: filePath, mtime: mtime }) } return fileList .sort((a, b) => a.mtime - b.mtime) // ASC .map(file => file.filePath) } |
従来のforより短くかけることを認めざるを得ない
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
'use strict' /* 引数の配列から偶数の要素に絞った新たな配列を得る */ const p = console.log const a = [-2, -1, 0, 1, 2, 3, 4, 5] p(a) /* for */ const getOnlyEvenArrayByFor = a => { const b = [] let i = 0; for (const n of a) { if (n % 2 == 0) { b[i++] = n } } return b } /* filter */ const getOnlyEvenArrayByFilter = a => { return a.filter(n => n % 2 == 0) } const getOnlyEvenArrayByFilterOneLiner = a => a.filter(n => n % 2 == 0) p(getOnlyEvenArrayByFor(a)) p(getOnlyEvenArrayByFilter(a)) p(getOnlyEvenArrayByFilterOneLiner(a)) |
1 2 3 4 |
[ -2, -1, 0, 1, 2, 3, 4, 5 ] [ -2, 0, 2, 4 ] [ -2, 0, 2, 4 ] [ -2, 0, 2, 4 ] |
JavaScriptのPromise配列とPromise.all()の練習
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
const main = () => { const p = console.log const getTimeoutPromise = (func, arg, time_ms) => { return new Promise((resolve, reject) => { setTimeout(() => { func(arg) resolve(arg) }, time_ms) }) } const execPromises_getTimeLapse = (promises, funcForResults) => { const startTime_ms = new Date() Promise.all(promises).then(results => { funcForResults(results) const endTime_ms = new Date() const timelapse_ms = endTime_ms - startTime_ms p("timelapse_ms : " + timelapse_ms) }); } const sub = () => { const promises = [] promises.push(getTimeoutPromise(p, "timeout1", 7)) promises.push(getTimeoutPromise(p, "timeout2", 0)) promises.push(getTimeoutPromise(p, "timeout3", 54)) promises.push(getTimeoutPromise(p, "timeout4", 32)) promises.push(getTimeoutPromise(p, "timeout5", 2)) execPromises_getTimeLapse(promises, p) } sub() } main() |
1 2 3 4 5 6 7 |
timeout2 timeout5 timeout1 timeout4 timeout3 [ 'timeout1', 'timeout2', 'timeout3', 'timeout4', 'timeout5' ] timelapse_ms : 56 |
JavaScriptのオブジェクトの比較は不思議である
1 2 3 4 5 6 7 8 |
const p = console.log p("{} == {} : " + ( {} == {} ) ) p("{} === {} : " + ( {} === {} ) ) p("{} < {} : " + ( {} < {} ) ) p("{} > {} : " + ( {} > {} ) ) p("{} <= {} : " + ( {} <= {} ) ) p("{} >= {} : " + ( {} >= {} ) ) |
1 2 3 4 5 6 |
{} == {} : false {} === {} : false {} < {} : false {} > {} : false {} <= {} : true {} >= {} : true |
リアルタイムチャット(ajax)
最大公約数を求める
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
const main = () => { 'use strict' const p = console.log const calcGreatestCommonFactor = (arg1, arg2) => { let a = Number(arg1) let b = Number(arg2) /* 絶対値にする */ if (a < 0) { a *= -1 } if (b < 0) { b *= -1 } /* 同じ値ならそのまま返却する */ if (a === b) { return a } /* 片方が0ならもう片方の値が最大公約数になる */ if (a === 0) { return b } if (b === 0) { return a } /* aの方を大きくする */ if (a < b) { const c = a a = b b = c } // 余り(初期値は引数の小さい方) let rem = b /* 最大公約数を求めるループ */ while (true) { rem = a % b /* 割り切れたら、その時割った値が最大公約数 */ if (rem === 0) { return b } a = b b = rem } } const spacePadding = (n, keta = 4) => { const strN = '' + n const nKeta = strN.length if (keta < nKeta) { return strN } let pad = '' for (let i = 0; i < keta; i++) { pad += ' ' } return (pad + strN).slice(-keta) } const dispGreatestCommonFactor = (a, b) => { let gcf = calcGreatestCommonFactor(a, b) a = spacePadding(a) b = spacePadding(b) gcf = spacePadding(gcf) p(`${a} と ${b} の最大公約数は ${gcf}`) } /* 最大公約数を求めて出力する */ dispGreatestCommonFactor(4997, 7890); dispGreatestCommonFactor(147, 105); dispGreatestCommonFactor(0, 19); } main() |
1 2 3 |
4997 と 7890 の最大公約数は 263 147 と 105 の最大公約数は 21 0 と 19 の最大公約数は 19 |