Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add traceAll #3

Merged
merged 1 commit into from
Jul 2, 2021
Merged

add traceAll #3

merged 1 commit into from
Jul 2, 2021

Conversation

lemastero
Copy link

Hello,
Thank you for lovely ❤️ library. Often I am in a situation where I have tracer for e.g. Transaction

putStrlnTracer :: Tracer IO String
putStrlnTracer = Tracer (\tx -> putStrLn tx)

txTracer :: Tracer IO Tx
txTracer = showTracing putStrlnTracer

and I have Block full of transactions:

data Tx = Tx Int deriving Show
data Block = Block String [Tx] deriving Show

b :: Block
b = Block "001" (map Tx [41,43,47,53,61,71,83])

I know how to get Transactions from Block.

blockTx :: Block -> [Tx]
blockTx (Block hash tx) = tx

I would like to trace all transactions from Block. Result from Tracer is IO () so If I can smash together results. PR contains (generalised) combinator for this:

traceAll :: (Applicative m) => (b -> [a]) -> Tracer m a -> Tracer m b

You can use it like so:

blockTracer :: Tracer IO Block
blockTracer = traceAll blockTx txTracer

traceWith blockTracer b
Tx 41
Tx 43
Tx 47
Tx 53
Tx 61
Tx 71
Tx 83

You could play this game with Maybe or any other Traversable.

@lemastero lemastero requested a review from CodiePP as a code owner June 19, 2021 22:16
@avieth
Copy link
Contributor

avieth commented Jun 20, 2021

Looks good to me, but I would suggest factoring it like this:

traceTraversable :: (Applicative m, Traversable t) => Tracer m a -> Tracer m (t a)
traceTraversable tr = Tracer (void . traverse (runTracer tr))

traceAll :: (Applicative m, Traversable t) => (b -> t a) -> Tracer m a -> Tracer m b
traceAll f = contramap f . traceTraversable

@coot
Copy link
Contributor

coot commented Jun 20, 2021

or maybe we should go one step further:

mapTracer :: ((a -> m ()) -> b -> n ())
          -> Tracer m a -> Tracer n b
mapTracer f (Tracer tr) = Tracer (f tr)

natTracer :: (forall x. m x -> n x)
          -> Tracer m a
          -> Tracer n a
natTracer f = mapTracer (f .)

instance Contravariant (Tracer m) where
  contramap f = mapTracer (. f)
  
contramapM :: Monad m
           => (a -> m b)
           -> Tracer m b
           -> Tracer m a
contramapM f = mapTracer (f >=>)


-- by using `traverse_` we can reduce the constraint of `t` to `Foldable t`.
traceTraversable :: (Applicative m, Foldable t) => Tracer m a -> Tracer m (t a)
traceTraversable = mapTracer traverse_

@lemastero
Copy link
Author

Thank you 🙇 for the review. Applied suggestion with traceTraversable and mapTracer. New functions are exposed.

mapTracer is beautiful in many ways :)

src/Control/Tracer.hs Outdated Show resolved Hide resolved
Copy link
Contributor

@coot coot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@coot coot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small formatting suggestions.

src/Control/Tracer.hs Outdated Show resolved Hide resolved
src/Control/Tracer.hs Outdated Show resolved Hide resolved
@coot
Copy link
Contributor

coot commented Jun 29, 2021

Could you just squash the review commits onto the other ones? Then it's ready to be merged.

@lemastero
Copy link
Author

Could you just squash the review commits onto the other ones? Then it's ready to be merged.

@coot Commits from PR were squashed. Thank you for review.

Copy link
Contributor

@coot coot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution.

@coot coot merged commit 0c9ff65 into input-output-hk:master Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants