1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enum.c New methods: Enumerable#take, #take_while, #drop and

#drop_while; backported from 1.9.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16012 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-04-14 10:12:17 +00:00
parent 0e5c1f9981
commit 1b90348252
4 changed files with 430 additions and 0 deletions

View file

@ -1,3 +1,9 @@
Mon Apr 14 19:10:47 2008 Akinori MUSHA <knu@iDaemons.org>
* enum.c New methods: Enumerable#take, #take_while, #drop and
#drop_while; backported from 1.9.
Mon Apr 14 18:50:15 2008 Akinori MUSHA <knu@iDaemons.org>
* enum.c: New methods: Enumerable#one?, #none?, #minmax, #min_by,

4
NEWS
View file

@ -63,6 +63,8 @@ with all sufficient information, see the ChangeLog file.
* Enumerable#count
* Enumerable#cycle
* Enumerable#drop
* Enumerable#drop_while
* Enumerable#find_index
* Enumerable#first
* Enumerable#group_by
@ -72,6 +74,8 @@ with all sufficient information, see the ChangeLog file.
* Enumerable#minmax_by
* Enumerable#none?
* Enumerable#one?
* Enumerable#take
* Enumerable#take_while
New methods.

163
enum.c
View file

@ -1563,6 +1563,165 @@ enum_zip(argc, argv, obj)
return result;
}
static VALUE
take_i(i, arg)
VALUE i;
VALUE *arg;
{
if (arg[1]-- == 0) rb_iter_break();
rb_ary_push(arg[0], i);
return Qnil;
}
/*
* call-seq:
* enum.take(n) => array
*
* Returns first n elements from <i>enum</i>.
*
* a = [1, 2, 3, 4, 5, 0]
* a.take(3) # => [1, 2, 3]
*
*/
static VALUE
enum_take(obj, n)
VALUE obj;
VALUE n;
{
VALUE args[2];
long len = NUM2LONG(n);
if (len < 0) {
rb_raise(rb_eArgError, "attempt to take negative size");
}
args[1] = len;
args[0] = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)args);
return args[0];
}
static VALUE
take_while_i(i, ary)
VALUE i;
VALUE *ary;
{
if (!RTEST(rb_yield(i))) rb_iter_break();
rb_ary_push(*ary, i);
return Qnil;
}
/*
* call-seq:
* enum.take_while {|arr| block } => array
*
* Passes elements to the block until the block returns nil or false,
* then stops iterating and returns an array of all prior elements.
*
* a = [1, 2, 3, 4, 5, 0]
* a.take_while {|i| i < 3 } # => [1, 2]
*
*/
static VALUE
enum_take_while(obj)
VALUE obj;
{
VALUE ary;
RETURN_ENUMERATOR(obj, 0, 0);
ary = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, take_while_i, (VALUE)&ary);
return ary;
}
static VALUE
drop_i(i, arg)
VALUE i;
VALUE *arg;
{
if (arg[1] == 0) {
rb_ary_push(arg[0], i);
}
else {
arg[1]--;
}
return Qnil;
}
/*
* call-seq:
* enum.drop(n) => array
*
* Drops first n elements from <i>enum</i>, and returns rest elements
* in an array.
*
* a = [1, 2, 3, 4, 5, 0]
* a.drop(3) # => [4, 5, 0]
*
*/
static VALUE
enum_drop(obj, n)
VALUE obj;
VALUE n;
{
VALUE args[2];
long len = NUM2LONG(n);
if (len < 0) {
rb_raise(rb_eArgError, "attempt to drop negative size");
}
args[1] = len;
args[0] = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)args);
return args[0];
}
static VALUE
drop_while_i(i, args)
VALUE i;
VALUE *args;
{
if (!args[1] && !RTEST(rb_yield(i))) {
args[1] = Qtrue;
}
if (args[1]) {
rb_ary_push(args[0], i);
}
return Qnil;
}
/*
* call-seq:
* enum.drop_while {|arr| block } => array
*
* Drops elements up to, but not including, the first element for
* which the block returns nil or false and returns an array
* containing the remaining elements.
*
* a = [1, 2, 3, 4, 5, 0]
* a.drop_while {|i| i < 3 } # => [3, 4, 5, 0]
*
*/
static VALUE
enum_drop_while(obj)
VALUE obj;
{
VALUE args[2];
RETURN_ENUMERATOR(obj, 0, 0);
args[0] = rb_ary_new();
args[1] = Qfalse;
rb_block_call(obj, id_each, 0, 0, drop_while_i, (VALUE)args);
return args[0];
}
static VALUE
cycle_i(i, ary)
VALUE i;
@ -1676,6 +1835,10 @@ Init_Enumerable()
rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0);
rb_define_method(rb_mEnumerable,"enum_with_index", enum_each_with_index, 0);
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, 1);
rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);
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);
id_eqq = rb_intern("===");

257
test/ruby/test_enum.rb Normal file
View file

@ -0,0 +1,257 @@
require 'test/unit'
class TestEnumerable < Test::Unit::TestCase
def setup
@obj = Object.new
class << @obj
include Enumerable
def each
yield 1
yield 2
yield 3
yield 1
yield 2
end
end
@verbose = $VERBOSE
$VERBOSE = nil
end
def teardown
$VERBOSE = @verbose
end
def test_grep
assert_equal([1, 2, 1, 2], @obj.grep(1..2))
a = []
@obj.grep(2) {|x| a << x }
assert_equal([2, 2], a)
end
def test_count
assert_equal(2, @obj.count(1))
assert_equal(3, @obj.count {|x| x % 2 == 1 })
assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
assert_raise(ArgumentError) { @obj.count(0, 1) }
end
def test_find
assert_equal(2, @obj.find {|x| x % 2 == 0 })
assert_equal(nil, @obj.find {|x| false })
assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
end
def test_find_index
assert_equal(1, @obj.find_index(2))
assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
assert_equal(nil, @obj.find_index {|x| false })
assert_raise(ArgumentError) { @obj.find_index(0, 1) }
end
def test_find_all
assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 })
end
def test_reject
assert_equal([2, 3, 2], @obj.reject {|x| x < 2 })
end
def test_to_a
assert_equal([1, 2, 3, 1, 2], @obj.to_a)
end
def test_inject
assert_equal(12, @obj.inject {|z, x| z * x })
assert_equal(48, @obj.inject {|z, x| z * 2 + x })
assert_equal(12, @obj.inject(:*))
assert_equal(24, @obj.inject(2) {|z, x| z * x })
assert_equal(24, @obj.inject(2, :*) {|z, x| z * x })
end
def test_partition
assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
end
def test_group_by
h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
assert_equal(h, @obj.group_by {|x| x })
end
def test_first
assert_equal(1, @obj.first)
assert_equal([1, 2, 3], @obj.first(3))
end
def test_sort
assert_equal([1, 1, 2, 2, 3], @obj.sort)
end
def test_sort_by
assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x })
end
def test_all
assert_equal(true, @obj.all? {|x| x <= 3 })
assert_equal(false, @obj.all? {|x| x < 3 })
assert_equal(true, @obj.all?)
assert_equal(false, [true, true, false].all?)
end
def test_any
assert_equal(true, @obj.any? {|x| x >= 3 })
assert_equal(false, @obj.any? {|x| x > 3 })
assert_equal(true, @obj.any?)
assert_equal(false, [false, false, false].any?)
end
def test_one
assert(@obj.one? {|x| x == 3 })
assert(!(@obj.one? {|x| x == 1 }))
assert(!(@obj.one? {|x| x == 4 }))
assert(%w{ant bear cat}.one? {|word| word.length == 4})
assert(!(%w{ant bear cat}.one? {|word| word.length > 4}))
assert(!(%w{ant bear cat}.one? {|word| word.length < 4}))
assert(!([ nil, true, 99 ].one?))
assert([ nil, true, false ].one?)
end
def test_none
assert(@obj.none? {|x| x == 4 })
assert(!(@obj.none? {|x| x == 1 }))
assert(!(@obj.none? {|x| x == 3 }))
assert(%w{ant bear cat}.none? {|word| word.length == 5})
assert(!(%w{ant bear cat}.none? {|word| word.length >= 4}))
assert([].none?)
assert([nil].none?)
assert([nil,false].none?)
end
def test_min
assert_equal(1, @obj.min)
assert_equal(3, @obj.min {|a,b| b <=> a })
a = %w(albatross dog horse)
assert_equal("albatross", a.min)
assert_equal("dog", a.min {|a,b| a.length <=> b.length })
assert_equal(1, [3,2,1].min)
end
def test_max
assert_equal(3, @obj.max)
assert_equal(1, @obj.max {|a,b| b <=> a })
a = %w(albatross dog horse)
assert_equal("horse", a.max)
assert_equal("albatross", a.max {|a,b| a.length <=> b.length })
assert_equal(1, [3,2,1].max{|a,b| b <=> a })
end
def test_minmax
assert_equal([1, 3], @obj.minmax)
assert_equal([3, 1], @obj.minmax {|a,b| b <=> a })
a = %w(albatross dog horse)
assert_equal(["albatross", "horse"], a.minmax)
assert_equal(["dog", "albatross"], a.minmax {|a,b| a.length <=> b.length })
assert_equal([1, 3], [2,3,1].minmax)
assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a })
end
def test_min_by
assert_equal(3, @obj.min_by {|x| -x })
a = %w(albatross dog horse)
assert_equal("dog", a.min_by {|x| x.length })
assert_equal(3, [2,3,1].min_by {|x| -x })
end
def test_max_by
assert_equal(1, @obj.max_by {|x| -x })
a = %w(albatross dog horse)
assert_equal("albatross", a.max_by {|x| x.length })
assert_equal(1, [2,3,1].max_by {|x| -x })
end
def test_minmax_by
assert_equal([3, 1], @obj.minmax_by {|x| -x })
a = %w(albatross dog horse)
assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length })
assert_equal([3, 1], [2,3,1].minmax_by {|x| -x })
end
def test_member
assert(@obj.member?(1))
assert(!(@obj.member?(4)))
assert([1,2,3].member?(1))
assert(!([1,2,3].member?(4)))
end
def test_each_with_index
a = []
@obj.each_with_index {|x, i| a << [x, i] }
assert_equal([[1,0],[2,1],[3,2],[1,3],[2,4]], a)
hash = Hash.new
%w(cat dog wombat).each_with_index do |item, index|
hash[item] = index
end
assert_equal({"cat"=>0, "wombat"=>2, "dog"=>1}, hash)
end
def test_zip
assert_equal([[1,1],[2,2],[3,3],[1,1],[2,2]], @obj.zip(@obj))
a = []
@obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
end
def test_take
assert_equal([1,2,3], @obj.take(3))
end
def test_take_while
assert_equal([1,2], @obj.take_while {|x| x <= 2})
end
def test_drop
assert_equal([3,1,2], @obj.drop(2))
end
def test_drop_while
assert_equal([3,1,2], @obj.drop_while {|x| x <= 2})
end
def test_cycle
assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10))
end
def test_callcc
assert_raise(RuntimeError) do
c = nil
@obj.sort_by {|x| callcc {|c2| c ||= c2 }; x }
c.call
end
assert_raise(RuntimeError) do
c = nil
o = Object.new
class << o; self; end.class_eval do
define_method(:<=>) do |x|
callcc {|c2| c ||= c2 }
0
end
end
[o, o].sort_by {|x| x }
c.call
end
assert_raise(RuntimeError) do
c = nil
o = Object.new
class << o; self; end.class_eval do
define_method(:<=>) do |x|
callcc {|c2| c ||= c2 }
0
end
end
[o, o, o].sort_by {|x| x }
c.call
end
end
end