Class: Enumerator.self::Lazy
- Defined in:
- opal/opal/corelib/enumerator/lazy.rb
Class Method Summary collapse
Instance Method Summary collapse
- #collect(&block) ⇒ Object (also: #map)
- #collect_concat(&block) ⇒ Object (also: #flat_map)
- #drop(n) ⇒ Object
- #drop_while(&block) ⇒ Object
- #enum_for(method = :each, *args, &block) ⇒ Object (also: #to_enum)
- #find_all(&block) ⇒ Object (also: #filter, #select)
- #grep(pattern, &block) ⇒ Object
-
#initialize(object, size = nil, &block) ⇒ Lazy
constructor
A new instance of Lazy.
- #inspect ⇒ Object
- #lazy ⇒ Object
- #reject(&block) ⇒ Object
- #take(n) ⇒ Object
- #take_while(&block) ⇒ Object
Constructor Details
#initialize(object, size = nil, &block) ⇒ Lazy
Returns a new instance of Lazy.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 14 def initialize(object, size = nil, &block) `$deny_frozen_access(self)` unless block_given? ::Kernel.raise ::ArgumentError, 'tried to call lazy new without a block' end @enumerator = object super size do |yielder, *each_args| object.each(*each_args) do |*args| %x{ args.unshift(#{yielder}); $yieldX(block, args); } end rescue StopLazyError nil end end |
Class Method Details
.for(object) ⇒ Object
8 9 10 11 12 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 8 def self.for(object, *) lazy = super `lazy.enumerator = object` lazy end |
Instance Method Details
#collect(&block) ⇒ Object Also known as: map
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 40 def collect(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy map without a block' end Lazy.new(self, enumerator_size) do |enum, *args| %x{ var value = $yieldX(block, args); #{enum.yield `value`}; } end end |
#collect_concat(&block) ⇒ Object Also known as: flat_map
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 54 def collect_concat(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy map without a block' end Lazy.new(self, nil) do |enum, *args| %x{ var value = $yieldX(block, args); if (#{`value`.respond_to? :force} && #{`value`.respond_to? :each}) { #{`value`.each { |v| enum.yield v }} } else { var array = #{::Opal.try_convert `value`, ::Array, :to_ary}; if (array === nil) { #{enum.yield `value`}; } else { #{`value`.each { |v| enum.yield v }}; } } } end end |
#drop(n) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 80 def drop(n) n = `$coerce_to(#{n}, #{::Integer}, 'to_int')` if n < 0 ::Kernel.raise ::ArgumentError, 'attempt to drop negative size' end current_size = enumerator_size set_size = if ::Integer === current_size n < current_size ? n : current_size else current_size end dropped = 0 Lazy.new(self, set_size) do |enum, *args| if dropped < n dropped += 1 else enum.yield(*args) end end end |
#drop_while(&block) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 104 def drop_while(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy drop_while without a block' end succeeding = true Lazy.new(self, nil) do |enum, *args| if succeeding %x{ var value = $yieldX(block, args); if (!$truthy(value)) { succeeding = false; #{enum.yield(*args)}; } } else enum.yield(*args) end end end |
#enum_for(method = :each, *args, &block) ⇒ Object Also known as: to_enum
127 128 129 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 127 def enum_for(method = :each, *args, &block) self.class.for(self, method, *args, &block) end |
#find_all(&block) ⇒ Object Also known as: filter, select
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 131 def find_all(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy select without a block' end Lazy.new(self, nil) do |enum, *args| %x{ var value = $yieldX(block, args); if ($truthy(value)) { #{enum.yield(*args)}; } } end end |
#grep(pattern, &block) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 147 def grep(pattern, &block) if block Lazy.new(self, nil) do |enum, *args| %x{ var param = #{::Opal.destructure(args)}, value = #{pattern === `param`}; if ($truthy(value)) { value = $yield1(block, param); #{enum.yield `$yield1(block, param)`}; } } end else Lazy.new(self, nil) do |enum, *args| %x{ var param = #{::Opal.destructure(args)}, value = #{pattern === `param`}; if ($truthy(value)) { #{enum.yield `param`}; } } end end end |
#inspect ⇒ Object
235 236 237 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 235 def inspect "#<#{self.class}: #{@enumerator.inspect}>" end |
#lazy ⇒ Object
36 37 38 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 36 def lazy self end |
#reject(&block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 175 def reject(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy reject without a block' end Lazy.new(self, nil) do |enum, *args| %x{ var value = $yieldX(block, args); if (!$truthy(value)) { #{enum.yield(*args)}; } } end end |
#take(n) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 191 def take(n) n = `$coerce_to(#{n}, #{::Integer}, 'to_int')` if n < 0 ::Kernel.raise ::ArgumentError, 'attempt to take negative size' end current_size = enumerator_size set_size = if ::Integer === current_size n < current_size ? n : current_size else current_size end taken = 0 Lazy.new(self, set_size) do |enum, *args| if taken < n enum.yield(*args) taken += 1 else ::Kernel.raise StopLazyError end end end |
#take_while(&block) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 216 def take_while(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to call lazy take_while without a block' end Lazy.new(self, nil) do |enum, *args| %x{ var value = $yieldX(block, args); if ($truthy(value)) { #{enum.yield(*args)}; } else { #{::Kernel.raise StopLazyError}; } } end end |