Module: DR::Lambda

Extended by:
Lambda
Included in:
Lambda
Defined in:
lib/dr/base/functional.rb

Instance Method Summary collapse

Instance Method Details

#compose(*f) ⇒ Object

compose a list of functions



32
33
34
35
36
37
38
39
# File 'lib/dr/base/functional.rb', line 32

def compose(*f)
	f.reverse!
	first=f.shift
	return lambda do |*args,&b|
		v=first.call(*args,&b)
		f.reduce(v) {|v,fun| fun.call(v)}
	end
end

#enum_map(f, *cols) ⇒ Object

like map but return an enumerator



22
23
24
25
26
27
28
29
# File 'lib/dr/base/functional.rb', line 22

def enum_map(f,*cols)
	cols=cols.map(&:each)
	Enumerator.new do |y|
		loop do
			y<<f.call(*cols.map(&:next))
		end
	end
end

#map(f, *cols) ⇒ Object

standard ruby: col.map(&f) for one variable Here: Lambda.map(f,col1,col2,...) for several variables Other implementation: (shift cols).zip(cols).map {|a| f.call(*a)} but our implementation stops as soon as a collection is empty whereas the zip implementation use the length of the first collection and pads with nil



11
12
13
14
15
16
17
18
19
# File 'lib/dr/base/functional.rb', line 11

def map(f,*cols)
	cols=cols.map(&:each)
	r=[]
	loop do
		r<<f.call(*cols.map(&:next))
	end
	r
rescue StopIteration
end