Browse thread
Generators/iterators and lazy evaluation?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Richard Jones <rich@a...> |
| Subject: | Re: [Caml-list] Generators/iterators and lazy evaluation? |
On Thu, Apr 05, 2007 at 07:16:52AM +1000, Erik de Castro Lopo wrote:
> One ocaml solution to this is an class/object:
>
> class pow2gen =
> object
> val mutable current = 0
>
> method next () =
> current <-
> if current == 0 then 1
> else current * 2 ;
> current
> end
Potentially anything which keeps state can be used. I much prefer:
let pow2gen =
let current = ref 0 in
fun () ->
current := if !current = 0 then 1 else !current * 2;
!current
Mainly because it can be inserted anywhere within a function.
Rich.
--
Richard Jones
Red Hat