getting it done with haskell

30
8/11/2019 Getting It Done With Haskell http://slidepdf.com/reader/full/getting-it-done-with-haskell 1/30 Getting it Done with Haskell From Elegance to Engineering Greg Weber - doxIQ BayHac 2014

Upload: safdsaf

Post on 03-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 1/30

Getting it Donewith Haskell

From Elegance to Engineering

Greg Weber - doxIQBayHac 2014

Page 2: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 2/30

Motivation

You already have an understanding of H

You want some suggestions and guidan

how to effectively write useful programs

Page 3: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 3/30

Page 4: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 4/30

Real World Haskell

Takes time to show concrete

instance of building real-world code.

Starting to get dated. Haskell libraries and best prahave evolved a lot in certain areas since RWH waspublished.

For example, the chapters on databases now feelslevel and their is a lack of information on web servprogramming.

Page 5: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 5/30

Page 6: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 6/30

Beginning Haskell

Kind of a modern RWH

● More beginner focus● Less of a deep dive into real world code

● Smaller code samples● Shows how to use modern libraries● Missing some things (example: command line, ● Looks good to me, but I am no longer a beginn

Page 7: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 7/30

Parallel & Concurrent ...

Great on its topic Author created async package

This topic spans most of the benefitsof the GHC implementation.

● async IO, but write in a blocking style

● STM

Page 8: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 8/30

Typeclassopedia

Learn (most of) this!

opinion: Don’t bother with Arrow* until la

Page 9: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 9/30

Typeclasses + Data Structures

composition & re-useapply abstract typeclasses to

concrete Data structures

This cheatsheet gives a great overview

Page 10: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 10/30

mono-traversable

Extends the concept ofsome of the coretypeclasses (Functor,Foldable, Traversable) towork on Monomorphic types(Text, ByteString, etc).

type family Element mon

type instance Element T.

instance MonoFunctor T.  omap = T.map

instance MonoFoldable T  ofoldMap f = ofoldr (ma  ofoldr = T.foldr   ofoldl' = T.foldl'  ...

Page 11: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 11/30

Page 12: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 12/30

Error Handling

Pure code

  * do not throw an exception, ever!  * Maybe/Either 

  * Use ErrorT/EitherT to break out if needed

IO: you must deal with exceptions alread● if you are doing throwError "string", you really j

2 separate types of error mechanisms to handl● instead, just use exceptions from the exception

Page 13: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 13/30

Page 14: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 14/30

Threads + Exceptions

By default, threads die silently

 Add a default exception handler 

Link your threads together link :: Async a -> IO ()

Link the given Async to the current thread, such that if the Async raises an exception, that exception will be re-thrown in t

myThreadId … forkIO $ catchAny … throwTo

Page 15: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 15/30

Page 16: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 16/30

Avoid errors altogether 

-- Data.List

-- watch out for the hidden exception!

head :: [a] -> a

head (x: _ )  =  xhead [] =  error "head: empty list"

-- List has an information loss

x : y : []

-- Data.List.NonEmpty (s

head :: NonEmpty a -> a

head (a :| _ ) = a

-- Prove your data structu

 -- NonEmpty knows ther

x :| (y : [])

-- Data.NonNull from mono-travesable. Apply NonEmpty concept to an

-- Also, use type-level numbers to be a little smarter.

head :: MonoTraversable mono => MinLen (Succ nat) mono -> Elemen

head :: MinLen (Succ nat) (Seq a) -> a

Page 17: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 17/30

Prefer NonEmpty to safesafe provides exception free functions or exceptionfunctions that you can provide useful error messageswith

By giving a unique error message with the exactfunction name, we can now track down the error.

But we already said: never throw an exception in purecode! (It can actually be ok to do this in your application, just don’t

share that code with anyone).

Prelude> head []

*** Exception: Prlist

-- headNote :: St

Safe> headNote

*** Exception: Pahead [], bad func

-- headMay :: [a

Safe> headMay

Nothing

So lets use headMay. But now we have to deal with the Maybe.The problem is we decided to drop information and defer figuring thinglater.

Instead, encode information in your program when you first have it.

Page 18: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 18/30

classy-prelude

no partial functionssome API cleanup

  * constant memory implementations (fo

mapM_)  * better grouping functionality

good exception handling tools by default

mono-traversable

Page 19: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 19/30

Libraries

Why you may not be more productive in

Haskell

But there are so many libraries now!They aren’t always as polished as their counterparts in other languamore likely to be in a quasi-abandoned state.

● Be prepared to dig into any library you use andpull request.

● Be prepared to maintain some libraries

● Be prepared to write new libraries

W iti / i t i lib i

Page 20: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 20/30

Writing /maintaing librariesGregWeber is part of the f ollowing groups:

● Maintainer s for azurify● Maintainer s for cabal-meta● Maintainer s for css-text

● Maintainer s for dlist-instances● Maintainer s for file-location● Maintainer s for fsnotif y● Maintainer s for hamlet● Maintainer s for heroku● Maintainer s for hspec-expectations-pretty● Maintainer s for hspec● Maintainer s for monad-logger-syslog● Maintainer s for mongodb-queue● Maintainer s for par se-help

● Maintainer s for persistent-mongoDB● Maintainer s for persistent● Maintainer s for rollbar ● Maintainers for shakespeare● Maintainers for shelly-extra● Maintainers for shelly● Maintainers for spelling-suggest● Maintainers for sphinx● Maintainers for wai-static-pages● Maintainers for wai-test● Maintainers for xss-sanitize

● Maintainers for yesod-bin● Maintainers for yesod-core

In a team of Haskell programmeleast one member  that works fluesource community.

Stop creating an artificial dividingapplication and the libraries you running the code, you need to ow

Learn to vendor the libr aries that

If there is a bug in a library, it is yand probably to fix it if it is easy.

If you need a feature, it is your jotalking with the maintainers).

Doing this is perhaps the best wa

Page 21: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 21/30

Choosing libraries

● search google, ask questions, see

announcements● goto hackage.haskell.org● goto github page and look at README.● view the haddock documentation● look at the source code● look at the author (but don’t cargo cult)● look at the dependencies (may need to

Page 22: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 22/30

Testing

QuickCheck: brilliant: confirm your propetasty or hspec: organize your test suites

ran out of time for this slide

C b l

Page 23: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 23/30

Cabal

Jonathan already talked about that at Bay

 Application developers, do this:cabal sandbox init && cabal install && cabal freeze

Library authors should not freezeDependencies that are not on hackage:cabal sandbox add-source ../vendor/lib

cabal install --only-dep

Page 24: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 24/30

Development environment

Integrate these with your text editor/workstylish-haskell, hlint, ghc-mod, hasktags, codex

local hoogle (hoogle data)

online hayoo & hoogle (+module)

Page 25: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 25/30

Page 26: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 26/30

Page 27: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 27/30

Page 28: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 28/30

Command line parsing

Use optparse-applicative* combinators, decent static assurances

* help output

* command line completion

ran out of time ...

Sh ll S i ti ith Sh ll

Page 29: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 29/30

Shell Scripting with Shelly

  {-# LANGUAGE OverloadedStrings #-}

  {-# LANGUAGE ExtendedDefaultRules #-}

  {-# OPTIONS_GHC -fno-warn-type-defaults #-}

  import Shelly

  import Data.Text as T

  default (T.Text)

  main = shelly $ verbosely $ do

  d <- cmd "date"

  c <- escaping False $

  cmd "git" "log -1 | head -1 | awk '{print $2}'"

  appendfile "log/deploy.log" $ T.intercalate " - " [T.stripEnd d, c]

Using shakespeare-text

From yesod developme

errorExit [lt|

  installation failure!

  Please try a clean build with: ./script/install

  If  you are peforming a clean install and have

  Please report this error to the mail list at htcom/group/yesodweb

  or on the issue tracker at http://github.com/

|]

Page 30: Getting It Done With Haskell

8/11/2019 Getting It Done With Haskell

http://slidepdf.com/reader/full/getting-it-done-with-haskell 30/30