lazy fibs

M

MenTaLguY

--=-O+I53OSM4tWNIBXGoOiJ
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

I've been playing around with adding explicit support for lazy streams
to lazy.rb. Even without explicit support, though, lazy streams aren't
hard. Check this out:

require 'lazy'

def generate( &f )
promise { f.call( generate( &f ) ) }
end

def head( pair ) ; pair[0] ; end

def tail( pair ) ; pair[1] ; end

def fibs
state =3D [ 1, 0 ]
generate { |rest|
value =3D state[0] + state[1]
state[0] =3D state[1]
state[1] =3D value
[ value, rest ]
}
end

blah =3D fibs
20.times do
p head( blah )
blah =3D tail( blah )
end

Nifty, huh?

-mental

--=-O+I53OSM4tWNIBXGoOiJ
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDb0CHcUNIGiXCc4MRAkK1AKCCeSY9U3ca+0L5kcdghWNOlkyS1QCguxjW
X2jMTOtWi2IPKcrNG8TJFnA=
=uXTV
-----END PGP SIGNATURE-----

--=-O+I53OSM4tWNIBXGoOiJ--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top