# Ruby code, ignore the thread-unsafety for now.
class Ticker
@@tick = 0
def self.update!
@@tick += 1
end
def self.current
return @@tick
end
end
Ticker.update! # 1
Ticker.update! # 2
Ticker.current # 2
--
Joe Van Dyk
http://fixieconsulting.com
_______________________________________________
Beginners mailing list
Begi...@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
If you really want a global ticker, use the State monad:
tick :: State Int ()
tick = modify (+1)
-- increment the counter twice then return its new value
mainStuff :: State Int Int
mainStuff = tick >> tick >> get
-Brent
Hi Joe,
The Ticker class is an example of stateful computation, which we try
to avoid in Haskell. What are you really trying to do?
* If you a number, and you want to add to it, (+1) works
plenty well.
* If you are going to be making ticks, but you don't care about
reading the ticks until the end of the computation, the Writer monad
is of interest.
* If you need to update the tick count and read it, the State
monad is a good bet.
* If you're interested in curiosities from imperative land, STRef, IORef and
other similar creatures may be helpful.
Cheers,
Edward
It's not personal.
John Velman.
>
>
> --
> Joe Van Dyk
> http://fixieconsulting.com
Feels like it though. :(