From 2b3f9c398ecaed917d13a78e7481c99b50acb612 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 15 Dec 2016 14:00:03 +0200 Subject: [PATCH] add find method to indexed --- lib/typed/parfait/indexed.rb | 10 ++++++++++ test/typed/parfait/test_list.rb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/typed/parfait/indexed.rb b/lib/typed/parfait/indexed.rb index b9e65b70..2f915b5e 100644 --- a/lib/typed/parfait/indexed.rb +++ b/lib/typed/parfait/indexed.rb @@ -133,6 +133,16 @@ module Parfait self end + def find + index = 1 + while index <= self.get_length + item = get(index) + return item if yield item + index = index + 1 + end + return nil + end + def set_length len was = self.get_length return if was == len diff --git a/test/typed/parfait/test_list.rb b/test/typed/parfait/test_list.rb index dc4b9547..f616e1f4 100644 --- a/test/typed/parfait/test_list.rb +++ b/test/typed/parfait/test_list.rb @@ -137,6 +137,15 @@ class TestList < MiniTest::Test end assert_equal 2 , counter end + def test_find + @list.set(1,1) + @list.set(2,2) + assert_equal 2, @list.find{|i| i == 2} + end + def test_not_find + @list.set(1,1) + assert_nil @list.find{|i| i == 3} + end def test_delete_at test_many_get assert @list.delete_at 2