michael@0: // A set iterator can cope with removing the current entry. michael@0: michael@0: function test(letters, toRemove) { michael@0: var set = Set(letters); michael@0: toRemove = Set(toRemove); michael@0: michael@0: var leftovers = [x for (x of set) if (!toRemove.has(x))].join(""); michael@0: michael@0: var log = ""; michael@0: for (let x of set) { michael@0: log += x; michael@0: if (toRemove.has(x)) michael@0: set.delete(x); michael@0: } michael@0: assertEq(log, letters); michael@0: michael@0: var remaining = [x for (x of set)].join(""); michael@0: assertEq(remaining, leftovers); michael@0: } michael@0: michael@0: test('a', 'a'); // removing the only entry michael@0: test('abc', 'a'); // removing the first entry michael@0: test('abc', 'b'); // removing a middle entry michael@0: test('abc', 'c'); // removing the last entry michael@0: test('abc', 'abc') // removing all entries michael@0: