Class: Transformer::Lazy

Inherits:
Object show all
Defined in:
lib/coroutines/base.rb

Defined Under Namespace

Classes: Yielder

Instance Method Summary collapse

Constructor Details

#initialize(trans) ⇒ Lazy

Returns a new instance of Lazy.



247
248
249
# File 'lib/coroutines/base.rb', line 247

def initialize(trans)
	@trans = trans.instance_variable_get :@self
end

Instance Method Details

#countObject



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/coroutines/base.rb', line 271

def count
	Consumer.new do |y|
		yy = Yielder.new y
		n = 0
		yy.define_yield do |*values|
			n += 1
			yy
		end
		@trans.call yy
		n
	end
end

#drop(n) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/coroutines/base.rb', line 284

def drop(n)
	Transformer.new do |y|
		yy = Yielder.new y
		to_drop = n
		yy.define_yield do |*values|
			if to_drop > 0
				to_drop -= 1
			else
				y.yield(*values)
			end
			yy
		end
		@trans.call yy
	end
end

#drop_while(&block) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/coroutines/base.rb', line 300

def drop_while(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		dropping = true
		yy.define_yield do |*values|
			if dropping
				if not block.call(*values)
					dropping = false
					y.yield(*values)
				end
			else
				y.yield(*values)
			end
			yy
		end
		@trans.call yy
	end
end

#each(&block) ⇒ Object



319
320
321
322
323
324
325
326
327
328
# File 'lib/coroutines/base.rb', line 319

def each(&block)
	Consumer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			block.call(*values)
			yy
		end
		@trans.call yy
	end
end

#filter_map(&block) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/coroutines/base.rb', line 330

def filter_map(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			x = block.call(*values)
			y.yield(x) unless x.nil?
			yy
		end
		@trans.call yy
	end
end

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



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/coroutines/base.rb', line 342

def flat_map(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			x = block.call(*values)
			if x.respond_to? :to_ary
				x.to_ary.each{|xx| y.yield xx }
			else
				y.yield x
			end
			yy
		end
		@trans.call yy
	end
end

#lazyObject



251
252
253
# File 'lib/coroutines/base.rb', line 251

def lazy
	self
end

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



359
360
361
362
363
364
365
366
367
368
# File 'lib/coroutines/base.rb', line 359

def map(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			y.yield(block.call(*values))
			yy
		end
		@trans.call yy
	end
end

#out_connect(other) ⇒ Object



371
372
373
# File 'lib/coroutines/base.rb', line 371

def out_connect(other)
	Transformer.new(&@trans).out_connect(other)
end

#reduce(*args) ⇒ Object Also known as: inject

Raises:

  • (ArgumentError)


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/coroutines/base.rb', line 375

def reduce(*args)
	if not block_given?
		if args.size == 1
			return reduce(&args[0].to_proc)
		elsif args.size == 2
			return reduce(args[0], &args[1].to_proc)
		else
			raise ArgumentError, "wrong number of arguments"
		end
	end
	raise ArgumentError, "wrong number of arguments" if args.size > 1
	block = proc

	memo = if args.empty? then nil else args[0] end
	Consumer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |value|
			if memo.nil?
				memo = value
			else
				memo = block.call memo, value
			end
			yy
		end
		@trans.call yy
		memo
	end
end

#reject(&block) ⇒ Object



405
406
407
408
409
410
411
412
413
414
# File 'lib/coroutines/base.rb', line 405

def reject(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			y.yield(*values) unless block.call(*values)
			yy
		end
		@trans.call yy
	end
end

#select(&block) ⇒ Object



416
417
418
419
420
421
422
423
424
425
# File 'lib/coroutines/base.rb', line 416

def select(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			y.yield(*values) if block.call(*values)
			yy
		end
		@trans.call yy
	end
end

#sortObject



459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/coroutines/base.rb', line 459

def sort
	Consumer.new do |y|
		yy = Yielder.new y
		result = []
		yy.define_yield do |value|
			result << value
			yy
		end
		@trans.call yy
		result.sort
	end
end

#sort_by(&block) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/coroutines/base.rb', line 472

def sort_by(&block)
	Consumer.new do |y|
		yy = Yielder.new y
		result = []
		yy.define_yield do |value|
			result << value
			yy
		end
		@trans.call yy
		result.sort_by(&block)
	end
end

#take(n) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/coroutines/base.rb', line 427

def take(n)
	Transformer.new do |y|
		yy = Yielder.new y
		to_take = n
		yy.define_yield do |*values|
			if to_take > 0
				y.yield(*values)
				to_take -= 1
			else
				raise StopIteration
			end
			yy
		end
		@trans.call yy
	end
end

#take_while(&block) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/coroutines/base.rb', line 444

def take_while(&block)
	Transformer.new do |y|
		yy = Yielder.new y
		yy.define_yield do |*values|
			if block.call(*values)
				y.yield(*values)
			else
				raise StopIteration
			end
			yy
		end
		@trans.call yy
	end
end

#to_aObject



485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/coroutines/base.rb', line 485

def to_a
	Consumer.new do |y|
		yy = Yielder.new y
		result = []
		yy.define_yield do |value|
			result << value
			yy
		end
		@trans.call yy
		result
	end
end

#to_hObject



498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/coroutines/base.rb', line 498

def to_h
	Consumer.new do |y|
		yy = Yielder.new y
		result = {}
		yy.define_yield do |value|
			result[value[0]] = value[1]
			yy
		end
		@trans.call yy
		result
	end
end