- Dynamic Typing: Personally lost 10 hours or more.
- Elegant code: No Defensive programming. Code with clear intent
brew update
brew install node
npm install -g bower purescript pulp
mkdir my-purescript-project
cd my-purescript-project
pulp init
pulp run
# Adding purescript packages
bower install --save purescript-maybe
salute :: String -> String
salute name = "Hi" ++ name
-- DON'T SAVE
square :: Int -> Int
square x = x * x
-- Currying
-- PSCi
> import Prelude
> let sayHi fName lName = "Hi " ++ fName ++ " " ++ lName
> let x = sayHi "R" "A"
> x
> let f = sayHi "Maker"
> :type f
> f "Bot"
-- Records. In PSCi
import Prelude
let normalPerson = { firstName: "John", lastName: "Doe" }
let abnormalPerson = { firstName: "D", lastName: "Trump", egoSize: 33}
let dog = { firstName: "Lassy", hair: "golden brown" }
let showPerson p = p.lastName ++ ", " ++ p.firstName
-- Pattern Matching
let showPersonPM { firstName: x, lastName: y } = y ++ ", " ++ x
-- GCD. In Atom and then PSCi
gcd :: Int -> Int -> Int
gcd n 0 = n
gcd 0 m = m
gcd n m = if n > m then gcd (n - m) m else gcd n (m - n)
-- pulp psci
import Main
gcd 5 0
-- No Nulls
-- psci> bower install --save purescript-maybe
-- psci> bower install --save purescript-maps
import Data.Map
add :: Map Int Int -> Int -> Int -> Int
add aMap aKey y = Data.Map.lookup aKey aMap + y
-- SAVE
-- Algebraic Data Types(Union Type)
-- data Bool = False | True
-- data Maybe a = Nothing | Just a
import Data.Maybe
import Data.Map
add :: Map Int Int -> Int -> Int -> Int
add aMap aKey y =
case aVal of
Just x -> x + y
Nothing -> y
where
aVal = Data.Map.lookup aKey aMap
-- A: Remove the Nothing clause
-- Algebraic Data Types(revisited)
-- data Maybe a = Nothing | Just a
-- Maybe is called a Type Constructor(like Class Name).
-- Nothing, Just is called a Data Constructor(like Factory Methods)
-- a is a parameterized type (like Java Generics T)
-- Enum
data Thing = Shoe
| Ship
| SealingWax
| Cabbage
| King
-- show
isSmall :: Thing -> Boolean
isSmall Ship = false
isSmall King = false
isSmall _ = true
main = do log (isSmall Cabbage)
-- Tree!
data Tree a = Leaf a | Node Tree a Tree
-- 5 Type Inference. In Atom
add x y = x + y
-- Calling Javascript (Foreign Function Interface)
foreign import strReverse :: String -> String
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log (strReverse "Hi")
-- A: Create Main.js, In Main.js
// module Main
exports.strReverse = function (str) {
return "Reversed String"
}
-- Example only
class Show a where
show :: a -> String
--
instance showBoolean :: Show Boolean where
show true = "true"
show false = "false"
data Emotion a = Love | Hate a
instance showEmotion :: Show a => Show (Emotion a) where
show Love = "Love is unconditional"
show (Hate a) = "Love is unconditional except for " ++ show a