Permalink
Browse files

Added in all code and presentation

  • Loading branch information...
1 parent d3d42ce commit 6594a310c33330a7ba6682fe7e1a6550cd8726a7 @MrJaba committed Sep 9, 2010
Binary file not shown.
Binary file not shown.
View
@@ -23,7 +23,8 @@ def walk(dir, parent)
end
end
-end
+end
+
callbacks = {
:directory => lambda{|dir| puts dir },
View
@@ -27,7 +27,7 @@ def scale_factory(factor)
# puts scale_3.call(x)
# end
-#Curried Map function
+#Curried Map function - bit pointless in ruby!
#before
p (1..10).to_a.map {|x| x * 2}
View
@@ -3,24 +3,20 @@
require 'node'
-def upfrom(start)
- Node.new( start, lambda{ upfrom(start+1) } )
+#AFTER
+def upfrom_lazy(start)
+ Node.new( start, lambda{ upfrom_lazy(start+1) } )
end
-def upfrom_list(start)
- Node.new( start, upfrom_list(start+1) )
+#BEFORE
+def upfrom_recursive(start)
+ Node.new( start, upfrom_recursive(start+1) )
end
-#b = upfrom_list(1) #StackLevelTooDeep
-a = upfrom(1)
+#b = upfrom_recursive(1) #StackLevelTooDeep
+a = upfrom_lazy(1)
#Debug statement to iterate over the list
-# while( a ) do
-# puts a.head
-# a = a.tail
-# end
-
-#Replaced with this
while( a ) do
puts a.drop
end
View
@@ -1,35 +1,19 @@
#Practical example - calculating Square Roots using the Newton Raphson method
#Streams to represent Approximate Calculations
-require 'rubygems'
-require 'ruby-debug'
-require 'node'
-
-def solve(initial_guess, function)
- debugger
- iterate_function(initial_guess, lambda{ |g| (g - function.call(g)) / slope(g,function) } )
-end
#Generate a stream where function is applied to each element
def iterate_function(start_value, function)
Node.new(start_value, lambda{ iterate_function( function.call(start_value), function ) })
end
-#From previous example
-def transform(stream, function)
- ns = stream
- Node.new( function.call(ns.head), lambda{ transform( ns.tail, function ) } )
-end
-
-#calculating slope of a tagent line on a curve - called the derivative function
-def slope(x,function)
- point_difference = 0.00000095367431640625
- (function.call(x+point_difference) - function.call(x-point_difference)) / (2*point_difference)
+def sqrt_stream(n)
+ iterate_function(n, lambda{|g| (g*g + n ) / (2*g) }) #Function to improve the guess by calculating the intersection of the x axis of the tangent line
end
#to solve for √2
-#so (x * x) - 2 = 0
-sqrt_2 = solve(1, lambda{|x| x * x - 2.0 })
-
-while( sqrt_2 ) do
+sqrt_2 = sqrt_stream(2.0)
+limit = 10
+while( sqrt_2 && limit > 0 ) do
+ limit -=1
puts sqrt_2.drop
end

0 comments on commit 6594a31

Please sign in to comment.