Feature #10958 ยป chunk_slice_before_remove_initial_state.patch
| NEWS (working copy) | ||
|---|---|---|
|
* Array#flatten and Array#flatten! no longer try to call #to_ary
|
||
|
method on elements beyond the given level. [Bug #10748]
|
||
|
* Enumerable
|
||
|
* Enumerable#chunk and Enumerable#slice_before no longer takes the
|
||
|
initial_state argument.
|
||
|
Use a local variable to maintain a state.
|
||
|
* IO
|
||
|
* IO#close doesn't raise when the IO object is closed. [Feature #10718]
|
||
| enum.c (working copy) | ||
|---|---|---|
|
struct chunk_arg {
|
||
|
VALUE categorize;
|
||
|
VALUE state;
|
||
|
VALUE prev_value;
|
||
|
VALUE prev_elts;
|
||
|
VALUE yielder;
|
||
| ... | ... | |
|
ENUM_WANT_SVALUE();
|
||
|
if (NIL_P(argp->state))
|
||
|
v = rb_funcall(argp->categorize, id_call, 1, i);
|
||
|
else
|
||
|
v = rb_funcall(argp->categorize, id_call, 2, i, argp->state);
|
||
|
v = rb_funcall(argp->categorize, id_call, 1, i);
|
||
|
if (v == alone) {
|
||
|
if (!NIL_P(argp->prev_value)) {
|
||
| ... | ... | |
|
enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
|
||
|
memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
|
||
|
memo->state = rb_ivar_get(enumerator, rb_intern("chunk_initial_state"));
|
||
|
memo->prev_value = Qnil;
|
||
|
memo->prev_elts = Qnil;
|
||
|
memo->yielder = yielder;
|
||
|
if (!NIL_P(memo->state))
|
||
|
memo->state = rb_obj_dup(memo->state);
|
||
|
rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
|
||
|
memo = MEMO_FOR(struct chunk_arg, arg);
|
||
|
if (!NIL_P(memo->prev_elts))
|
||
| ... | ... | |
|
/*
|
||
|
* call-seq:
|
||
|
* enum.chunk { |elt| ... } -> an_enumerator
|
||
|
* enum.chunk(initial_state) { |elt, state| ... } -> an_enumerator (deprecated)
|
||
|
*
|
||
|
* Enumerates over the items, chunking them together based on the return
|
||
|
* value of the block.
|
||
| ... | ... | |
|
*
|
||
|
*/
|
||
|
static VALUE
|
||
|
enum_chunk(int argc, VALUE *argv, VALUE enumerable)
|
||
|
enum_chunk(VALUE enumerable)
|
||
|
{
|
||
|
VALUE initial_state;
|
||
|
VALUE enumerator;
|
||
|
int n;
|
||
|
if (!rb_block_given_p())
|
||
|
rb_raise(rb_eArgError, "no block given");
|
||
|
n = rb_scan_args(argc, argv, "01", &initial_state);
|
||
|
if (n != 0)
|
||
|
rb_warn("initial_state given for chunk. (Use local variables.)");
|
||
|
enumerator = rb_obj_alloc(rb_cEnumerator);
|
||
|
rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
|
||
|
rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
|
||
|
rb_ivar_set(enumerator, rb_intern("chunk_initial_state"), initial_state);
|
||
|
rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
|
||
|
return enumerator;
|
||
|
}
|
||
| ... | ... | |
|
struct slicebefore_arg {
|
||
|
VALUE sep_pred;
|
||
|
VALUE sep_pat;
|
||
|
VALUE state;
|
||
|
VALUE prev_elts;
|
||
|
VALUE yielder;
|
||
|
};
|
||
| ... | ... | |
|
if (!NIL_P(argp->sep_pat))
|
||
|
header_p = rb_funcall(argp->sep_pat, id_eqq, 1, i);
|
||
|
else if (NIL_P(argp->state))
|
||
|
header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
|
||
|
else
|
||
|
header_p = rb_funcall(argp->sep_pred, id_call, 2, i, argp->state);
|
||
|
header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
|
||
|
if (RTEST(header_p)) {
|
||
|
if (!NIL_P(argp->prev_elts))
|
||
|
rb_funcall(argp->yielder, id_lshift, 1, argp->prev_elts);
|
||
| ... | ... | |
|
enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
|
||
|
memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
|
||
|
memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
|
||
|
memo->state = rb_attr_get(enumerator, rb_intern("slicebefore_initial_state"));
|
||
|
memo->prev_elts = Qnil;
|
||
|
memo->yielder = yielder;
|
||
|
if (!NIL_P(memo->state))
|
||
|
memo->state = rb_obj_dup(memo->state);
|
||
|
rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
|
||
|
memo = MEMO_FOR(struct slicebefore_arg, arg);
|
||
|
if (!NIL_P(memo->prev_elts))
|
||
| ... | ... | |
|
* call-seq:
|
||
|
* enum.slice_before(pattern) -> an_enumerator
|
||
|
* enum.slice_before { |elt| bool } -> an_enumerator
|
||
|
* enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator (deprecated)
|
||
|
*
|
||
|
* Creates an enumerator for each chunked elements.
|
||
|
* The beginnings of chunks are defined by _pattern_ and the block.
|
||
| ... | ... | |
|
VALUE enumerator;
|
||
|
if (rb_block_given_p()) {
|
||
|
VALUE initial_state;
|
||
|
int n;
|
||
|
n = rb_scan_args(argc, argv, "01", &initial_state);
|
||
|
if (n != 0)
|
||
|
rb_warn("initial_state given for slice_before. (Use local variables.)");
|
||
|
if (argc != 0)
|
||
|
rb_error_arity(argc, 0, 0);
|
||
|
enumerator = rb_obj_alloc(rb_cEnumerator);
|
||
|
rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
|
||
|
rb_ivar_set(enumerator, rb_intern("slicebefore_initial_state"), initial_state);
|
||
|
}
|
||
|
else {
|
||
|
VALUE sep_pat;
|
||
| ... | ... | |
|
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, "chunk", enum_chunk, -1);
|
||
|
rb_define_method(rb_mEnumerable, "chunk", enum_chunk, 0);
|
||
|
rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
|
||
|
rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
|
||
|
rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
|
||
| test/ruby/test_enum.rb (working copy) | ||
|---|---|---|
|
e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
|
||
|
assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)
|
||
|
e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
|
||
|
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a)
|
||
|
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated.
|
||
|
hs = [{}]
|
||
|
e = [:foo].chunk(hs[0]) {|elt, h|
|
||
|
hs << h
|
||
|
true
|
||
|
}
|
||
|
assert_equal([[true, [:foo]]], e.to_a)
|
||
|
assert_equal([[true, [:foo]]], e.to_a)
|
||
|
assert_equal([{}, {}, {}], hs)
|
||
|
assert_not_same(hs[0], hs[1])
|
||
|
assert_not_same(hs[0], hs[2])
|
||
|
assert_not_same(hs[1], hs[2])
|
||
|
e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
|
||
|
assert_equal([[:_alone, [1]],
|
||
|
[:_alone, [2]],
|
||
| ... | ... | |
|
e = @obj.slice_before {|elt| elt.odd? }
|
||
|
assert_equal([[1,2], [3], [1,2]], e.to_a)
|
||
|
e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
|
||
|
assert_equal([[1,2], [3,1,2]], e.to_a)
|
||
|
assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated.
|
||
|
hs = [{}]
|
||
|
e = [:foo].slice_before(hs[0]) {|elt, h|
|
||
|
hs << h
|
||
|
true
|
||
|
}
|
||
|
assert_equal([[:foo]], e.to_a)
|
||
|
assert_equal([[:foo]], e.to_a)
|
||
|
assert_equal([{}, {}, {}], hs)
|
||
|
assert_not_same(hs[0], hs[1])
|
||
|
assert_not_same(hs[0], hs[2])
|
||
|
assert_not_same(hs[1], hs[2])
|
||
|
ss = %w[abc defg h ijk l mno pqr st u vw xy z]
|
||
|
assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
|
||
|
ss.slice_before(/\A...\z/).to_a)
|
||