Git Product home page Git Product logo

purescript-oak's Introduction

Oak is an implementation of the Elm architecture in Purescript.

bower install purescript-oak

This library requires the virtual-dom module. You can get it by using npm to install virtual-dom.

npm install virtual-dom

Documentation is published on pursuit.

A breif example Oak app:

module Main (main) where

import Prelude
  ( Unit
  , (+)
  , (-)
  , bind
  , unit
  , discard
  )

import Data.Monoid (mempty)
import Effect.Random (randomInt)

import Effect (Effect)
import Effect.Console (log)
import Oak
  ( App
  , createApp
  , runApp
  )
import Oak.Html
  ( Html
  , div
  , text
  , button
  , br
  )
import Oak.Html.Events ( onClick )
import Oak.Document
  ( appendChildNode
  , getElementById
  )


type Model =
  { number :: Int
  }


data Msg
  = Inc
  | Dec
  | GetRand
  | Set Int


view :: Model -> Html Msg
view model =
  div []
    [ button [ onClick Inc ] [ text "+" ]
    , div [] [ text model.number ]
    , button [ onClick Dec ] [ text "-" ]
    , br [] []
    , button [ onClick GetRand ] [ text "random" ]
    ]

next :: Msg -> Model -> (Msg -> Effect Unit) -> Effect Unit
next GetRand model h = do
  log "generating random int"
  n <- randomInt 0 100
  h (Set n)
next _ _ _       = mempty

update :: Msg -> Model -> Model
update msg model =
  case msg of
    Inc ->
      model { number = model.number + 1 }
    Dec ->
      model { number = model.number - 1 }
    (Set n) ->
      model { number = n }
    GetRand ->
      model

init :: Model
init =
  { number: 0
  }

app :: App Model Msg
app = createApp
  { init: init
  , view: view
  , update: update
  , next: next
  }

main :: Effect Unit
main = do
  rootNode <- runApp app
  container <- getElementById "app"
  appendChildNode container rootNode

purescript-oak's People

Contributors

ehrenmurdick avatar desmondrawls avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.