PureScript & Pux

bit.ly/purescript-pux-talk

bit.ly/purescript-pux-repo

Typed

Pure

Functional

Typed

Types are known at compile time; they're static

Types can be inferred by the compiler

Pure

inputs -> outputs

No side-effects

Effects As Data

						
Eff (Effect) Result
						
					
						
f :: Eff (dom :: DOM) Location

g :: Eff (now :: NOW) Instant

h :: UserId -> Aff (ajax :: AJAX) User
						
					

Functional

Literally just programming with functions.

Code

Functions

						
add :: Number -> Number -> Number
add x y = x + y
						
					
						
add 10 11
						
					
						
arrayMap :: (a -> b) -> [a] -> [b]
arrayMap = map
						
					

Data Types

						
data RGB = Red | Green | Blue
						
					
						
data Action = AddUser User | UpdateName String
						
					
						
data User = User String Number
						
					

Records

						
type User =
  { name :: String
  , age :: Number
  }
						
					

Newtype

						
newtype Name = Name String

newtype Age = Age Number

newtype User = User { name :: Name
                    , age :: Age
                    }
						
					

Pattern Matching

						
royGeeBiv :: RGB -> String
royGeeBiv R = "Roy"
royGeeBiv G = "Gee"
royGeeBiv B = "Biv"
						
					
						
data UserAccount =
    Guest
  | Authenticated { name :: String }

greetUser :: UserAccount -> String
greetUser Guest = "Wanna create an account"
greetUser (Authenticated { name }) = "Welcome back " <> name
						
					
						
isItOne :: Int -> Boolean
isItOne 1 = true
isItOne _ = false
						
					

Fancy Types

Monoid

Functor

Monad

						
class Eq a where
eq :: a -> a -> Boolean
						
					

Pux

Pux is a PureScript interface to React, similar to the Elm app It is a simple pattern for modular, nested components that are easy to test, refactor, and debug...
http://www.alexmingoia.com/purescript-pux/

The Elm Architecture?

The Elm Architecture is a simple pattern for architecting webapps. It is great for modularity, code reuse, and testing.
https://guide.elm-lang.org/architecture/

An Example

						
data Action = Increment | Decrement
						
					
						
type State = Int

init :: State
init = 0
						
					
						
update :: Action -> State -> State
update Increment state = state + 1
update Decrement state = state - 1
						
					
						
view :: State -> Html Action
view state =
  div
    []
    [ h1 [ className "page-title" ] [ text "Counter" ]
    , button [ onClick (const Increment) ] [ text "Increment" ]
    , span [] [ text (show state) ]
    , button [ onClick (const Decrement) ] [ text "Decrement" ]
    ]
						
					

Demo

Why

Testing

Reliability

Reasoning

						
update :: Action -> State -> State
						
					

Resources

PureScript & Pux: A Safer Way To React

PureScript website

PureScript By Example

Pux Gitbook

Haskell Book

Descriptive Variable Names: A Code Smell

Thanks.

@thunk_life