As I was refactoring line 38-44, by injecting that anonymous function (\g -> ...), it occured to me that this whole thing looked similar to some well-known monad. Lo and hold - modify of Reader:
modify::MonadStatesm=>(s->s)->m()
So, my code can be rewritten to use State.
The question is: which version is more performant?
actually state still internally does tupling+de-tupling, so maybe your original thing is faster in the end
it also doesn't have the cross-module specialisation issues that using mtl type classes has
I build a list of "edit" functions to modify a graph datastructure. The functions list is evaluated using
compose
, which is defined as:See full code here: https://github.com/srid/ka/blob/b24fc03dee53a91fd11122422405b3be3939eeb0/src/Ka/Graph.hs#L24-L48
As I was refactoring line 38-44, by injecting that anonymous function (
\g -> ...
), it occured to me that this whole thing looked similar to some well-known monad. Lo and hold -modify
of Reader:So, my code can be rewritten to use State.
The question is: which version is more performant?
fok it. State version is easier to read: https://github.com/srid/ka/commit/edd9ff9d6c485d88431064c38d52bbd5aae13109
well it's exactly a state monad, except everywhere you have
()
for your "result" parameter (so it'll bem ()
everywhere)so it should be somewhat same performance? (very questionable claims :D )
actually state still internally does tupling+de-tupling, so maybe your original thing is faster in the end
it also doesn't have the cross-module specialisation issues that using mtl type classes has
I guess we would need to consult Core to know for sure
Because GHC could remove case of known constructor
(I mean, we're talking about compiler that can optimize out (un)boxing, which is very similar)
btw, for your
a -> a
usage, I think you can get away with only aReader
andlocal
Also fyi, I believe
compose
isfoldMap Endo
:eyes:yeah but you need to unwrap it afterwards, and the noise becomes more than just
foldr (.) id
I wish there was something like
sum
is to(+)
for(.)
or we had something like
ApplyingVia
Georgi Lyubenov // googleson78 said:
not sure how that would work