Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move Array#select_with_index: into Enumerable

  • Loading branch information...
commit 41b6f04cadfffea60e2897478b42358b79b2dbc9 1 parent 88d6b42
Christopher Bertels authored
18 lib/array.fy
View
@@ -389,24 +389,6 @@ class Array {
subarr
}
- def select_with_index: condition {
- """
- @condition A @Block@ that is used as a filter on all elements in @self.
- @return An @Array@ containing all elements and their indices in @self that yield @true when called with @condition.
-
- Returns a new @Array@ with all elements and their indices that meet the given condition block.
- @condition is called with each element and its index in @self.
- """
-
- tmp = []
- each_with_index: |obj idx| {
- if: (condition call: [obj, idx]) then: {
- tmp << [obj, idx]
- }
- }
- tmp
- }
-
def to_hash {
"""
Returns a @Hash@ with each key-value pair in @self.
18 lib/enumerable.fy
View
@@ -388,6 +388,24 @@ class Fancy {
coll
}
+ def select_with_index: condition {
+ """
+ @condition A @Block@ that is used as a filter on all elements in @self.
+ @return An @Array@ containing all elements and their indices in @self that yield @true when called with @condition.
+
+ Returns a new @Array@ with all elements and their indices that meet the given condition block.
+ @condition is called with each element and its index in @self.
+ """
+
+ tmp = []
+ each_with_index: |obj idx| {
+ if: (condition call: [obj, idx]) then: {
+ tmp << [obj, idx]
+ }
+ }
+ tmp
+ }
+
def reject: condition {
"""
Similar to @select:@ but inverse.
4 tests/array.fy
View
@@ -356,10 +356,6 @@ FancySpec describe: Array with: {
[1,2,3,4] any?: |x| { x > 4 } . is: false
}
- it: "is selected from it with each index" with: 'select_with_index: when: {
- ["yooo",2,3,1,'foo,"bar"] select_with_index: |x i| { x is_a?: Fixnum } . is: [[2,1], [3,2], [1,3]]
- }
-
it: "returns its remaining (all but the first) elements as a new Array" with: 'rest when: {
[1,2,3,4] rest is: [2,3,4]
[] rest is: []
4 tests/enumerable.fy
View
@@ -252,4 +252,8 @@ FancySpec describe: Fancy Enumerable with: {
[1,2,1,2] last_index_of: 2 . is: 3
[1,2,1,2] last_index_of: 3 . is: nil
}
+
+ it: "is selected from it with each index" with: 'select_with_index: when: {
+ ["yooo",2,3,1,'foo,"bar"] select_with_index: |x i| { x is_a?: Fixnum } . is: [[2,1], [3,2], [1,3]]
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.