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.equal(d.has('baz'), false);
assert.deepEqual([...d.keys()], ['foo', 'bar']); 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']]); assert.deepEqual(d.items(), [['foo', 'baz'], ['bar', 'qux']]);
d.delete('bar'); d.delete('bar');

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -47,7 +47,7 @@ exports.get_group_typists = function (group) {
}; };
exports.get_all_typists = function () { 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 = util.sorted_ids(typists);
typists = _.uniq(typists, true); typists = _.uniq(typists, true);
return typists; return typists;

View File

@@ -55,7 +55,7 @@ exports.get_user_group_from_name = function (name) {
}; };
exports.get_realm_user_groups = function () { 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; return a.id - b.id;
}); });
}; };