Class: Functional

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

Defined Under Namespace

Classes: Base, BottomUp, Collect, Compact, DeleteIf, Each, Inject, Map, P, Reduce, Select, To_a, TopDown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Functional.



176
177
178
# File 'lib/functional.rb', line 176

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.



174
175
176
# File 'lib/functional.rb', line 174

def args
  @args
end

#funcObject

Returns the value of attribute func.



174
175
176
# File 'lib/functional.rb', line 174

def func
  @func
end

#nextObject

Returns the value of attribute next.



174
175
176
# File 'lib/functional.rb', line 174

def next
  @next
end

#objObject

Returns the value of attribute obj.



174
175
176
# File 'lib/functional.rb', line 174

def obj
  @obj
end

#stackObject

Returns the value of attribute stack.



174
175
176
# File 'lib/functional.rb', line 174

def stack
  @stack
end

Instance Method Details

#collect(&exe) ⇒ Object



185
186
187
# File 'lib/functional.rb', line 185

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

#compactObject



209
210
211
# File 'lib/functional.rb', line 209

def compact
	push Compact.new
end

#delete_if(&exe) ⇒ Object



205
206
207
# File 'lib/functional.rb', line 205

def delete_if &exe
	push DeleteIf.new( &exe)
end

#each(&exe) ⇒ Object



221
222
223
224
225
226
# File 'lib/functional.rb', line 221

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

#grep(re) ⇒ Object



201
202
203
# File 'lib/functional.rb', line 201

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

#join(deli) ⇒ Object



228
229
230
231
# File 'lib/functional.rb', line 228

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

#map(&exe) ⇒ Object



189
190
191
# File 'lib/functional.rb', line 189

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

#pObject



238
239
240
# File 'lib/functional.rb', line 238

def p
	each {|*a|Kernel.p a}
end

#push(a) ⇒ Object



180
181
182
183
# File 'lib/functional.rb', line 180

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

#reduce(iv, &exe) ⇒ Object



193
194
195
# File 'lib/functional.rb', line 193

def reduce iv, &exe
	push Reduce.new( iv, &exe)
end

#runObject



233
234
235
236
# File 'lib/functional.rb', line 233

def run
	@obj.send @func||:each, *@args, &@next #.method(:call)
	@next.end
end

#select(&exe) ⇒ Object



197
198
199
# File 'lib/functional.rb', line 197

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

#topdown(init, &exe) ⇒ Object



217
218
219
# File 'lib/functional.rb', line 217

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

#updown(init, &exe) ⇒ Object



213
214
215
# File 'lib/functional.rb', line 213

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