dict, lazy_set: Return an iterator from values method.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2020-02-02 23:51:09 -08:00
committed by Tim Abbott
parent 45d3be5449
commit 9e1343ff8a
12 changed files with 33 additions and 29 deletions

View File

@@ -25,7 +25,7 @@ run_test('basic', () => {
assert.equal(d.has('baz'), false);
assert.deepEqual([...d.keys()], ['foo', 'bar']);
assert.deepEqual(d.values(), ['baz', 'qux']);
assert.deepEqual([...d.values()], ['baz', 'qux']);
assert.deepEqual(d.items(), [['foo', 'baz'], ['bar', 'qux']]);
d.delete('bar');

View File

@@ -25,7 +25,7 @@ run_test('basic', () => {
assert.equal(d.has('baz'), false);
assert.deepEqual([...d.keys()], ['foo', 'bar']);
assert.deepEqual(d.values(), ['baz', 'qux']);
assert.deepEqual([...d.values()], ['baz', 'qux']);
assert.deepEqual(d.items(), [['foo', 'baz'], ['bar', 'qux']]);
d.delete('bar');

View File

@@ -25,7 +25,7 @@ run_test('basic', () => {
assert.equal(d.has(999), false);
assert.deepEqual([...d.keys()], [101, 102]);
assert.deepEqual(d.values(), ['baz', 'qux']);
assert.deepEqual([...d.values()], ['baz', 'qux']);
d.delete(102);
assert.equal(d.has(102), false);

View File

@@ -22,8 +22,8 @@ export class Dict<V> {
return this._items.keys();
}
values(): V[] {
return [...this._items.values()];
values(): Iterator<V> {
return this._items.values();
}
items(): [string, V][] {

View File

@@ -44,8 +44,10 @@ export class FoldDict<V> {
}
}
values(): V[] {
return [...this._items.values()].map(({v}) => v);
*values(): Iterator<V> {
for (const {v} of this._items.values()) {
yield v;
}
}
items(): [string, V][] {

View File

@@ -46,8 +46,8 @@ export class IntDict<V> {
return this._map.keys();
}
values(): V[] {
return Array.from(this._map.values());
values(): Iterator<V> {
return this._map.values();
}
filter_values(pred: (item: V) => boolean): V[] {

View File

@@ -718,7 +718,7 @@ exports.filter_all_persons = function (pred) {
};
exports.get_realm_persons = function () {
return active_user_dict.values();
return [...active_user_dict.values()];
};
exports.get_active_human_persons = function () {
@@ -907,13 +907,13 @@ exports.get_people_for_stream_create = function () {
to your use case.
*/
const people_minus_you = [];
_.each(active_user_dict.values(), function (person) {
for (const person of active_user_dict.values()) {
if (!exports.is_my_user_id(person.user_id)) {
people_minus_you.push({email: person.email,
user_id: person.user_id,
full_name: person.full_name});
}
});
}
return people_minus_you.sort(people_cmp);
};

View File

@@ -34,11 +34,13 @@ const BinaryDict = function (pred) {
return self.falses.values();
};
self.values = function () {
const trues = self.trues.values();
const falses = self.falses.values();
const both = trues.concat(falses);
return both;
self.values = function* () {
for (const value of self.trues.values()) {
yield value;
}
for (const value of self.falses.values()) {
yield value;
}
};
self.get = function (k) {
@@ -269,7 +271,7 @@ exports.delete_sub = function (stream_id) {
};
exports.get_non_default_stream_names = function () {
let subs = stream_info.values();
let subs = [...stream_info.values()];
subs = _.reject(subs, function (sub) {
return exports.is_default_stream_id(sub.stream_id) || !sub.subscribed && sub.invite_only;
});
@@ -278,14 +280,14 @@ exports.get_non_default_stream_names = function () {
};
exports.get_unsorted_subs = function () {
return stream_info.values();
return [...stream_info.values()];
};
exports.get_updated_unsorted_subs = function () {
// This function is expensive in terms of calculating
// some values (particularly stream counts) but avoids
// prematurely sorting subs.
let all_subs = stream_info.values();
let all_subs = [...stream_info.values()];
// Add in admin options and stream counts.
_.each(all_subs, function (sub) {
@@ -307,11 +309,11 @@ exports.num_subscribed_subs = function () {
};
exports.subscribed_subs = function () {
return stream_info.true_values();
return [...stream_info.true_values()];
};
exports.unsubscribed_subs = function () {
return stream_info.false_values();
return [...stream_info.false_values()];
};
exports.subscribed_streams = function () {
@@ -771,7 +773,7 @@ exports.get_streams_for_admin = function () {
return util.strcmp(a.name, b.name);
}
const subs = stream_info.values();
const subs = [...stream_info.values()];
subs.sort(by_name);

View File

@@ -141,7 +141,7 @@ exports.topic_history = function (stream_id) {
};
self.get_recent_names = function () {
const my_recents = topics.values();
const my_recents = [...topics.values()];
const missing_topics = unread.get_missing_topics({
stream_id: stream_id,

View File

@@ -21,9 +21,9 @@ let zoomed = false;
exports.remove_expanded_topics = function () {
stream_popover.hide_topic_popover();
_.each(active_widgets.values(), function (widget) {
for (const widget of active_widgets.values()) {
widget.remove();
});
}
active_widgets.clear();
};
@@ -240,7 +240,7 @@ exports.active_stream_id = function () {
};
exports.get_stream_li = function () {
const widgets = active_widgets.values();
const widgets = [...active_widgets.values()];
if (widgets.length !== 1) {
return;

View File

@@ -47,7 +47,7 @@ exports.get_group_typists = function (group) {
};
exports.get_all_typists = function () {
let typists = _.flatten(typist_dct.values(), true);
let typists = _.flatten([...typist_dct.values()], true);
typists = util.sorted_ids(typists);
typists = _.uniq(typists, true);
return typists;

View File

@@ -55,7 +55,7 @@ exports.get_user_group_from_name = function (name) {
};
exports.get_realm_user_groups = function () {
return user_group_by_id_dict.values().sort(function (a, b) {
return [...user_group_by_id_dict.values()].sort(function (a, b) {
return a.id - b.id;
});
};