Skip to content

Commit

Permalink
Moved non-rbx specific Array methods from lib/rbx/array to lib/array.…
Browse files Browse the repository at this point in the history
… Also changed Array#each: to return self.
  • Loading branch information
bakkdoor committed Apr 26, 2011
1 parent 1484a14 commit 4d42e67
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 126 deletions.
124 changes: 124 additions & 0 deletions lib/array.fy
Expand Up @@ -7,6 +7,30 @@ class Array {

include: FancyEnumerable

def Array new: size {
"Creates a new Array with a given size (default value is nil)."

Array new: size with: nil
}

def clone {
"Clones (shallow copy) the Array."
new = []
each: |x| {
new << x
}
new
}

def append: arr {
"Appends another Array onto this one."

arr each: |x| {
self << x
}
self
}

def [] index {
"""
Given an Array of 2 Numbers, it returns the sub-array between the
Expand Down Expand Up @@ -47,6 +71,34 @@ class Array {
from: 1 to: -1
}

def each: block {
"""
@block @Block@ to be called for each element in @self.
@return Return value of calling @block on the last item in @self.
Calls a given @Block@ with each element in the @Array@.
"""

size times: |i| {
block call: [at: i]
}
}

def each_with_index: block {
"""
@block @Block@ to be called with each element and its inde in the @Array@.
Iterate over all elements in Array. Calls a given Block with each element and its index.
"""

i = 0
each: |x| {
block call: [x, i]
i = i + 1
}
nil
}

def =? other {
"""
@other Other @Array@ to compare this one to.
Expand Down Expand Up @@ -93,6 +145,7 @@ class Array {
return x
}
}
nil
}

def values_at: idx_arr {
Expand Down Expand Up @@ -219,6 +272,77 @@ class Array {
0 upto: (size - 1)
}

def indices_of: item {
"""
@item Item/Value for which a list of indices is requested within an @Array@.
@return @Array@ of all indices for a given value within an @Array@ (possibly empty).
Returns an Array of all indices of this item. Empty Array if item does not occur.
"""

tmp = []
each_with_index: |obj, idx| {
if: (item == obj) then: {
tmp << idx
}
}
tmp
}

def from: from to: to {
"""
@from Start index for sub-array.
@to End index ofr sub-array.
Returns sub-array starting at from: and going to to:
"""

if: (from < 0) then: {
from = size + from
}
if: (to < 0) then: {
to = size + to
}
subarr = []
from upto: to do: |i| {
subarr << (at: i)
}
subarr
}

def select: block {
"""
@block Predicate @Block@ to be used as filter.
@return @Array@ of all the elements for which @block doesn't yield @false or @nil.
Returns a new Array with all the elements in self that yield a
true-ish value when called with the given Block.
"""

tmp = []
each: |x| {
if: (block call: [x]) then: {
tmp << x
}
}
return tmp
}

def select_with_index: block {
"""
Same as select:, just gets also called with an additional argument
for each element's index value.
"""

tmp = []
each_with_index: |obj idx| {
if: (block call: [obj, idx]) then: {
tmp << [obj, idx]
}
}
tmp
}

def Array === object {
"""
@object Object to match @self against.
Expand Down
125 changes: 1 addition & 124 deletions lib/rbx/array.fy
Expand Up @@ -18,49 +18,12 @@ class Array {
Array new(size, default)
}

def Array new: size {
"Creates a new Array with a given size (default value is nil)."

Array new: size with: nil
}

def append: arr {
"Appends another Array onto this one."

arr each: |x| {
self << x
}
self
}

def includes?: obj {
"Indicates, if an Array includes a given value."

include?(obj)
}

def clone {
"Clones (shallow copy) the Array."
new = []
each: |x| {
new << x
}
new
}

def each: block {
"""
@block @Block@ to be called for each element in @self.
@return Return value of calling @block on the last item in @self.
Calls a given @Block@ with each element in the @Array@.
"""

val = nil
each() |x| { val = block call: [x] }
val
}

def remove_at: index {
"""
Removes an element at a given index.
Expand Down Expand Up @@ -95,7 +58,7 @@ class Array {
Returns the element in the @Array@ at a given index.
"""

ruby: '[] args: [idx]
at(idx)
}

def at: idx put: obj {
Expand All @@ -106,24 +69,9 @@ class Array {
Inserts a given object at a given index (position) in the Array.
"""

ruby: '[]= args: [idx, obj]
}

def each_with_index: block {
"""
@block @Block@ to be called with each element and its inde in the @Array@.
Iterate over all elements in Array. Calls a given Block with each element and its index.
"""

i = 0
each: |x| {
block call: [x, i]
i = i + 1
}
}

def index: item {
"""
@item Item/Value for which the index is requested within an @Array@.
Expand All @@ -134,44 +82,6 @@ class Array {
index(item)
}

def indices_of: item {
"""
@item Item/Value for which a list of indices is requested within an @Array@.
@return @Array@ of all indices for a given value within an @Array@ (possibly empty).
Returns an Array of all indices of this item. Empty Array if item does not occur.
"""

tmp = []
each_with_index: |obj, idx| {
if: (item == obj) then: {
tmp << idx
}
}
tmp
}

def from: from to: to {
"""
@from Start index for sub-array.
@to End index ofr sub-array.
Returns sub-array starting at from: and going to to:
"""

if: (from < 0) then: {
from = size + from
}
if: (to < 0) then: {
to = size + to
}
subarr = []
from upto: to do: |i| {
subarr << (at: i)
}
subarr
}

def last: count {
"""
@count Number of last elements to get from an @Array@.
Expand Down Expand Up @@ -202,39 +112,6 @@ class Array {
all?(&block)
}

def select: block {
"""
@block Predicate @Block@ to be used as filter.
@return @Array@ of all the elements for which @block doesn't yield @false or @nil.
Returns a new Array with all the elements in self that yield a
true-ish value when called with the given Block.
"""

tmp = []
each: |x| {
if: (block call: [x]) then: {
tmp << x
}
}
return tmp
}

def select_with_index: block {
"""
Same as select:, just gets also called with an additional argument
for each element's index value.
"""

tmp = []
each_with_index: |obj idx| {
if: (block call: [obj, idx]) then: {
tmp << [obj, idx]
}
}
tmp
}

def reject: block {
"""
Returns a new Array with all the elements which yield nil or false
Expand Down
15 changes: 13 additions & 2 deletions tests/array.fy
Expand Up @@ -10,8 +10,7 @@ FancySpec describe: Array with: {

it: "should iterate over all elements, calling a block" for: 'each: when: {
sum = 0
retval = [1,2,3,4,5] each: |x| { sum = sum + x }
retval should == sum
[1,2,3,4,5] each: |x| { sum = sum + x }
sum should == ([1,2,3,4,5] sum)
}

Expand Down Expand Up @@ -43,6 +42,18 @@ FancySpec describe: Array with: {
arr at: 0 . should == 'a
}

it: "should set the value for a given index" for: 'at:put: when: {
arr = [1,2,3]
arr at: 0 put: 10
arr should == [10, 2, 3]
arr at: -1 put: 30
arr should == [10, 2, 30]
arr at: 1 put: 20
arr should == [10,20,30]
arr at: 3 put: 40
arr should == [10,20,30,40]
}

it: "should NOT include the items" for: "includes?:" when: {
arr = ['a, 10, "hello, world"]
arr includes?: "hello" . should == false
Expand Down

0 comments on commit 4d42e67

Please sign in to comment.