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 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Member taro = new Member("太郎", 31); Member hanako = new Member("花子"); System.out.println("taro : " + taro); System.out.println("hanako : " + hanako); } } class Member { private String name; private Optional<Integer> age; public Member(String name) { this.name = name; this.age = Optional.ofNullable(null); } public Member(String name, int age) { this.name = name; this.age = Optional.ofNullable(age); } public String getProfile() { return this.age .map(a -> String.format("%s(%d)", this.name, a)) .orElse(this.name); } @Override public String toString() { return this.getProfile(); } } |
1 2 |
taro : 太郎(31) hanako : 花子 |
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 |