Project

General

Profile

Feature #10958 ยป chunk_slice_before_remove_initial_state.patch

akr (Akira Tanaka), 03/10/2015 12:48 PM

View differences:

NEWS (working copy)
25 25
  * Array#flatten and Array#flatten! no longer try to call #to_ary
26 26
    method on elements beyond the given level.  [Bug #10748]
27 27

  
28
* Enumerable
29
  * Enumerable#chunk and Enumerable#slice_before no longer takes the
30
    initial_state argument.
31
    Use a local variable to maintain a state.
32

  
28 33
* IO
29 34
  * IO#close doesn't raise when the IO object is closed.  [Feature #10718]
30 35

  
enum.c (working copy)
2705 2705

  
2706 2706
struct chunk_arg {
2707 2707
    VALUE categorize;
2708
    VALUE state;
2709 2708
    VALUE prev_value;
2710 2709
    VALUE prev_elts;
2711 2710
    VALUE yielder;
......
2721 2720

  
2722 2721
    ENUM_WANT_SVALUE();
2723 2722

  
2724
    if (NIL_P(argp->state))
2725
        v = rb_funcall(argp->categorize, id_call, 1, i);
2726
    else
2727
        v = rb_funcall(argp->categorize, id_call, 2, i, argp->state);
2723
    v = rb_funcall(argp->categorize, id_call, 1, i);
2728 2724

  
2729 2725
    if (v == alone) {
2730 2726
        if (!NIL_P(argp->prev_value)) {
......
2770 2766

  
2771 2767
    enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
2772 2768
    memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
2773
    memo->state = rb_ivar_get(enumerator, rb_intern("chunk_initial_state"));
2774 2769
    memo->prev_value = Qnil;
2775 2770
    memo->prev_elts = Qnil;
2776 2771
    memo->yielder = yielder;
2777 2772

  
2778
    if (!NIL_P(memo->state))
2779
	memo->state = rb_obj_dup(memo->state);
2780

  
2781 2773
    rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
2782 2774
    memo = MEMO_FOR(struct chunk_arg, arg);
2783 2775
    if (!NIL_P(memo->prev_elts))
......
2788 2780
/*
2789 2781
 *  call-seq:
2790 2782
 *     enum.chunk { |elt| ... }                       -> an_enumerator
2791
 *     enum.chunk(initial_state) { |elt, state| ... } -> an_enumerator (deprecated)
2792 2783
 *
2793 2784
 *  Enumerates over the items, chunking them together based on the return
2794 2785
 *  value of the block.
......
2874 2865
 *
2875 2866
 */
2876 2867
static VALUE
2877
enum_chunk(int argc, VALUE *argv, VALUE enumerable)
2868
enum_chunk(VALUE enumerable)
2878 2869
{
2879
    VALUE initial_state;
2880 2870
    VALUE enumerator;
2881
    int n;
2882 2871

  
2883 2872
    if (!rb_block_given_p())
2884 2873
	rb_raise(rb_eArgError, "no block given");
2885
    n = rb_scan_args(argc, argv, "01", &initial_state);
2886
    if (n != 0)
2887
        rb_warn("initial_state given for chunk.  (Use local variables.)");
2888 2874

  
2889 2875
    enumerator = rb_obj_alloc(rb_cEnumerator);
2890 2876
    rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
2891 2877
    rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
2892
    rb_ivar_set(enumerator, rb_intern("chunk_initial_state"), initial_state);
2893 2878
    rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
2894 2879
    return enumerator;
2895 2880
}
......
2898 2883
struct slicebefore_arg {
2899 2884
    VALUE sep_pred;
2900 2885
    VALUE sep_pat;
2901
    VALUE state;
2902 2886
    VALUE prev_elts;
2903 2887
    VALUE yielder;
2904 2888
};
......
2913 2897

  
2914 2898
    if (!NIL_P(argp->sep_pat))
2915 2899
        header_p = rb_funcall(argp->sep_pat, id_eqq, 1, i);
2916
    else if (NIL_P(argp->state))
2917
        header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
2918 2900
    else
2919
        header_p = rb_funcall(argp->sep_pred, id_call, 2, i, argp->state);
2901
        header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
2920 2902
    if (RTEST(header_p)) {
2921 2903
        if (!NIL_P(argp->prev_elts))
2922 2904
            rb_funcall(argp->yielder, id_lshift, 1, argp->prev_elts);
......
2942 2924
    enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
2943 2925
    memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
2944 2926
    memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
2945
    memo->state = rb_attr_get(enumerator, rb_intern("slicebefore_initial_state"));
2946 2927
    memo->prev_elts = Qnil;
2947 2928
    memo->yielder = yielder;
2948 2929

  
2949
    if (!NIL_P(memo->state))
2950
        memo->state = rb_obj_dup(memo->state);
2951

  
2952 2930
    rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
2953 2931
    memo = MEMO_FOR(struct slicebefore_arg, arg);
2954 2932
    if (!NIL_P(memo->prev_elts))
......
2960 2938
 *  call-seq:
2961 2939
 *     enum.slice_before(pattern)                             -> an_enumerator
2962 2940
 *     enum.slice_before { |elt| bool }                       -> an_enumerator
2963
 *     enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator (deprecated)
2964 2941
 *
2965 2942
 *  Creates an enumerator for each chunked elements.
2966 2943
 *  The beginnings of chunks are defined by _pattern_ and the block.
......
3106 3083
    VALUE enumerator;
3107 3084

  
3108 3085
    if (rb_block_given_p()) {
3109
        VALUE initial_state;
3110
        int n;
3111
        n = rb_scan_args(argc, argv, "01", &initial_state);
3112
        if (n != 0)
3113
	    rb_warn("initial_state given for slice_before.  (Use local variables.)");
3086
        if (argc != 0)
3087
            rb_error_arity(argc, 0, 0);
3114 3088
        enumerator = rb_obj_alloc(rb_cEnumerator);
3115 3089
        rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
3116
        rb_ivar_set(enumerator, rb_intern("slicebefore_initial_state"), initial_state);
3117 3090
    }
3118 3091
    else {
3119 3092
        VALUE sep_pat;
......
3454 3427
    rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
3455 3428
    rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
3456 3429
    rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
3457
    rb_define_method(rb_mEnumerable, "chunk", enum_chunk, -1);
3430
    rb_define_method(rb_mEnumerable, "chunk", enum_chunk, 0);
3458 3431
    rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
3459 3432
    rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
3460 3433
    rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
test/ruby/test_enum.rb (working copy)
481 481
    e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
482 482
    assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)
483 483

  
484
    e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
485
    assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a)
486
    assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated.
487

  
488
    hs = [{}]
489
    e = [:foo].chunk(hs[0]) {|elt, h|
490
      hs << h
491
      true
492
    }
493
    assert_equal([[true, [:foo]]], e.to_a)
494
    assert_equal([[true, [:foo]]], e.to_a)
495
    assert_equal([{}, {}, {}], hs)
496
    assert_not_same(hs[0], hs[1])
497
    assert_not_same(hs[0], hs[2])
498
    assert_not_same(hs[1], hs[2])
499

  
500 484
    e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
501 485
    assert_equal([[:_alone, [1]],
502 486
                  [:_alone, [2]],
......
526 510
    e = @obj.slice_before {|elt| elt.odd? }
527 511
    assert_equal([[1,2], [3], [1,2]], e.to_a)
528 512

  
529
    e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
530
    assert_equal([[1,2], [3,1,2]], e.to_a)
531
    assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated.
532

  
533
    hs = [{}]
534
    e = [:foo].slice_before(hs[0]) {|elt, h|
535
      hs << h
536
      true
537
    }
538
    assert_equal([[:foo]], e.to_a)
539
    assert_equal([[:foo]], e.to_a)
540
    assert_equal([{}, {}, {}], hs)
541
    assert_not_same(hs[0], hs[1])
542
    assert_not_same(hs[0], hs[2])
543
    assert_not_same(hs[1], hs[2])
544

  
545 513
    ss = %w[abc defg h ijk l mno pqr st u vw xy z]
546 514
    assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
547 515
                 ss.slice_before(/\A...\z/).to_a)