Michael Snoyman, Director of Engineering, FP Complete
Twitter tech talk, November 10, 2015
What makes Haskell great for writing network services?
How much memory does it take to store the following? (Assume 64-bit)
data Foo = Foo Int Int
Foo constructor (8 bytes), 2 * (pointer to Int (8), Int constructor (8), raw Int (8)) == 56 bytes
data Foo = Foo {-# UNPACK #-} !Int {-# UNPACK #-} !Int
Foo constructor (8), 2 * (raw Int (8)) == 24 bytes
Note: UNPACK pragmas aren't necessary for small data types, just strictness (!)
Unpacking also works for larger values!
data Foo = Foo !Int !Int
data Bar = Bar {-# UNPACK #-} !Foo !Int
std::list vs std::vectorST monadmap, fold, filter, etc)Foldable and TraversableHow much memory does the following code use?
import qualified Data.Vector.Unboxed as V
main = print $ V.sum $ V.enumFromTo 1 (10^9 :: Int)
map f . map g = map (f . g)
Monoid){-# LANGUAGE OverloadedStrings #-}
import Data.Streaming.Network
import Control.Monad
import Control.Concurrent
main :: IO ()
main = runTCPServer (serverSettingsTCP 4500 "*") $ \ad ->
forever $ do
bs <- appRead ad
print $ "Received: " ++ show bs
appWrite ad "Now I'm going to delay...\n"
threadDelay 1000000
IORef, MVar, TVar)Int -> String does not modify the worldparMap)import Control.Concurrent.STM
import Control.Concurrent.Async
main = do
alice <- newTVarIO 50
bob <- newTVarIO 50
runConcurrently $
Concurrently (transfer 60 alice bob) *>
Concurrently (transfer 30 bob alice) *>
Concurrently (transfer 40 bob alice)
where
transfer amt srcVar dstVar = atomically $ do
modifyTVar dstVar (+ amt) -- yes, even this position works!
srcOrig <- readTVar srcVar
let srcNew = srcOrig - amt
check (srcNew >= 0)
writeTVar srcVar srcNew
race and bothConcurrency)import Conduit
import qualified Data.Text as T
main :: IO ()
main = stdinC
$$ mapC T.toUpper
=$ filterCE (/= 'E')
=$ stdoutC
Easy way to get started
#!/usr/bin/env stack
-- stack --resolver lts-3.2 --install-ghc runghc --package turtle
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main = echo "Hello World!"
Thanks for being a great audeince