message-driven function: more explanation
- To: mathgroup at smc.vnet.net
- Subject: [mg95603] message-driven function: more explanation
- From: "Bert Aerts (rm X)" <bert.aertsX at advalvas.beX>
- Date: Thu, 22 Jan 2009 07:03:16 -0500 (EST)
In the book Programming Paradigms with Mathematica http://library.wolfram.com/infocenter/MathSource/1847/ in the notebook Hw21.nb there is following function: Clear[makeCounter]; makeCounter::usage="makeCounter[] returns a message-driven function which manipulates a \"hidden\" counter variable. The initial value of the counter variable may be set via an optional argument to makeCounter (default is 0). Valid messages for the function are \"++\" (increment), \"--\" (decrement), \"value\", and \"reset\". For example, ctr = makeCounter[100]; ctr[\"value\"] returns 100."; makeCounter[ defaultValue_Integer:0 ] := Module[ { counter = defaultValue, changeCounter }, changeCounter[msg_String:"++"] := Switch[msg, "++", counter = counter + 1; counter, "--", counter = counter - 1; counter, "value", counter, "reset", counter = defaultValue; counter, _, Print["Unknown message to counter:", msg]; counter ]; changeCounter ]; ctr = makeCounter[100] ctr["value"] ctr["++"] gives as output changeCounter$723 100 101 Where can I find more explanation of this type of "message-driven function"?