Created
June 12, 2014 18:00
-
-
Save davetapley/8130eca1c9386cba6935 to your computer and use it in GitHub Desktop.
Array Iter in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArrayIter < Array | |
| class Iter < SimpleDelegator | |
| attr_reader :next | |
| attr_reader :prev | |
| end | |
| def <<(other) | |
| last.instance_variable_set(:@next, other) if last | |
| other_iter = Iter.new other | |
| other_iter.instance_variable_set(:@prev, last) if last | |
| super other_iter | |
| end | |
| def push(other) | |
| throw 'push not supported' | |
| end | |
| def concat(other) | |
| return if other.empty? | |
| last.instance_variable_set(:@next, other.first) if last | |
| other.first.instance_variable_set(:@prev, last) if last | |
| super other | |
| end | |
| def map! | |
| # not sure | |
| super | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative './test_helper' | |
| require './lib/array_iter.rb' | |
| class TestArrayIter < Minitest::Test | |
| def test_empty | |
| assert_empty ArrayIter.new | |
| end | |
| def test_one | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| assert_equal 1, array_iter.size | |
| assert_equal nil, array_iter.first.prev | |
| assert_equal nil, array_iter.first.next | |
| end | |
| def test_two | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| array_iter << 'b' | |
| assert_equal 2, array_iter.size | |
| assert_equal 'a', array_iter.first | |
| assert_equal nil, array_iter.first.prev | |
| assert_equal 'b', array_iter.first.next | |
| assert_equal 'b', array_iter.last | |
| assert_equal 'a', array_iter.last.prev | |
| assert_equal nil, array_iter.last.next | |
| assert_equal 97, array_iter.first.ord | |
| assert_equal "apple", array_iter.first.concat('pple') | |
| assert_equal "apple", array_iter.first | |
| assert_equal "apple", array_iter.last.prev | |
| end | |
| def test_three | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| array_iter << 'b' | |
| array_iter << 'c' | |
| assert_equal 3, array_iter.size | |
| assert_equal 'a', array_iter.first | |
| assert_equal nil, array_iter.first.prev | |
| assert_equal 'b', array_iter.first.next | |
| assert_equal 'b', array_iter.at(1) | |
| assert_equal 'a', array_iter.at(1).prev | |
| assert_equal 'c', array_iter.at(1).next | |
| assert_equal 'c', array_iter.last | |
| assert_equal 'b', array_iter.last.prev | |
| assert_equal nil, array_iter.last.next | |
| end | |
| def test_dup | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| array_iter << 'b' | |
| array_iter_dup = array_iter.dup | |
| assert_equal 'a', array_iter_dup.first | |
| assert_equal array_iter.first, array_iter_dup.first | |
| assert_equal nil, array_iter_dup.first.prev | |
| assert_equal 'b', array_iter_dup.first.next | |
| end | |
| def test_select! | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| array_iter << 'b' | |
| array_iter << 'c' | |
| array_iter.select! { |x| ['a', 'c'].include? x } | |
| assert_equal 2, array_iter.size | |
| assert_equal 'a', array_iter.first | |
| assert_equal nil, array_iter.first.prev | |
| assert_equal 'b', array_iter.first.next | |
| assert_equal 'c', array_iter.last | |
| assert_equal 'b', array_iter.last.prev | |
| assert_equal nil, array_iter.last.next | |
| end | |
| def test_concat | |
| array_iter_one = ArrayIter.new | |
| array_iter_one << 'a' | |
| array_iter_one << 'b' | |
| array_iter_two = ArrayIter.new | |
| array_iter_two << 'y' | |
| array_iter_two << 'z' | |
| array_iter_one.concat array_iter_two | |
| assert_equal 4, array_iter_one.size | |
| assert_equal 'a', array_iter_one.first | |
| assert_equal nil, array_iter_one.first.prev | |
| assert_equal 'b', array_iter_one.first.next | |
| assert_equal 'b', array_iter_one.at(1) | |
| assert_equal 'a', array_iter_one.at(1).prev | |
| assert_equal 'y', array_iter_one.at(1).next | |
| assert_equal 'z', array_iter_one.last | |
| assert_equal 'y', array_iter_one.last.prev | |
| assert_equal nil, array_iter_one.last.next | |
| end | |
| def test_map! | |
| array_iter = ArrayIter.new | |
| array_iter << 'a' | |
| array_iter << 'b' | |
| array_iter.map! { |x| x.upcase } | |
| assert_equal 2, array_iter.size | |
| assert_equal 'A', array_iter.first | |
| assert_equal nil, array_iter.first.prev | |
| assert_equal 'B', array_iter.first.next | |
| assert_equal 'B', array_iter.last | |
| assert_equal 'A', array_iter.last.prev | |
| assert_equal nil, array_iter.last.next | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Failing (as expected) on:
I'm just not sure how to re-wrap the updated array with
Iterdelegates.