Class: Transformer::Lazy
Defined Under Namespace
Classes: Yielder
Instance Method Summary collapse
- #count ⇒ Object
- #drop(n) ⇒ Object
- #drop_while(&block) ⇒ Object
- #each(&block) ⇒ Object
- #filter_map(&block) ⇒ Object
- #flat_map(&block) ⇒ Object (also: #collect_concat)
-
#initialize(trans) ⇒ Lazy
constructor
A new instance of Lazy.
- #lazy ⇒ Object
- #map(&block) ⇒ Object (also: #collect)
- #out_connect(other) ⇒ Object
- #reduce(*args) ⇒ Object (also: #inject)
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object
- #sort ⇒ Object
- #sort_by(&block) ⇒ Object
- #take(n) ⇒ Object
- #take_while(&block) ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(trans) ⇒ Lazy
Returns a new instance of Lazy.
246 247 248 |
# File 'lib/coroutines/base.rb', line 246 def initialize(trans) @trans = trans.instance_variable_get :@self end |
Instance Method Details
#count ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/coroutines/base.rb', line 270 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
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/coroutines/base.rb', line 283 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/coroutines/base.rb', line 299 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
318 319 320 321 322 323 324 325 326 327 |
# File 'lib/coroutines/base.rb', line 318 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
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/coroutines/base.rb', line 329 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
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/coroutines/base.rb', line 341 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 |
#lazy ⇒ Object
250 251 252 |
# File 'lib/coroutines/base.rb', line 250 def lazy self end |
#map(&block) ⇒ Object Also known as: collect
358 359 360 361 362 363 364 365 366 367 |
# File 'lib/coroutines/base.rb', line 358 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
370 371 372 |
# File 'lib/coroutines/base.rb', line 370 def out_connect(other) Transformer.new(&@trans).out_connect(other) end |
#reduce(*args) ⇒ Object Also known as: inject
374 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 |
# File 'lib/coroutines/base.rb', line 374 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
404 405 406 407 408 409 410 411 412 413 |
# File 'lib/coroutines/base.rb', line 404 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
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/coroutines/base.rb', line 415 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 |
#sort ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/coroutines/base.rb', line 458 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
471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/coroutines/base.rb', line 471 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
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/coroutines/base.rb', line 426 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
443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/coroutines/base.rb', line 443 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 |