Skip to content

Instantly share code, notes, and snippets.

@RAbraham
Last active April 21, 2016 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RAbraham/5d4e8c8596c94c7ce87a31a449ddffe0 to your computer and use it in GitHub Desktop.
Save RAbraham/5d4e8c8596c94c7ce87a31a449ddffe0 to your computer and use it in GitHub Desktop.
PureScript Intro Meetup April 20

Primary Motivation

  • Dynamic Typing: Personally lost 10 hours or more.
  • Elegant code: No Defensive programming. Code with clear intent

Setup

Mac OS

brew update
brew install node
npm install -g bower purescript pulp

Atom IDE

  • https://atom.io/
  • Install Packages: linter, language-purescript, ide-purescript

Create project

mkdir my-purescript-project
cd my-purescript-project
pulp init
pulp run
# Adding purescript packages
bower install --save purescript-maybe

Code Demo(No IO/Effects)

Purescript Console

pulp psci

Code

x :: Int
x = 5
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
    

Further Reading

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