Class: Enumerator::Lazy

Inherits:
Enumerator show all
Defined in:
opal/opal/corelib/enumerator.rb

Defined Under Namespace

Classes: StopLazyError

Instance Method Summary collapse

Methods inherited from Enumerator

#each, for, #size, #with_index

Methods included from Enumerable

#all?, #any?, #chunk, #count, #cycle, #detect, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #find_index, #first, #group_by, #include?, #inject, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #slice_before, #sort, #sort_by, #zip

Constructor Details

#initialize(object, size = nil, &block) ⇒ Lazy

Returns a new instance of Lazy



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'opal/opal/corelib/enumerator.rb', line 163

def initialize(object, size = nil, &block)
  unless block_given?
    raise ArgumentError, 'tried to call lazy new without a block'
  end

  @enumerator = object

  super size do |yielder, *each_args|
    begin
      object.each(*each_args) {|*args|
        %x{
          args.unshift(#{yielder});

          if (Opal.yieldX(block, args) === $breaker) {
            return $breaker;
          }
        }
      }
    rescue Exception
      nil
    end
  end
end

Instance Method Details

#collect(&block) ⇒ Object Also known as: map



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'opal/opal/corelib/enumerator.rb', line 193

def collect(&block)
  unless block
    raise ArgumentError, 'tried to call lazy map without a block'
  end

  Lazy.new(self, enumerator_size) {|enum, *args|
    %x{
      var value = Opal.yieldX(block, args);

      if (value === $breaker) {
        return $breaker;
      }

      #{enum.yield `value`};
    }
  }
end

#collect_concat(&block) ⇒ Object Also known as: flat_map



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'opal/opal/corelib/enumerator.rb', line 211

def collect_concat(&block)
  unless block
    raise ArgumentError, 'tried to call lazy map without a block'
  end

  Lazy.new(self, nil) {|enum, *args|
    %x{
      var value = Opal.yieldX(block, args);

      if (value === $breaker) {
        return $breaker;
      }

      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

#drop(n) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'opal/opal/corelib/enumerator.rb', line 241

def drop(n)
  n = Opal.coerce_to n, Integer, :to_int

  if n < 0
    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) {|enum, *args|
    if dropped < n
      dropped += 1
    else
      enum.yield(*args)
    end
  }
end

#drop_while(&block) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'opal/opal/corelib/enumerator.rb', line 265

def drop_while(&block)
  unless block
    raise ArgumentError, 'tried to call lazy drop_while without a block'
  end

  succeeding = true
  Lazy.new(self, nil) {|enum, *args|
    if succeeding
      %x{
        var value = Opal.yieldX(block, args);

        if (value === $breaker) {
          return $breaker;
        }

        if (#{Opal.falsy?(`value`)}) {
          succeeding = false;

          #{enum.yield(*args)};
        }
      }
    else
      enum.yield(*args)
    end
  }
end

#enum_for(method = :each, *args, &block) ⇒ Object Also known as: to_enum



292
293
294
# File 'opal/opal/corelib/enumerator.rb', line 292

def enum_for(method = :each, *args, &block)
  self.class.for(self, method, *args, &block)
end

#find_all(&block) ⇒ Object Also known as: select



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'opal/opal/corelib/enumerator.rb', line 296

def find_all(&block)
  unless block
    raise ArgumentError, 'tried to call lazy select without a block'
  end

  Lazy.new(self, nil) {|enum, *args|
    %x{
      var value = Opal.yieldX(block, args);

      if (value === $breaker) {
        return $breaker;
      }

      if (#{Opal.truthy?(`value`)}) {
        #{enum.yield(*args)};
      }
    }
  }
end

#grep(pattern, &block) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'opal/opal/corelib/enumerator.rb', line 318

def grep(pattern, &block)
  if block
    Lazy.new(self, nil) {|enum, *args|
      %x{
        var param = #{Opal.destructure(args)},
            value = #{pattern === `param`};

        if (#{Opal.truthy?(`value`)}) {
          value = Opal.yield1(block, param);

          if (value === $breaker) {
            return $breaker;
          }

          #{enum.yield `Opal.yield1(block, param)`};
        }
      }
    }
  else
    Lazy.new(self, nil) {|enum, *args|
      %x{
        var param = #{Opal.destructure(args)},
            value = #{pattern === `param`};

        if (#{Opal.truthy?(`value`)}) {
          #{enum.yield `param`};
        }
      }
    }
  end
end

#inspectObject



424
425
426
# File 'opal/opal/corelib/enumerator.rb', line 424

def inspect
  "#<#{self.class}: #{@enumerator.inspect}>"
end

#lazyObject



189
190
191
# File 'opal/opal/corelib/enumerator.rb', line 189

def lazy
  self
end

#reject(&block) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'opal/opal/corelib/enumerator.rb', line 354

def reject(&block)
  unless block
    raise ArgumentError, 'tried to call lazy reject without a block'
  end

  Lazy.new(self, nil) {|enum, *args|
    %x{
      var value = Opal.yieldX(block, args);

      if (value === $breaker) {
        return $breaker;
      }

      if (#{Opal.falsy?(`value`)}) {
        #{enum.yield(*args)};
      }
    }
  }
end

#take(n) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'opal/opal/corelib/enumerator.rb', line 374

def take(n)
  n = Opal.coerce_to n, Integer, :to_int

  if n < 0
    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) {|enum, *args|
    if taken < n
      enum.yield(*args)
      taken += 1
    else
      raise StopLazyError
    end
  }
end

#take_while(&block) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'opal/opal/corelib/enumerator.rb', line 399

def take_while(&block)
  unless block
    raise ArgumentError, 'tried to call lazy take_while without a block'
  end

  Lazy.new(self, nil) {|enum, *args|
    %x{
      var value = Opal.yieldX(block, args);

      if (value === $breaker) {
        return $breaker;
      }

      if (#{Opal.truthy?(`value`)}) {
        #{enum.yield(*args)};
      }
      else {
        #{raise StopLazyError};
      }
    }
  }
end