Skip to content

Commit

Permalink
Implement Array.compact and .compact!; tests included.
Browse files Browse the repository at this point in the history
  • Loading branch information
treed committed Jul 23, 2009
1 parent ff8ddf9 commit db1ff47
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -191,6 +191,7 @@ namespace :test do |ns|
test "array/at.t"
test "array/clear.t"
test "array/collect.t"
test "array/compact.t"
test "array/concat.t"
test "array/delete.t"
test "array/empty.t"
Expand All @@ -212,7 +213,7 @@ namespace :test do |ns|
test "array/uniq.t"
test "array/warray.t"

task :all => [:array, :at, :clear, :collect, :concat, :delete, :empty, :equals, :fill, :first, :flatten, :grep, :include, :intersection, :join, :mathop, :pop, :reverse, :shift, :slice, :sort, :to_s, :uniq, :warray]
task :all => [:array, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fill, :first, :flatten, :grep, :include, :intersection, :join, :mathop, :pop, :reverse, :shift, :slice, :sort, :to_s, :uniq, :warray]
end

namespace :file do
Expand Down
30 changes: 30 additions & 0 deletions src/classes/Array.pir
Expand Up @@ -570,6 +570,36 @@ Returns self.
.return(self)
.end

.sub 'compact' :method
.local pmc array
.local int i, len
array = new 'CardinalArray'

len = self
i = 0

loop:
if i == len goto done

$P0 = self[i]

$I0 = isa $P0, "NilClass"
if $I0 goto end_loop

array.'push'($P0)
end_loop:
inc i
goto loop
done:
.return (array)
.end

.sub 'compact!' :method
$P0 = self.'compact'()
self = 0
self.'concat'($P0)
.end

=item delete()

Deletes the given element from the CardinalArray, replacing them with Undef.
Expand Down
19 changes: 19 additions & 0 deletions t/array/compact.t
@@ -0,0 +1,19 @@
require "Test"
include Test

plan 3

a = [0, 1, 2, nil, 3, nil, 4, nil, nil, 5, nil]
b = a.compact

is b, [0, 1,2,3,4,5], "Array.compact"

a = [1,2,3,4,5,'']
b = a.compact

is b, [1,2,3,4,5,''], "Array.compact with no nil elements"

a = [1, nil, 2]
a.compact!

is a, [1,2], "Array.compact!"

0 comments on commit db1ff47

Please sign in to comment.