[Feature:1.9] Enumerable#uniq

e$B$J$+$@$G$9!#e(B

Arraye$B$@$1$G$J$/!“e(BEnumerablee$B$K$be(Buniqe$B$,$”$C$F$b$$$$$s$8$c$J$$$G$7$ge(B
e$B$&$+!#e(B

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("===");

e$B1sF#$G$9!#e(B

2009/02/13 22:17 Nobuyoshi N. [email protected]:

e$B$J$+$@$G$9!#e(B

Arraye$B$@$1$G$J$/!“e(BEnumerablee$B$K$be(Buniqe$B$,$”$C$F$b$$$$$s$8$c$J$$$G$7$ge(B
e$B$&$+!#e(B

e$B;d$b$$$$$H;W$$$^$9!#e(B
e$BJX>h$G$9$,!"e(BEnumerable#compact e$B$b$[$7$$$G$9!#e(B

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”);

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

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

|e$BJX>h$G$9$,!"e(BEnumerable#compact e$B$b$[$7$$$G$9!#e(B

uniqe$BF1MM!“A4FI$9~$$,I,MW$J%a%=%C%I$rA}$d$9$N$O$”$^$j;?@.$7e(B
e$B$^$;$s!#e(B

Enumeratore$B$rJV$9e(Bcompacte$B$G$“$l$P$b$7$+$7$?$i2ACM$,$”$k$+$b$7e(B
e$B$l$^$;$s!#$1$I!“e(BArraye$B$HLa$jCM$,0c$&$N$ONI$/$J$$$+$b$7$l$^$;e(B
e$B$s$M$(!#e(BLazyArraye$B$N$h$&$J$b$N$,$”$l$P$h$$$N$+$J!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:37997] [Feature:1.9] Enumerable#uniq”
on Fri, 13 Feb 2009 22:17:21 +0900, Nobuyoshi N.
[email protected] writes:

|Arraye$B$@$1$G$J$/!“e(BEnumerablee$B$K$be(Buniqe$B$,$”$C$F$b$$$$$s$8$c$J$$$G$7$ge(B
|e$B$&$+!#e(B

Enumerablee$B$OM-8B$NJ]>Z$,$J$$$N$GA4FI$9~$$,I,MW$J%a%=%C%I$re(B
e$B$3$l0J>eA}$d$9$N$O$I$&$+$H;W$$$^$9!#$b$&$9$G$K$"$k$N$O:o$l$Je(B
e$B$$$K$7$F$b!#e(B