Skip to content

Instantly share code, notes, and snippets.

@noughtmare
Created November 8, 2023 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noughtmare/a41f3e822210245d1880d3f397f282aa to your computer and use it in GitHub Desktop.
Save noughtmare/a41f3e822210245d1880d3f397f282aa to your computer and use it in GitHub Desktop.
Why no strict until'?
import Prelude hiding (until)
import Test.Tasty.Bench
{-# NOINLINE until #-}
until :: (a -> Bool) -> (a -> a) -> a -> a
until p f = go
where
go x | p x = x
| otherwise = go (f x)
{-# NOINLINE until' #-}
until' :: (a -> Bool) -> (a -> a) -> a -> a
until' p f = go
where
go !x | p x = x
| otherwise = go (f x)
main = defaultMain
[ bench "until" (nf (until (> 1000) (+ 1)) (0 :: Int))
, bench "until'" (nf (until' (> 1000) (+ 1)) (0 :: Int))
]
-- Results:
--
-- until: OK
-- 11.0 μs ± 698 ns, 47 KB allocated, 2 B copied, 6.0 MB peak memory
-- until': OK
-- 5.68 μs ± 297 ns, 16 KB allocated, 0 B copied, 6.0 MB peak memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment