Permalink
Browse files

add enumerable.rb

  • Loading branch information...
1 parent 261c968 commit 2379d73f49cdaa76a1cab929914a7d4bbb893d6a @quix committed Jul 17, 2009
Showing with 115 additions and 2 deletions.
  1. +1 −2 lib/quix.rb
  2. +28 −0 lib/quix/ext/1.8.6/enumerable.rb
  3. +39 −0 lib/quix/ext/enumerable.rb
  4. +47 −0 test/test_enumerable.rb
View
3 lib/quix.rb
@@ -1,8 +1,7 @@
-require 'enumerator' if RUBY_VERSION <= "1.8.6"
-
require 'quix/ext/array'
require 'quix/ext/dir'
+require 'quix/ext/enumerable'
require 'quix/ext/file'
require 'quix/ext/hash'
require 'quix/ext/kernel'
View
28 lib/quix/ext/1.8.6/enumerable.rb
@@ -0,0 +1,28 @@
+
+require 'enumerator'
+
+module Enumerable
+ def drop_while
+ result = []
+ found = false
+ each { |elem|
+ if found or not yield(elem)
+ result << elem
+ found = true
+ end
+ }
+ result
+ end
+
+ def take_while
+ result = []
+ each { |elem|
+ if yield(elem)
+ result << elem
+ else
+ return result
+ end
+ }
+ result
+ end
+end
View
39 lib/quix/ext/enumerable.rb
@@ -0,0 +1,39 @@
+
+require 'quix/ext/1.8.6/enumerable.rb' if RUBY_VERSION <= "1.8.6"
+
+module Enumerable
+ def group_consecutive_by
+ result = Array.new
+ last_value = nil
+ each_with_index { |elem, index|
+ value = yield(elem)
+ if index == 0 or value != last_value
+ result << Array.new
+ end
+ result.last << elem
+ last_value = value
+ }
+ result
+ end
+
+ def build_hash
+ inject(Hash.new) { |acc, elem|
+ result = yield(elem)
+ acc.merge!(result[0] => result[1])
+ }
+ end
+
+ def take_until(&block)
+ take_while { |elem|
+ not block.call(elem)
+ }
+ end
+
+ def drop_until(&block)
+ drop_while { |elem|
+ not block.call(elem)
+ }
+ end
+
+ alias_method :each_consecutive, :each_cons
+end
View
47 test/test_enumerable.rb
@@ -0,0 +1,47 @@
+require File.dirname(__FILE__) + "/common"
+
+require "quix/ext/enumerable"
+
+class TestEnumerable < Test::Unit::TestCase
+ def test_group_consecutive_by
+ data = [3, 4, 5, 5, 6, 5, 5, 4, 4, 4, 4, 3, 3, 2, 1]
+ grouped = [[3], [4], [5, 5], [6], [5, 5], [4, 4, 4, 4], [3, 3], [2], [1]]
+
+ assert_equal grouped, data.group_consecutive_by { |t| t }
+
+ data = (1..6).to_a
+ assert_equal data.map { |t| [t] }, data.group_consecutive_by { |t| t }
+
+ expected = [[1, 2, 3], [4, 5, 6]]
+ computed = data.group_consecutive_by { |t| t <= 3 }
+ assert_equal expected, computed
+ end
+
+ def test_build_hash
+ data = 3..5
+ expected = {
+ 3 => 9,
+ 4 => 16,
+ 5 => 25,
+ }
+ computed = data.build_hash { |elem|
+ [elem, elem**2]
+ }
+ assert_equal expected, computed
+ end
+
+ def test_take_drop
+ data = 3..8
+
+ assert_equal [6, 7, 8], data.drop_while { |t| t < 6 }
+ assert_equal [6, 7, 8], data.drop_until { |t| t == 6 }
+
+ assert_equal [3, 4, 5], data.take_while { |t| t < 6 }
+ assert_equal [3, 4, 5], data.take_until { |t| t == 6 }
+
+ assert_equal data.to_a, data.take_while { true }
+ assert_equal data.to_a, data.take_until { |t| t == 999 }
+ assert_equal [], data.drop_while { true }
+ assert_equal [], data.drop_until { |t| t == 999 }
+ end
+end

0 comments on commit 2379d73

Please sign in to comment.