From: "Shot (Piotr Szotkowski)" Date: 2009-09-18T04:56:17+09:00 Subject: How can I cache param-based Enumerator call results? --7AUc2qLy4jB3hD7Z Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I=E2=80=99m trying to cache a lazy-evaluated, param-based Enumerator calls. Considering the following code: def arith a, b Enumerator.new do |yielder| ['+', '*'].each do |oper| sleep 1 # simulates a param-based, long-running process yielder.yield [oper, eval("#{a} #{oper} #{b}")] end end end [[1,2], [3,4], [5,6], [1,2], [3,4], [5,6], [7,8]].each do |a, b| arith(a,b).each do |oper, result|=20 puts "#{a} #{oper} #{b} =3D #{result}" end end should take about 14 seconds to run, with arith() lazily returning the sum and product of its params. Still, if you look closely, arith(1,2), arith(3,4) and arith(5,6) are called twice; if there was caching of the yielded values there would be no need to recompute these sums and products (and the whole process would take around eight seconds instead). I=E2=80=99m still rather new to Enumerators, but in my project their lazy evaluation is an amazing benefit. What=E2=80=99s the best/simplest way to introduce caching in the above scenario? (Ideally, I=E2=80=99d love to have the caching done outside the Enumerator, as in my project=E2=80=99s case the arith() method is provided by various interchangeable modules=C2=B9, but if it=E2=80=99s only =E2=80=93 or much e= asier =E2=80=93 doable =66rom inside the Enumerator then so be it, I=E2=80=99ll add it in every implementation=E2=80=A6) =C2=B9 http://github.com/Chastell/art-decomp/blob/master/lib/art-decomp/dec= omposer.rb =E2=80=93 ignoring that #decompositions is itself an Enumerator, what I wan= t is for qv_gen to not recompute blankets if qv_gen.blankets(fsm, u, v, qu) was already called previously with the same params Thanks in advance for any insight in how to solve this problem! =E2=80=94 Shot --=20 IMO, the primary historical significance of Unix is that it marks the time in computer history where CPUs became so cheap that it was possible to build an operating system without adult supervision. [Russ Holsclaw, afc] --7AUc2qLy4jB3hD7Z Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkqylAwACgkQi/mCfdEo8UqDRgCguEBg14lOXvf7gDbY8QoHZLi5 a38An2Ur7u1bHLr2Ma7sBmwrWm23zWHP =5elJ -----END PGP SIGNATURE----- --7AUc2qLy4jB3hD7Z--