Skip to content

Commit

Permalink
implement filter builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Feb 1, 2013
1 parent bafd115 commit 5277099
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ruby/ycp/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def self.change object, *params

# - Filters a List
# - Filter a Map
def self.filter object, block
raise "Builtin filter() is not implemented yet"
def self.filter object, &block
#TODO investigate break and continue with filter as traverse workflow is different for ruby
if object.is_a?(Array) || object.is_a?(Hash)
object.select &block
else
return nil
end
end

# find() YCP built-in
Expand Down
14 changes: 14 additions & 0 deletions tests/ruby/builtins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,18 @@ def test_lookup
assert_equal 1, YCP::Builtins.lookup({"a" => 1}, "a", 2)
assert_equal 2, YCP::Builtins.lookup({"a" => 1}, "b", 2)
end

def test_filter_list
assert_equal nil, YCP::Builtins.filter(nil)
assert_equal [2,3,4], YCP::Builtins.filter([2,3,4]) {|i| next true }
assert_equal [4], YCP::Builtins.filter([2,3,4]){ |i| next i>3 }
assert_equal [], YCP::Builtins.filter([2,3,4]){ |i| next i>4 }
end

def test_filter_map
test_hash = {2=>3,3=>4}
assert_equal Hash[2=>3,3=>4], YCP::Builtins.filter(test_hash) {|i,j| next true }
assert_equal Hash[3=>4], YCP::Builtins.filter(test_hash){ |i,j| next i>2 }
assert_equal Hash.new, YCP::Builtins.filter(test_hash){ |i,j| next i>4 }
end
end

0 comments on commit 5277099

Please sign in to comment.