billboswell

billboswell

Effective Haskell: Chapter 10 (pages 379-383) - code does not compile

Under “Building a Basic Metrics System with IORefs”, have created a sample file with the code snippets from page 379-383 as follows:


module Metrics where

import qualified Data.Map.Strict as Map
import Data.IORef
import Data.Maybe (fromMaybe)
import Data.Time.Clock
( diffUTCTime
, getCurrentTime
, nominalDiffTimeToSeconds
)

data AppMetrics = AppMetrics
{ successCount :: Int
, failureCount :: Int
, callDuration :: Map.Map String Int
} deriving Show

metrics :: IO (IORef AppMetrics)
metrics = newIORef AppMetrics
{ successCount = 0
, failureCount = 0
, callDuration = Map.empty
}

printMetrics :: IO ()
printMetrics = metrics >>= readIORef >>= print

incrementSuccess :: IO ()
incrementSuccess =
metrics >>= flip modifyIORef incrementSuccess
where
incrementSuccess m =
m { successCount = 1 + successCount m}

successfullyPrintHello :: IO ()
successfullyPrintHello = do
print “Hello”
incrementSuccess

printHelloAndMetrics = do
successfullyPrintHello
printMetrics

newtype Metrics = Metrics { appMetricsStore :: IORef AppMetrics}

newMetrics :: IO Metrics
newMetrics =
let
emptyAppMetrics = AppMetrics
{ successCount = 0
, failureCount = 0
, callDuration = Map.empty
}
in Metrics <$> newIORef emptyAppMetrics

tickSuccess :: Metrics → IO ()
tickSuccess (Metrics metricsRef) = modifyIORef metricsRef $ \m →
m { successCount = 1 + successCount m}

tickFailure :: Metrics → IO ()
tickFailure (Metrics metricsRef) = modifyIORef metricsRef $ \m →
m { failureCount = 1 + failureCount m}

timeFunction :: Metrics → String → IO a → IO a
timeFunction (Metrics metrics) actionName action = do
startTime ← getCurrentTime
result ← action
endTime ← getCurrentTime

modifyIORef metrics $ \oldMetrics -> 
    let
        oldDurationValue = 
            fromMaybe 0 $ Map.lookup actionName (callDuration oldMetrics)

        runDuration = 
            floor . nominalDiffTimeToSeconds $ 
               diffUTCTime endTime startTime
        
        newDurationValue = oldDurationValue + runDuration

    in oldMetrics {
        callDuration = 
            Map.insert actionName newDurationValue $
              callDuration oldMetrics}

    pure result

Code does not compile, throws this error:

The function ‘oldMetrics
{callDuration = Map.insert actionName newDurationValue
$ callDuration oldMetrics}’
is applied to two value arguments,
but its type ‘AppMetrics’ has none

Not able to figure out the problem.

Most Liked

billboswell

billboswell

That did the trick. Amen to fixup on dangling code lines. Loving the book. Thx for speedy reply!

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
New

Latest in Effective Haskell

Effective Haskell Portal