archives

Monitoring whether a function happens

I'm fairly new to functional programming. I found myself gravitating towards it without knowing what it was (oddly enough, I did the same with object-orientated programming about 10 years ago).

I'm most interested in avoiding destructive updates, but there's something I can't get.

In a test, I want to know whether a particular function has been run. I'm not sure how to go about that.

In the Clean way, the function that calls that function would return a new World (I think) that somehow returns 'yes' when asked whether that function has been run.

In the Haskell way, I would use monads to emulate a mutable variable (I think).

I'm actually using Java, the program is event-driven (Swing). I think perhaps the World-passing metaphor is invalid because I don't control the code that calls my event handlers (and they ultimately return nothing [void]) anyway.

The actual case is that when a particular label on the screen is changed, I want it to flash. I have a little animation routine that changes its background colour appropriately, but in one case, it doesn't happen. So my test should detect this. I can only see an imperative way of solving that - by adding a PropertyChangeListener that modifies an array element or equivalent to determine whether the 'background' property was changed.

Is there another way - a suggested non-imperative way? I don't mind if the suggestion isn't related to Java, or if it ultimately is invalid in Java - I'm really more interested in the concepts at the moment - obviously I can do imperative stuff in Java to make this test fail (and then fix the bug so that it passes).