「JavaScript/Set」の版間の差分

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
Fix Cite
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
→‎Polyfill: cleanup.
タグ: 2017年版ソースエディター
74 行
;[https://paiza.io/projects/Zx6wmzOUm7IMcltSD14Cug?language=javascript Polyfill]:<syntaxhighlight lang="javascript">
Object.entries({
filter: function (callback, thisArg) {
if ( !typeof callback =!== 'function' )
throw new TypeError();
 
const result = new Set()
if (thisArg === 'undefined')
thisArg = this
for (const x of this)
if (callback.call(thisArg, x))
result.add(x)
return result },
},
map: function (callback, thisArg) {
if ( !typeof callback =!== 'function' )
throw new TypeError();
 
const result = new Set()
if (thisArg === 'undefined')
thisArg = this
for (const x of this)
result.add(callback.call(thisArg, x))
return result },
},
reduce: function (callback, initial) {
if ( !typeof callback =!== 'function' )
throw new TypeError();
let result = initial
for (const x of this) {
if (result === 'undefined')
result = x
else
result = callback(result, x, this)
}
return result },
},
union: function (other) {
const result = new Set(this)
for (const x of other) {
result.add(x)
}
return result },
},
intersection: function (other) {
return this.filter(e => (other.has(e))) },
},
difference: function (other) {
return this.filter(e => (!other.has(e))) },
},
superset_p: function (other) {
for (letconst elem of other) {
if (!this.has(elem)) {
return false
}
}
return true },
},
subset_p: function (other) {
return other.superset_p(this) },
},
}).forEach(pair => {
Object.defineProperty(Set.prototype, pair[0], {
131 ⟶ 135行目:
})
 
let a = new Set([0, 1, 2]),
b = new Set([2, 3, 4]),
c = new Set([0, 1, 2, 3, 4, 5, 6])
console.log("a = ", a, ", b = ", b),
console.log(a.filter(x => x % 2))
//console.log(a.filter(x => x % 2, b1))
console.log(a.map(x => x * 2))
console.log(a.union(b))
console.log(a.intersection(b))
146 ⟶ 151行目:
a = Set(3) { 0, 1, 2 } , b = Set(3) { 2, 3, 4 }
Set(1) { 1 }
Set(13) { 10, 2, 4 }
Set(5) { 0, 1, 2, 3, 4 }
Set(1) { 2 }