-
Notifications
You must be signed in to change notification settings - Fork 29
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 Unit #105
Comments
instance NFData1 [] where
liftRnf f = foldr (\x r -> f x `seq` r) () |
That's not the point. The aim is to make it easier for other people to define instances on their structures, for which they already have a fold. |
Is it impossible to use |
|
Are you sure that a strict |
Sure, if you'd like proof, here are some benchmarks. Source codeimport Control.DeepSeq
import Data.Map (Map)
import qualified Data.Map as M
import Test.Tasty.Bench -- requires tasty-bench
main :: IO ()
main = defaultMain
[ env (pure m_) $ \m -> bgroup ""
[ bench "rnfFoldr" $ whnf rnfFoldr m
, bcompare "rnfFoldr" $ bench "rnfFoldMap" $ whnf rnfFoldMap m
]
]
where
m_ = M.fromList [(i,i) | i <- [1..100000]] :: Map Int Int
rnfFoldr :: (NFData k, NFData a) => Map k a -> ()
rnfFoldr = M.foldrWithKey (\k x z -> rnf k `seq` rnf x `seq` z) ()
rnfFoldMap :: (NFData k, NFData a) => Map k a -> ()
rnfFoldMap = runUnit . M.foldMapWithKey (\k x -> Unit (rnf k `seq` rnf x))
newtype Unit = Unit { runUnit :: () }
instance Semigroup Unit where
(<>) = seq
instance Monoid Unit where
mempty = Unit () With GHC 9.6.3 and -O, I get
I should also mention that providing monoids that represent some binary operation is not unusual. See the various newtypes provided in |
I wouldn't be opposed to a PR closing #18, as long as the code continues to support the same versions. |
I'll make a PR to add the Personally I don't feel inclined to add
|
Please consider adding a
Unit
type:This makes it possible to writes instances conveniently using folds. Some examples:
See related: #18
Unit
here is theUnitLTR
type defined there. However, #18 only uses it internally. This issue is about exporting the type so users can use it freely.The text was updated successfully, but these errors were encountered: