Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx committed Feb 19, 2016
1 parent 8d57137 commit 415ef16
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 0 deletions.
44 changes: 44 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* jshint node: true */
"use strict";

var gulp = require("gulp");
var purescript = require("gulp-purescript");
var webpack = require("webpack-stream");

var sources = [
"src/**/*.purs",
"bower_components/purescript-*/src/**/*.purs"
];

var foreigns = [
"src/**/*.js",
"bower_components/purescript-*/src/**/*.js"
];

gulp.task("make", function() {
return purescript.psc({ src: sources, ffi: foreigns });
});

gulp.task("prebundle", ["make"], function() {
return purescript.pscBundle({
src: "output/**/*.js",
output: "dist/bob16.js",
module: "Main",
main: "Main"
});
});

gulp.task("bundle", ["prebundle"], function () {
return gulp.src("dist/bob16.js")
.pipe(webpack({
resolve: { modulesDirectories: ["node_modules"] },
output: { filename: "bob16.js" }
}))
.pipe(gulp.dest("static"));
});

gulp.task("watch", function () {
return gulp.watch(sources.concat(foreigns), ["bundle"]);
});

gulp.task("default", ["bundle"]);
25 changes: 25 additions & 0 deletions src/Main.js-spoiler-05-ffi
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global exports */
"use strict";

var greetInJs = function(target) {
console.log(target);
return (function() {});
};

var loadResource = function(link) {
link.content_type = "overwrite"; // write access
console.log(link); // read access
return (function() {});
};

var loadResourceAsync = function(link) {
return function(callback) {
callback(3)();
return (function() {});
}
}

// module Main
exports.greetInJs = greetInJs;
exports.loadResource = loadResource;
exports.loadResourceAsync = loadResourceAsync;
11 changes: 11 additions & 0 deletions src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Main
where

import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Console


main :: forall eff. Eff ( console :: CONSOLE | eff ) Unit
main = do
log "hello, world!"
11 changes: 11 additions & 0 deletions src/Main.purs-spoiler-01-hello-world
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Main
where

import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Console


main :: forall eff. Eff ( console :: CONSOLE | eff ) Unit
main = do
log "hello, world!"
94 changes: 94 additions & 0 deletions src/Main.purs-spoiler-02-basics
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module Main
where

import Control.Monad.Eff
import Control.Monad.Eff.Console
import Data.Array
import Prelude


-- expressions, names, types

someTarget :: String
someTarget = "world"

pi :: Number
pi = 3.14159


-- functions

greet :: String -> String
greet target = "hi " <> target

enumerate :: Int -> Array Int
enumerate 1000 = []
enumerate i = i : enumerate (i + 1)

naturals :: Array Int
naturals = enumerate 1


-- rows, type synonyms

type Resource = { content_type :: String
, path :: String
}

resourceLink :: Resource
resourceLink = { content_type: "text/html;charset=utf8"
, path: "index.html"
}

setPath :: forall extra.
String
-> { path :: String | extra }
-> { path :: String | extra }
setPath new link = link { path = new }


-- algebraic data types, pattern matching

data MyBool = MyTrue | MyFalse

myBoolToCBool :: MyBool -> Int
myBoolToCBool MyTrue = 0
myBoolToCBool MyFalse = 1

myIf :: forall t. MyBool -> t -> t -> t
myIf MyTrue thn _ = thn
myIf MyFalse _ els = els

data Expr =
Add Expr Expr
| Mul Expr Expr
| Num Int

calc :: Expr -> Int
calc (Add a b) = calc a + calc b
calc (Mul a b) = calc a * calc b
calc (Num a) = a

testCalc :: Int
testCalc = calc
( Add
( Mul (Num 3) (Add (Num 1) (Num 2)) )
( Num 19 )
)


-- taking a look

main :: forall eff. Eff ( console :: CONSOLE | eff ) Unit
main = do
print (greet someTarget)
print pi
print naturals
print resourceLink.content_type
print resourceLink.path
let resourceLinkNew = setPath "new_index.html" resourceLink
print resourceLinkNew.path
print (myBoolToCBool MyTrue)
print (myIf MyTrue "yes" "no")
print (myIf MyFalse "yes" "no")
print testCalc
70 changes: 70 additions & 0 deletions src/Main.purs-spoiler-03-halogen
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Main where

import Control.Monad.Aff (runAff, Aff())
import Control.Monad.Aff.Class (MonadAff)
import Control.Monad.Aff.Console (log)
import Control.Monad.Eff.Console (CONSOLE())
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (throwException)
import Halogen ( Component(), ComponentHTML(), ComponentDSL(), HalogenEffects(), Natural()
, component, runUI, liftAff'
)
import Halogen.Util (appendToBody, onLoad)
import Prelude
import Prim

import qualified Halogen.HTML.Events.Indexed as E
import qualified Halogen.HTML.Indexed as H
import qualified Halogen.HTML.Properties.Indexed as P


-- app state

data State = State

initialState :: State
initialState = State


-- event handlers

data Query a = Tick a


-- from state to html

render :: State -> ComponentHTML Query
render _ = H.div_
[ H.div [] [H.text "hello universe!"]
, H.input
[ P.inputType P.InputSubmit
, P.value "click me!"
, E.onClick (E.input_ Tick)
]
]


-- from events to state

type CalcAff eff = Aff (HalogenEffects (console :: CONSOLE | eff))
type CalcDSL eff = ComponentDSL State Query (CalcAff eff)

eval :: forall eff. Natural Query (CalcDSL eff)
eval (Tick next) = do
liftAff' (log "clicked!")
return next


-- throwing it all together

type CalcComponent eff = Component State Query (CalcAff eff)

ui :: forall eff. CalcComponent eff
ui = component render eval

main :: forall eff. Eff (HalogenEffects (console :: CONSOLE | eff)) Unit
main = runAff throwException (const (pure unit)) appAsync
where
appAsync = do
{ node: node, driver: driver } <- runUI ui initialState
onLoad (appendToBody node)
114 changes: 114 additions & 0 deletions src/Main.purs-spoiler-04-halogen-advanced
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
module Main where

import Control.Monad.Aff (runAff)
import Control.Monad.Aff.Class (MonadAff)
import Control.Monad.Aff.Console (log)
import Control.Monad.Eff.Console (CONSOLE())
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (throwException)
import Data.Array
import Data.Maybe
import Data.Tuple
import Halogen ( Component(), ComponentHTML(), ComponentDSL(), HalogenEffects(), Natural()
, component, modify, runUI
)
import Halogen.Query (liftAff')
import Halogen.Util (appendToBody, onLoad)
import Prelude
import Prim

import qualified Halogen.HTML.Events.Indexed as E
import qualified Halogen.HTML.Indexed as H
import qualified Halogen.HTML.Properties.Indexed as P


-- app state

data State = State (Array String) (Array Int) Int

initialState :: State
initialState = State [] [] 0

-- event handlers

data Button = Eval | Value Int | Plus

data Query a = Click Button a

type CalcEffects eff = HalogenEffects (console :: CONSOLE | eff)

-- from state to html

css :: forall p a. String -> P.IProp ("class" :: P.I | p) a
css = P.class_ <<< H.className

render :: State -> ComponentHTML Query
render (State log stack value) = H.div_
[ H.div [css "value"] [H.text (show (Tuple stack value))]
, H.input
[ P.inputType P.InputSubmit
, P.value "="
, E.onClick (E.input_ (Click Eval))
]

, valueButton 1
, valueButton 2
, valueButton 3
, valueButton 4
, valueButton 847

, H.input
[ P.inputType P.InputSubmit
, P.value "+"
, E.onClick (E.input_ (Click Plus))
]

, H.div_ (map (\entry -> H.div_ [H.text entry]) log)

]

valueButton :: Int -> ComponentHTML Query
valueButton i = H.input
[ P.inputType P.InputSubmit
, P.value (show i)
, E.onClick (E.input_ (Click (Value i)))
]


-- from events to state

type CalcAff eff = Aff (HalogenEffects (console :: CONSOLE | eff))
type CalcDSL eff = ComponentDSL State Query (CalcAff eff)

eval :: forall eff. Natural State Query (CalcDSL eff)
eval (Click button next) = do
liftAff' (log "click!")
modify (\(State log arr i) -> case button of
Eval -> State log arr (i + 1)
Value i' -> State log (i' : arr) i
Plus -> let newEntry = show arr ++ " -> " ++ show value
value = sum arr
in State (newEntry : log) [] value)
return next

sum :: Array Int -> Int
sum [] = 0
sum arr = maybe 0 id i + maybe 0 sum is
where
i = head arr
is = tail arr


-- throwing it all together

type CalcComponent eff = Component State Query (CalcAff eff)

ui :: forall eff. CalcComponent eff
ui = component render eval

main :: forall eff. Eff (CalcEffects eff) Unit
main = runAff throwException (const (pure unit)) appAsync
where
appAsync = do
{ node: node, driver: driver } <- runUI ui initialState
onLoad (appendToBody node)
Loading

0 comments on commit 415ef16

Please sign in to comment.