mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
enumerator.c: Enumerator::Lazy#uniq
* enumerator.c (lazy_uniq): new method Enumerator::Lazy#uniq. [Feature #11090] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55710 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
42c6a5137e
commit
6882b35460
3 changed files with 45 additions and 1 deletions
|
@ -1,4 +1,7 @@
|
||||||
Wed Jul 20 17:39:11 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Jul 20 17:44:07 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* enumerator.c (lazy_uniq): new method Enumerator::Lazy#uniq.
|
||||||
|
[Feature #11090]
|
||||||
|
|
||||||
* enum.c (enum_uniq): new method Enumerable#uniq.
|
* enum.c (enum_uniq): new method Enumerable#uniq.
|
||||||
[Feature #11090]
|
[Feature #11090]
|
||||||
|
|
36
enumerator.c
36
enumerator.c
|
@ -1958,6 +1958,41 @@ lazy_drop_while(VALUE obj)
|
||||||
Qnil, 0);
|
Qnil, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_uniq_i(VALUE i, VALUE hash, int argc, const VALUE *argv, VALUE yielder)
|
||||||
|
{
|
||||||
|
if (rb_hash_add_new_element(hash, i, Qfalse))
|
||||||
|
return Qnil;
|
||||||
|
return rb_funcall2(yielder, id_yield, argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
|
||||||
|
{
|
||||||
|
VALUE yielder = (--argc, *argv++);
|
||||||
|
i = rb_enum_values_pack(argc, argv);
|
||||||
|
return lazy_uniq_i(i, hash, argc, argv, yielder);
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
|
||||||
|
{
|
||||||
|
VALUE yielder = (--argc, *argv++);
|
||||||
|
i = rb_yield_values2(argc, argv);
|
||||||
|
return lazy_uniq_i(i, hash, argc, argv, yielder);
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_uniq(VALUE obj)
|
||||||
|
{
|
||||||
|
rb_block_call_func *const func =
|
||||||
|
rb_block_given_p() ? lazy_uniq_iter : lazy_uniq_func;
|
||||||
|
VALUE hash = rb_obj_hide(rb_hash_new());
|
||||||
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||||
|
func, hash),
|
||||||
|
0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_super(int argc, VALUE *argv, VALUE lazy)
|
lazy_super(int argc, VALUE *argv, VALUE lazy)
|
||||||
{
|
{
|
||||||
|
@ -2074,6 +2109,7 @@ InitVM_Enumerator(void)
|
||||||
rb_define_method(rb_cLazy, "slice_before", lazy_super, -1);
|
rb_define_method(rb_cLazy, "slice_before", lazy_super, -1);
|
||||||
rb_define_method(rb_cLazy, "slice_after", lazy_super, -1);
|
rb_define_method(rb_cLazy, "slice_after", lazy_super, -1);
|
||||||
rb_define_method(rb_cLazy, "slice_when", lazy_super, -1);
|
rb_define_method(rb_cLazy, "slice_when", lazy_super, -1);
|
||||||
|
rb_define_method(rb_cLazy, "uniq", lazy_uniq, 0);
|
||||||
|
|
||||||
rb_define_alias(rb_cLazy, "force", "to_a");
|
rb_define_alias(rb_cLazy, "force", "to_a");
|
||||||
|
|
||||||
|
|
|
@ -646,5 +646,10 @@ class TestEnumerator < Test::Unit::TestCase
|
||||||
e.next
|
e.next
|
||||||
assert_raise(StopIteration) { e.peek }
|
assert_raise(StopIteration) { e.peek }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_uniq
|
||||||
|
assert_equal([1, 2, 3, 4, 5, 10],
|
||||||
|
(1..Float::INFINITY).lazy.uniq{|x| (x**2) % 10 }.first(6))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue