Project

General

Profile

Actions

Feature #1153

closed

Enumerable#uniq

Added by nobu (Nobuyoshi Nakada) almost 16 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
[ruby-dev:37997]

Description

=begin
なかだです。

Arrayだけでなく、Enumerableにもuniqがあってもいいんじゃないでしょ
うか。


Index: enum.c

--- enum.c (revision 22100)
+++ enum.c (working copy)
@@ -1794,4 +1794,29 @@ enum_cycle(int argc, VALUE *argv, VALUE
}

+static VALUE
+enum_uniq_i(VALUE i, VALUE hash, int argc, VALUE *argv)
+{

  • return rb_hash_aset(hash, i, Qtrue);
    +}

+static int
+push_key(st_data_t key, st_data_t val, st_data_t ary)
+{

  • rb_ary_push((VALUE)ary, (VALUE)key);
  • return ST_DELETE;
    +}

+static VALUE
+enum_uniq(VALUE obj)
+{

  • VALUE hash = rb_hash_new(), uniq;
  • RBASIC(hash)->klass = 0;
  • rb_block_call(obj, id_each, 0, 0, enum_uniq_i, hash);
  • uniq = rb_ary_new2(RHASH_SIZE(hash));
  • st_foreach(RHASH_TBL(hash), push_key, uniq);
  • return uniq;
    +}

/*

  • The Enumerable mixin provides collection classes with
    @@ -1853,4 +1878,5 @@ Init_Enumerable(void)
    rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
    rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
  • rb_define_method(rb_mEnumerable, "uniq", enum_uniq, 0);

    id_eqq = rb_intern("===");

--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦
=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11090: Enumerable#each_uniq and #each_uniq_byClosednobu (Nobuyoshi Nakada)Actions
Actions #1

Updated by mame (Yusuke Endoh) almost 16 years ago

=begin
遠藤です。

2009/02/13 22:17 Nobuyoshi Nakada :

なかだです。

Arrayだけでなく、Enumerableにもuniqがあってもいいんじゃないでしょ
うか。

私もいいと思います。
便乗ですが、Enumerable#compact もほしいです。

Index: enum.c

--- enum.c (revision 22297)
+++ enum.c (working copy)
@@ -1793,6 +1793,32 @@
return Qnil; /* not reached */
}

+static VALUE
+compact_i(VALUE i, VALUE ary, int argc, VALUE *argv)
+{

  • VALUE val = enum_values_pack(argc, argv);
  • if (!NIL_P(val)) rb_ary_push(ary, val);
  • return Qnil;
    +}

+/*

    • call-seq:
    • enum.compact    ->   array
      
    • Returns an array containing the items in enum with all +nil+
    • elements removed.
    • [ "a", nil, "b", nil, "c" ].to_enum.compact #=> [ "a", "b", "c" ]
      
  • */

+static VALUE
+enum_compact(VALUE obj)
+{

  • VALUE ary = rb_ary_new();
  • rb_block_call(obj, id_each, 0, 0, compact_i, ary);
  • return ary;
    +}

/*

  • The Enumerable mixin provides collection classes with
  • several traversal and searching methods, and with the ability to
    @@ -1852,6 +1878,7 @@
    rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
    rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
    rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
  • rb_define_method(rb_mEnumerable, "compact", enum_compact, 0);

    id_eqq = rb_intern("===");
    id_each = rb_intern("each");

--
Yusuke ENDOH

=end

Actions #2

Updated by matz (Yukihiro Matsumoto) almost 16 years ago

=begin
まつもと ゆきひろです

In message "Re: [ruby-dev:37997] [Feature:1.9] Enumerable#uniq"
on Fri, 13 Feb 2009 22:17:21 +0900, Nobuyoshi Nakada writes:

|Arrayだけでなく、Enumerableにもuniqがあってもいいんじゃないでしょ
|うか。

Enumerableは有限の保証がないので全読み込みが必要なメソッドを
これ以上増やすのはどうかと思います。もうすでにあるのは削れな
いにしても。

=end

Actions #3

Updated by matz (Yukihiro Matsumoto) almost 16 years ago

=begin
まつもと ゆきひろです

In message "Re: [ruby-dev:38002] Re: [Feature:1.9] Enumerable#uniq"
on Sat, 14 Feb 2009 09:59:14 +0900, Yusuke ENDOH writes:

|便乗ですが、Enumerable#compact もほしいです。

uniq同様、全読み込みが必要なメソッドを増やすのはあまり賛成し
ません。

Enumeratorを返すcompactであればもしかしたら価値があるかもし
れません。けど、Arrayと戻り値が違うのは良くないかもしれませ
んねえ。LazyArrayのようなものがあればよいのかな。

=end

Actions #4

Updated by marcandre (Marc-Andre Lafortune) about 15 years ago

  • Category set to core
  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #5

Updated by naruse (Yui NARUSE) about 15 years ago

  • Status changed from Open to Rejected

=begin

=end

Actions #6

Updated by shyouhei (Shyouhei Urabe) over 5 years ago

  • Related to Feature #11090: Enumerable#each_uniq and #each_uniq_by added

Updated by shyouhei (Shyouhei Urabe) over 5 years ago

  • Description updated (diff)
  • Status changed from Rejected to Closed

This is accepted & implemented as per #11090.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0