開発環境
- macOS Mojave - Apple (OS)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- TypeScript (プログラミング言語)
- Node.js(JavaScriptの実行環境)
Programming TypeScript: Making Your JavaScript Applications Scale (Boris Cherny(著)、O'Reilly Media)のChapter 4(Functions)、Exercises(82)5の解答を求めてみる。
コード
TypeScript
type Is = <T>(a:T, b:T) => boolean
let is:Is = (a, b) => a === b
function is1<T>(a:T, b:T) {
return a == b
}
let pairs = [['string', 'otherstring'],
[true, false],
[42, 42]]
pairs.forEach(([a, b]) => console.log(a, b, is(a, b), is1(a, b)))
console.log('[Hard]')
let is2 = <T>(...args:T[]): boolean => {
let len = args.length
if (len === 0) {
return true
}
let first = args[0]
for (let index = 1; index < len; index++) {
if (args[index] !== first) {
return false
}
}
return true
}
console.log(is2([1], [1,2], [1,2,3]))
console.log(is2(1, 1, 1, 1, 1))
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ ts-node sample5.ts string otherstring false false true false false false 42 42 true true [Hard] false true $
0 コメント:
コメントを投稿