aboutsummaryrefslogtreecommitdiffstats
path: root/enumerator.c
diff options
context:
space:
mode:
authorzverok <zverok.offline@gmail.com>2020-12-05 13:39:20 +0200
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-01-02 17:27:24 +0900
commitb8d33df1d9799cd04b92c1c28e42cc3028cc7524 (patch)
treee84491b66b3adf71b6f78c18848c59c10ba21320 /enumerator.c
parentf690eb34e28b000627e5f0649dd81a04e252286f (diff)
downloadruby-b8d33df1d9799cd04b92c1c28e42cc3028cc7524.tar.gz
Add Enumerable#compact and Enumerator::Lazy#compact
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/enumerator.c b/enumerator.c
index b4a7cb588e..6d6a588af9 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -2654,6 +2654,30 @@ lazy_uniq(VALUE obj)
}
static struct MEMO *
+lazy_compact_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
+{
+ if (NIL_P(result->memo_value)) return 0;
+ return result;
+}
+
+static const lazyenum_funcs lazy_compact_funcs = {
+ lazy_compact_proc, 0,
+};
+
+/*
+ * call-seq:
+ * lazy.compact -> lazy_enumerator
+ *
+ * Like Enumerable#compact, but chains operation to be lazy-evaluated.
+ */
+
+static VALUE
+lazy_compact(VALUE obj)
+{
+ return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_compact_funcs);
+}
+
+static struct MEMO *
lazy_with_index_proc(VALUE proc_entry, struct MEMO* result, VALUE memos, long memo_index)
{
struct proc_entry *entry = proc_entry_ptr(proc_entry);
@@ -4098,6 +4122,7 @@ InitVM_Enumerator(void)
rb_define_method(rb_cLazy, "slice_when", lazy_super, -1);
rb_define_method(rb_cLazy, "chunk_while", lazy_super, -1);
rb_define_method(rb_cLazy, "uniq", lazy_uniq, 0);
+ rb_define_method(rb_cLazy, "compact", lazy_compact, 0);
rb_define_method(rb_cLazy, "with_index", lazy_with_index, -1);
lazy_use_super_method = rb_hash_new_with_size(18);