<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "coroutine"]]></title>
		<link>http://www.lua.ru/forum/posts/list/1.page</link>
		<description><![CDATA[Latest messages posted in the topic "coroutine"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>coroutine</title>
				<description><![CDATA[ Опять хочеться наглядности <img src="http://www.lua.ru/forum//images/smilies/3b63d1616c5dfcf29f8a7a031aaa7cad.gif"/><br /> Покажите пример, который<br /> по заданому натуральному ряду 1,2,3...n<br /> считает и возвращает в одном мнимом потоке числа фибоначчи F(1)=1, F(2)=1, F(n+1)=F(n)+F(n-1)<br /> а в другом факториал числа f(1)=1, f(2)=2, f(n)=n*f(n-1)<br /> спасибо.]]></description>
				<guid isPermaLink="true">http://www.lua.ru/forum/posts/preList/67/508.page</guid>
				<link>http://www.lua.ru/forum/posts/preList/67/508.page</link>
				<pubDate><![CDATA[Tue, 15 Apr 2008 20:55:33]]> GMT</pubDate>
				<author><![CDATA[ al]]></author>
			</item>
			<item>
				<title>Re:coroutine</title>
				<description><![CDATA[ Я не уверен, что я точно понял, чего вам хочется. Вот:  функция [b]stream_maker[/b] делает бесконечный ленивый поток поток натуральных чисел, а функция [b]lift [/b] лифтит две скалярные унарные функции и исходный поток до бесконечного потока пар значений функций.<br /> <br /> [code]<br /> local wrap  = coroutine.wrap<br /> local yield = coroutine.yield<br /> <br /> local function stream_maker(from)<br />   local function N(n) yield(n) return  N(n+1) end<br />   return wrap(function() return N(from) end)<br /> end<br /> <br /> local function print_first_n(n,stream)<br />   for i=1,n do<br />     local a,b = stream()<br />     print(string.format("%-5d %-20g %d\n",i,a,b))<br />   end<br /> end<br /> <br /> local function lift(foo1,foo2,stream)<br />   local function loop()<br />     local el = stream()<br />     yield(foo1(el),foo2(el))<br />     return loop()<br />   end<br />   return wrap(function() return loop() end)<br /> end<br /> <br /> --// TEST: ------------------------------------------------------<br /> local function fact(x)<br />   local function loop(acc,n)<br />     if n == 1 then return acc else return loop(acc*n,n-1) end<br />   end<br />   return loop(1,x)<br /> end<br /> <br /> local function fib(x)<br />   local function loop(n,n1,n2)<br />     if n == 3 then return n1+n2 else return loop(n-1,n2,n1+n2) end<br />   end<br />   if x &lt; 3 then return 1 else return loop(x,1,1) end<br /> end<br /> <br /> print_first_n(20,lift(fact,fib,stream_maker(1)))<br /> <br /> [/code]<br /> <br /> печатает:<br /> [code]<br /> bash$ lua coro.hlua <br /> 1     1                    1<br /> <br /> 2     2                    1<br /> <br /> 3     6                    2<br /> <br /> 4     24                   3<br /> <br /> 5     120                  5<br /> <br /> 6     720                  8<br /> <br /> 7     5040                 13<br /> <br /> 8     40320                21<br /> <br /> 9     362880               34<br /> <br /> 10    3.6288e+006          55<br /> <br /> 11    3.99168e+007         89<br /> <br /> 12    4.79002e+008         144<br /> <br /> 13    6.22702e+009         233<br /> <br /> 14    8.71783e+010         377<br /> <br /> 15    1.30767e+012         610<br /> <br /> 16    2.09228e+013         987<br /> <br /> 17    3.55687e+014         1597<br /> <br /> 18    6.40237e+015         2584<br /> <br /> 19    1.21645e+017         4181<br /> <br /> 20    2.4329e+018          6765<br /> <br /> [/code]]]></description>
				<guid isPermaLink="true">http://www.lua.ru/forum/posts/preList/67/509.page</guid>
				<link>http://www.lua.ru/forum/posts/preList/67/509.page</link>
				<pubDate><![CDATA[Wed, 16 Apr 2008 00:52:26]]> GMT</pubDate>
				<author><![CDATA[ z00n]]></author>
			</item>
			<item>
				<title>Re:coroutine</title>
				<description><![CDATA[ Да, спасибо, принцип понял.]]></description>
				<guid isPermaLink="true">http://www.lua.ru/forum/posts/preList/67/513.page</guid>
				<link>http://www.lua.ru/forum/posts/preList/67/513.page</link>
				<pubDate><![CDATA[Wed, 16 Apr 2008 14:13:03]]> GMT</pubDate>
				<author><![CDATA[ al]]></author>
			</item>
			<item>
				<title>Re:coroutine</title>
				<description><![CDATA[ А вот так, по-моему, понятнее:<br /> [code]local n,pre=1,1<br /> coroutine_factorial=coroutine.wrap(<br /> 	function(a)<br /> 		while true do<br /> 			n,pre=n+1,pre*n<br /> 			coroutine.yield(pre)<br /> 		end<br /> 	end<br /> )<br /> <br /> local n,pre1,pre2=1,0,1<br /> coroutine_fibonacci=coroutine.wrap(<br /> 	function(a)<br /> 		while true do<br /> 			n,pre1,pre2=n+1,pre1+pre2,pre1<br /> 			coroutine.yield(pre1)<br /> 		end<br /> 	end<br /> )<br /> <br /> print('factorial','fibonacci')<br /> for i=1,10 do<br /> 	print(coroutine_factorial(),coroutine_fibonacci())<br /> end<br /> <br /> print(coroutine_factorial())<br /> print(coroutine_factorial())<br /> print('',coroutine_fibonacci())<br /> print('',coroutine_fibonacci())[/code]]]></description>
				<guid isPermaLink="true">http://www.lua.ru/forum/posts/preList/67/515.page</guid>
				<link>http://www.lua.ru/forum/posts/preList/67/515.page</link>
				<pubDate><![CDATA[Sat, 19 Apr 2008 12:27:08]]> GMT</pubDate>
				<author><![CDATA[ XNut]]></author>
			</item>
	</channel>
</rss>
