Hacker News new | ask | show | jobs
by tawgx 4849 days ago
If Java programmers would only apply the concept of immutability the world be such a better place... Every time I log from within a setter to see who's calling this and when, a little fairy falls from the sky.
1 comments

I have hacked Haskell just to be able to see what goes on inside a particular method chain.
Do you mean you have trouble with seeing what's going on inside a long function composition? I wrote a cool function to examine it with almost no code changes:

  module TraceCompose where

  import Debug.Trace

  -- | Trace a value, resulting in the value itself
  idTrace :: Show a => a -> a
  idTrace x = trace (show x) x

  traceCompose :: (Show a, Show b, Show c) =>
    (b -> c) -> (a -> b) -> a -> c
  traceCompose f g = h f . h g . h id
    where h f x = idTrace (f $ seq x x)

  -- Replace "normal" function composition using traceCompose.
  test = (+1) . (*10) $ 4
    where (.) = traceCompose
When you run "test", it will print:

4 40 41

(i.e., the outputs of each function at each point in the composition in order of execution.)

We call this trading one hell (direct mutability) for another worse hell (Indirect mutability).