* enum.c (enum_flat_map): new method that concatenates the values

from given block.  also provides alias #collect_concat.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25456 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2009-10-24 17:01:38 +00:00
Родитель ba3bfda7a2
Коммит 55bf7f9d40
2 изменённых файлов: 49 добавлений и 0 удалений

Просмотреть файл

@ -7,6 +7,11 @@ Sat Oct 24 13:38:45 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* object.c (rb_obj_cmp): defines Object#<=>. [ruby-core:24063] * object.c (rb_obj_cmp): defines Object#<=>. [ruby-core:24063]
Sat Oct 24 09:51:28 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (enum_flat_map): new method that concatenates the values
from given block. also provides alias #collect_concat.
Sat Oct 24 00:36:47 2009 Tanaka Akira <akr@fsij.org> Sat Oct 24 00:36:47 2009 Tanaka Akira <akr@fsij.org>
* io.c (io_cntl): update max file descriptor by the result of * io.c (io_cntl): update max file descriptor by the result of

44
enum.c
Просмотреть файл

@ -396,6 +396,48 @@ enum_collect(VALUE obj)
return ary; return ary;
} }
static VALUE
flat_map_i(VALUE i, VALUE ary, int argc, VALUE *argv)
{
VALUE tmp;
i = enum_yield(argc, argv);
tmp = rb_check_array_type(i);
if (NIL_P(tmp)) {
rb_ary_push(ary, i);
}
else {
rb_ary_concat(ary, tmp);
}
return Qnil;
}
/*
* call-seq:
* enum.flat_map {| obj | block } => array
* enum.collect_concat {| obj | block } => array
*
* Returns a new array with the concatenated results of running
* <em>block</em> once for every element in <i>enum</i>.
*
* [[1,2],[3,4]].flat_map {|i| i } #=> [1, 2, 3, 4]
*
*/
static VALUE
enum_flat_map(VALUE obj)
{
VALUE ary;
RETURN_ENUMERATOR(obj, 0, 0);
ary = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, flat_map_i, ary);
return ary;
}
/* /*
* call-seq: * call-seq:
* enum.to_a => array * enum.to_a => array
@ -2375,6 +2417,8 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "reject", enum_reject, 0); rb_define_method(rb_mEnumerable, "reject", enum_reject, 0);
rb_define_method(rb_mEnumerable, "collect", enum_collect, 0); rb_define_method(rb_mEnumerable, "collect", enum_collect, 0);
rb_define_method(rb_mEnumerable, "map", enum_collect, 0); rb_define_method(rb_mEnumerable, "map", enum_collect, 0);
rb_define_method(rb_mEnumerable, "flat_map", enum_flat_map, 0);
rb_define_method(rb_mEnumerable, "collect_concat", enum_flat_map, 0);
rb_define_method(rb_mEnumerable, "inject", enum_inject, -1); rb_define_method(rb_mEnumerable, "inject", enum_inject, -1);
rb_define_method(rb_mEnumerable, "reduce", enum_inject, -1); rb_define_method(rb_mEnumerable, "reduce", enum_inject, -1);
rb_define_method(rb_mEnumerable, "partition", enum_partition, 0); rb_define_method(rb_mEnumerable, "partition", enum_partition, 0);