Skip to content

Instantly share code, notes, and snippets.

@prednaz
Created July 10, 2020 17:13
Show Gist options
  • Save prednaz/10e943924c5cc4be4b16803a65385a8b to your computer and use it in GitHub Desktop.
Save prednaz/10e943924c5cc4be4b16803a65385a8b to your computer and use it in GitHub Desktop.
{-# language BangPatterns #-}
module Main where
import Debug.Trace (trace)
data Source = Source Char Bool
data Intermediate = Intermediate !Char !Bool
data Destination = Destination Char Bool
process1 :: Source -> Intermediate
process1 (Source char bool) = Intermediate char bool -- or do something more complicated
process2 :: Intermediate -> Destination
process2 (Intermediate char bool) = Destination char bool -- or do something more complicated
-- put some data into a lazy type so we will not do any unnecessary expensive computation
source :: Source
source = Source 'c' (trace "expensive computation" False)
destination :: Destination
destination = process2 (process1 source)
result :: Char
result = case destination of Destination char _bool -> char
-- destination is lazy, so let's only get and pay for what we need, right?
main :: IO ()
main = print result
-- output:
-- expensive computation
-- 'c'
@prednaz
Copy link
Author

prednaz commented Jul 10, 2020

Making data strict comes with the same risks as making functions strict by adding seq, $!, bang parameters,..., namely unnecessary computation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment