Skip to content

Commit

Permalink
Implement Array#select.
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Reed <ted.reed@gmail.com>
  • Loading branch information
dtm authored and treed committed Aug 23, 2009
1 parent a2b5f4d commit f33b494
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -302,6 +302,7 @@ namespace :test do |ns|
test "array/reject.t"
test "array/replace.t"
test "array/reverse.t"
test "array/select.t"
test "array/shift.t"
test "array/slice.t"
test "array/sort.t"
Expand All @@ -311,7 +312,7 @@ namespace :test do |ns|
test "array/warray.t"

desc "Run tests on Array."
task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :count, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :pop, :push, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :count, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :pop, :push, :reject, :replace, :reverse, :select, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
end

namespace :file do
Expand Down
21 changes: 21 additions & 0 deletions src/classes/Array.pir
Expand Up @@ -284,6 +284,27 @@ Return a sorted copy of the list
.return ($P0)
.end

.sub 'select' :method
.param pmc block :named('!BLOCK')
.local pmc uarray, val

uarray = new 'CardinalArray'
$P0 = iter self

loop:
unless $P0 goto done

$P1 = shift $P0
$P2 = block($P1)
unless $P2 goto loop

uarray.'push'($P1)
goto loop

done:
.return (uarray)
.end

.sub reject :method
.param pmc block :named("!BLOCK")
.local pmc uarray, val
Expand Down
11 changes: 11 additions & 0 deletions t/array/select.t
@@ -0,0 +1,11 @@
require 'Test'
include Test
plan 2

a = [ "a", "b", "c", "d", "e", "f" ]
b = a.select {|v| v == "a"}
is b, [ "a" ]

a = [ 0, 1, 2, 3, 4, 5 ]
b = a.select {|v| v % 2 == 1}
is b, [ 1, 3, 5 ]

0 comments on commit f33b494

Please sign in to comment.