Class: Functional

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/functional.rb

Defined Under Namespace

Classes: Base, BottomUp, Collect, Compact, Cons, DEFAULT, Each, Filter, Flatten, Inject, Map, P, Pager, Reduce, Save, Select, Slice, Tap, ToHash, To_a, TopDown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, func = nil, *args) ⇒ Functional

Returns a new instance of Functional.



337
338
339
# File 'lib/functional.rb', line 337

def initialize obj = nil, func = nil, *args
	@next, @stack, @obj, @func, @args = self, self, obj, func, args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



335
336
337
# File 'lib/functional.rb', line 335

def args
  @args
end

#funcObject

Returns the value of attribute func.



335
336
337
# File 'lib/functional.rb', line 335

def func
  @func
end

#nextObject

Returns the value of attribute next.



335
336
337
# File 'lib/functional.rb', line 335

def next
  @next
end

#objObject

Returns the value of attribute obj.



335
336
337
# File 'lib/functional.rb', line 335

def obj
  @obj
end

#stackObject

Returns the value of attribute stack.



335
336
337
# File 'lib/functional.rb', line 335

def stack
  @stack
end

Class Method Details

.__version__Object



50
# File 'lib/functional.rb', line 50

def self.__version__() '0.1.5' end

Instance Method Details

#collect(&exe) ⇒ Object



346
347
348
# File 'lib/functional.rb', line 346

def collect &exe
	push Collect.new( &exe)
end

#compactObject



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

def compact
	push Compact.new
end

#cons(n, &exe) ⇒ Object



441
442
443
444
# File 'lib/functional.rb', line 441

def cons n, &exe
	push Cons.new( n)
	block_given? ? self.collect( &exe) : self
end

#each(&exe) ⇒ Object



387
388
389
390
391
# File 'lib/functional.rb', line 387

def each &exe
	return self  unless exe
	push Each.new( &exe)
	run
end

#filter(&exe) ⇒ Object Also known as: reject



366
367
368
# File 'lib/functional.rb', line 366

def filter &exe
	push Filter.new( &exe)
end

#flatten(&exe) ⇒ Object



383
384
385
# File 'lib/functional.rb', line 383

def flatten &exe
	push Flatten.new
end

#grep(re) ⇒ Object



362
363
364
# File 'lib/functional.rb', line 362

def grep re
	push Select.new( &re.method( :match))
end

#join(deli) ⇒ Object



398
399
400
401
# File 'lib/functional.rb', line 398

def join deli
	push Inject.new('') {|i,j|i+deli+j}
	run
end

#map(&exe) ⇒ Object



350
351
352
# File 'lib/functional.rb', line 350

def map &exe
	push Map.new( &exe)
end

#pObject



411
412
413
# File 'lib/functional.rb', line 411

def p
	each &Kernel.method( :p)
end

#pager(*opts) ⇒ Object



415
416
417
418
# File 'lib/functional.rb', line 415

def pager *opts
	push Pager.new( *opts)
	run
end

#push(a) ⇒ Object



341
342
343
344
# File 'lib/functional.rb', line 341

def push a
	@stack = @stack.next = a
	self
end

#reduce(iv = ::Functional::DEFAULT, &exe) ⇒ Object



354
355
356
# File 'lib/functional.rb', line 354

def reduce iv = ::Functional::DEFAULT, &exe
	push Reduce.new( iv, &exe)
end

#runObject



403
404
405
406
407
408
409
# File 'lib/functional.rb', line 403

def run
	@obj.send @func||:each, *@args, &@next #.method(:call)
	@next.end
rescue Object
	@next.clean
	raise $!
end

#save(db) ⇒ Object



462
463
464
# File 'lib/functional.rb', line 462

def save db
	push Save.new( db)
end

#select(&exe) ⇒ Object



358
359
360
# File 'lib/functional.rb', line 358

def select &exe
	push Select.new( &exe)
end

#slice(n, &exe) ⇒ Object



436
437
438
439
# File 'lib/functional.rb', line 436

def slice n, &exe
	push Slice.new( n)
	block_given? ? self.collect( &exe) : self
end

#sort(&exe) ⇒ Object



420
421
422
# File 'lib/functional.rb', line 420

def sort &exe
	to_a.sort &exe
end

#sort_by(&exe) ⇒ Object



424
425
426
# File 'lib/functional.rb', line 424

def sort_by &exe
	to_a.sort_by &exe
end

#tap(&exe) ⇒ Object



446
447
448
# File 'lib/functional.rb', line 446

def tap &exe
	push Tap.new( &exe)
end

#to_hashObject



393
394
395
396
# File 'lib/functional.rb', line 393

def to_hash
	push ToHash.new
	run
end

#topdown(init, &exe) ⇒ Object



379
380
381
# File 'lib/functional.rb', line 379

def topdown init, &exe
	push TopDown.new( init, &exe)
end

#updown(init, &exe) ⇒ Object



375
376
377
# File 'lib/functional.rb', line 375

def updown init, &exe
	push UpDown.new( init, &exe)
end

#with_index(&exe) ⇒ Object

_A, _B, …, _C, …, _D

> [ [0, _A], [1, _B], …, [_I, _C], …, [_N, _D]]

[_A|_As], [_B|_Bs], …, [_C|_Cs], …, [_D|_Ds

] ==> [ [0,_A|_As], [1,_B|_Bs], …, [_I,_C|_Cs], …, [_N,_D|_Ds] ]



430
431
432
433
434
# File 'lib/functional.rb', line 430

def with_index &exe
	i = 0
	exe ||= Array.method :[]
	push Collect.new {|*a| exe.call i, *a; i += 1 }
end