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.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 13 def initialize(object, size = nil, &block) 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
7 8 9 10 11 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 7 def self.for(object, *) lazy = super `lazy.enumerator = object` lazy end |
Instance Method Details
#collect(&block) ⇒ Object Also known as: map
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 37 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 51 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 77 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 101 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
124 125 126 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 124 def enum_for(method = :each, *args, &block) self.class.for(self, method, *args, &block) end |
#find_all(&block) ⇒ Object Also known as: filter, select
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 128 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 144 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
232 233 234 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 232 def inspect "#<#{self.class}: #{@enumerator.inspect}>" end |
#lazy ⇒ Object
33 34 35 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 33 def lazy self end |
#reject(&block) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 172 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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 188 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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'opal/opal/corelib/enumerator/lazy.rb', line 213 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 |