Class: Egison::LazyArray
- Includes:
- Enumerable
- Defined in:
- lib/egison/lazyarray.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #clone ⇒ Object (also: #dup)
- #concat(other) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(org_enum) ⇒ LazyArray
constructor
A new instance of LazyArray.
- #inspect ⇒ Object
- #shift ⇒ Object
- #size ⇒ Object (also: #length)
- #unshift(*obj) ⇒ Object
Constructor Details
#initialize(org_enum) ⇒ LazyArray
Returns a new instance of LazyArray.
76 77 78 79 80 |
# File 'lib/egison/lazyarray.rb', line 76 def initialize(org_enum) @org_enum = OrgEnum.new(org_enum) @cache = [] @terminated = false end |
Instance Method Details
#+(other) ⇒ Object
148 149 150 |
# File 'lib/egison/lazyarray.rb', line 148 def + other clone.concat(other) end |
#clone ⇒ Object Also known as: dup
132 133 134 135 136 137 138 139 |
# File 'lib/egison/lazyarray.rb', line 132 def clone obj = super obj.instance_eval do @org_enum = @org_enum.clone @cache = @cache.clone end obj end |
#concat(other) ⇒ Object
142 143 144 145 146 |
# File 'lib/egison/lazyarray.rb', line 142 def concat other @org_enum.concat(other) @terminated = false self end |
#each(&block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/egison/lazyarray.rb', line 82 def each(&block) return to_enum unless block_given? @cache.each(&block) return if @terminated while true # StopIteration will NOT be raised if `loop do ... end` el = @org_enum.next @cache.push(el) block.(el) end rescue StopIteration => ex @terminated = true end |
#empty? ⇒ Boolean
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/egison/lazyarray.rb', line 115 def empty? return false unless @cache.empty? return true if @terminated begin @cache << @org_enum.next false rescue StopIteration => ex @terminated = true true end end |
#inspect ⇒ Object
152 153 154 |
# File 'lib/egison/lazyarray.rb', line 152 def inspect "\#<#{self.class.name}#{@terminated ? @cache.inspect : "[#{@cache.map(&:inspect).join(', ')}...]"}>" end |
#shift ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/egison/lazyarray.rb', line 95 def shift if @cache.size > 0 @cache.shift elsif @terminated nil else begin @org_enum.next rescue StopIteration => ex @terminated = true nil end end end |
#size ⇒ Object Also known as: length
127 128 129 |
# File 'lib/egison/lazyarray.rb', line 127 def size @terminated ? @cache.size : nil end |
#unshift(*obj) ⇒ Object
110 111 112 113 |
# File 'lib/egison/lazyarray.rb', line 110 def unshift(*obj) @cache.unshift(*obj) self end |